zoj 3659 Conquer a New Region
// 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大
// i 到 j的最大权值为 i到j所经历的树边容量的最小值
// 第一感觉是树上的dp
// 后面发现不可以
// 看了题解说是并查集
// 然后发现这不就是在最小生成树那个模板上做其它操作吗、、
// 的确是好题
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <string.h>
using namespace std;
#define maxn 200010
#define LL long long
struct Eg{
int a,b,w;
bool operator<(const Eg &t)const{
return w>t.w;
}
}E[maxn<<];
int f[maxn];
LL cnt[maxn],sum[maxn];
int Find(int x){
if(x!=f[x])
f[x]=Find(f[x]);
return f[x];
}
int main(){
int n;
int i;
while(scanf("%d",&n)!=EOF){
for(i=;i<n-;i++)
scanf("%d %d %d",&E[i].a,&E[i].b,&E[i].w);
sort(E,E+n-);
for(i=;i<=n;i++){
f[i]=i;
cnt[i]=;
sum[i]=;
}
LL ans=;
int u,v;
LL su,sv;
for(i=;i<n-;i++){
u=Find(E[i].a);
v=Find(E[i].b);
su=cnt[v]*E[i].w+sum[u];
sv=cnt[u]*E[i].w+sum[v];
if(su>sv){
f[v]=u;
cnt[u]+=cnt[v];
sum[u]=su;
}else {
f[u]=v;
cnt[v]+=cnt[u];
sum[v]=sv;
}
ans=max(ans,max(su,sv));
}
printf("%lld\n",ans);
}
return ;
}
zoj 3659 Conquer a New Region的更多相关文章
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest
Conquer a New Region Time Limit: 5 Seconds Memory Limit: 32768 KB The wheel of the history roll ...
- zoj 3659 Conquer a New Region(并查集)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...
- HDOJ 4424 Conquer a New Region
并检查集合 侧降序,每增加一个侧面应该推断,其中基本建设方..... Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Me ...
- ZOJ3659 Conquer a New Region 并查集
Conquer a New Region Time Limit: 5 Seconds Memory Limit: 32768 KB The wheel of the history roll ...
- ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)
这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...
- zoj 3659
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3659 #include<cstdio> #inclu ...
- hdu4424 Conquer a New Region 并查集/类似最小生成树
The wheel of the history rolling forward, our king conquered a new region in a distant continent.The ...
- HDU 4424 Conquer a New Region
http://acm.hdu.edu.cn/showproblem.php?pid=4424 [题目大意] 给你N个点和N-1条边的连通图,也就是说任意两点间的路径是唯一的.每条边有个权值,从一点到另 ...
随机推荐
- 坑人的 try catch finally
一直以为这样可以关闭 fs, 其实不行 static void Main(string[] args) { FileStream fs = null; try { fs = new FileStrea ...
- 宏基5750G 不能用内置无线网卡上网
宏基5750G 不能用内置无线网卡上网 具体体现在: 1.--> 搜索不到无线网络: 2.点击“打开网络和共享中心”-->没有“管理无线网络”一项.如果正常的话应该是这样的: 3.“我的电 ...
- 设置HTTP header方式
一, Server Code JSP----> <%@ page language="java" contentType="text/html; charse ...
- 如何使用工具进行线上 PHP 性能追踪及分析?
工作了一两年的 PHPer 大概都多多少少知道一些性能分析的工具,比如 Xdebug.xhprof.New Relic .OneAPM.使用基于 Xdebug 进行 PHP 的性能分析,对于本地开发环 ...
- poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)
题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...
- POJ 1486 Sorting Slides(寻找必须边)
题意:找出幻灯片与编号唯一对应的情况 思路: 1:求最大匹配,若小于n,则答案为none,否则转2 (不过我代码没有事先判断一开始的最大匹配数是否<n,但这样也过了,估计给的数据最大匹配数一定为 ...
- IDataParameter[]
IDataParameter[]简单的说来,就是可以包含任何数据库参数的数组,这些参数可以是SqlParameter或者OleDbParameter,但是,IDataParameter[]不可以直接从 ...
- http://www.aboutyun.com/thread-8792-1-1.html
http://www.aboutyun.com/thread-8792-1-1.html
- [优先队列]HDOJ5360 Hiking
题意:有n个人,每个人有两个参数$l$和$r$ 邀请他们去hiking, 当 当前已经邀请到的人数大于等于$l$,并且小于等于$r$,那么这个人就会去 问最多能邀请到几个人 并输出 依次要邀请的人的 ...
- Sina App Engine(SAE)入门教程(1)
此教程只针对刚接触SAE的小白用户,资深码农.高手请绕道.首先还是一个经典的实例,hello sae. 创建应用 在注册完账号之后,需要到 http://sae.sina.com.cn/?m=myap ...