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条边的连通图,也就是说任意两点间的路径是唯一的.每条边有个权值,从一点到另 ...
随机推荐
- [CF]codeforces round#366(div2)滚粗记
开场心理活动:啊打完这场大概有1700了吧 中途心理活动:啊这个ABC看起来都随便做啊 死亡原因:欸怎么没网了 -75 .. A [题意]Hulk说完一句I hate会说that I love 然后是 ...
- 如何通过热修复,搞定开发中的那些 Bug?
作为程序员,Bug 修复终究是绕不开的话题,本期移动开发精英俱乐部讨论的主题便是 Bug 修复中的 Hotfix,即热修复.接下来让我们跟随大牛的脚步来了解 Hotfix,就算你不能一下豁然开朗,相信 ...
- C++编写Config类读取配置文件
老外写的一段代码,在Server中编写这个类读取配置文件比较实用 //Config.h #pragma once #include <string> #include <map> ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 1928
#include <iostream> #include <algorithm> #define MAXN 3000 using namespace std; struct n ...
- gulp下livereload和webserver实现本地服务器下文件自动刷新
一.前言 node从v0.10.26升级(为了匹配autoprefixer)到v5.3.0后出现了gulp插件兼容问题,在nodejs下各种新的插件出现问题,需要重新配置.livereload实现ch ...
- 如何在服务(Service)程序中显示对话框
原文:http://www.vckbase.com/index.php/wv/94 服务程序(Service)一般是不能和用户进行交互的,所以他一般是不能显示窗口的.要和用户进行交互(如显示窗口),我 ...
- 如何实现Windows Phone代码与Unity相互通信(直接调用)
我之前用了两篇文章写了WP与Unity相互通信.调用的办法,一个是事件,一个是插件. 这次来说个更简单的,我觉得这应该是Unity发布到WP或者Win Store上得天独厚的优势.毕竟都是C#. 懒得 ...
- C++:this指针
this指针 this关键字:表示本类中的对象成员,可以通过this指针访问当前类的成员//举例 //例 3.18 隐藏this指针的引例 #include<iostream> using ...
- YASKAWA电机控制(1)---接线
实验室所购置电机型号为YASKAWA-AC SERVO MOTOR SGM7J-01AFC6S型,配SGD7S-R90A00A002伺服控制器.电机和控制器的操作说明书由安川中文官网安川电机资料提供. ...