题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; const int maxn = ; struct Edge
{
int u,v,w;
Edge(int u=,int v=,int w=): u(u), v(v), w(w) {}
bool operator < (const Edge& rhs) const
{
return w > rhs.w;
}
}edges[maxn]; int counts[maxn];
int pa[maxn];
long long cap[maxn]; int find(int x)
{
return x == pa[x] ? x : pa[x] = find(pa[x]);
} int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int N;
while(cin>>N)
{
for(int i=; i<=N; i++)
{
counts[i] = ;
pa[i] = i;
cap[i] = ;
} for(int i=; i<N; i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
edges[i] = Edge(a,b,c);
}
sort(edges+,edges+N); long long ans = ;
for(int i=; i<N; i++)
{
long long w = edges[i].w;
int u_fa = find(edges[i].u);
int v_fa = find(edges[i].v); //下面目的是维护根到集合中所有的点cap值最大
long long Lu = cap[u_fa] + w*counts[v_fa];
long long Rv = cap[v_fa] + w*counts[u_fa];
if(Lu > Rv)
{
pa[v_fa] = u_fa;
cap[u_fa] = Lu;
counts[u_fa] += counts[v_fa];
}
else
{
pa[u_fa] = v_fa;
cap[v_fa] = Rv;
counts[v_fa] += counts[u_fa];
}
}
for(int i=; i<=N; i++)
ans = max(ans,cap[i]);
cout<<ans<<"\n";
}
}

zoj 3659 Conquer a New Region(并查集)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. zoj 3659 Conquer a New Region

    // 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大// i 到 j的最大权值为 i到j所经历的树边容量的最小值// 第一感觉是树上的dp// 后面发现不可以// 看了题解说是并查集// ...

  4. ZOJ3659 Conquer a New Region 并查集

    Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history roll ...

  5. ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)

    这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...

  6. hdu4424 Conquer a New Region 并查集/类似最小生成树

    The wheel of the history rolling forward, our king conquered a new region in a distant continent.The ...

  7. hdu 4424 Conquer a New Region (并查集)

    ///题意:给出一棵树.树的边上都有边权值,求从一点出发的权值和最大,权值为从一点出去路径上边权的最小值 # include <stdio.h> # include <algorit ...

  8. zoj 3659 第37届ACM/ICPC 长春赛区现场赛E题 (并查集)

    题意:给出一棵树,找出一个点,求出所有点到这个点的权值和最大,权值为路径上所有边权的最小值. 用神奇的并查集,把路按照权值从大到小排序,然后用类似Kruskal的方法不断的加入边. 对于要加入的一条路 ...

  9. UVa 1664 Conquer a New Region(并查集)

    https://vjudge.net/problem/UVA-1664 题意: n个城市形成一棵树,每条边有权值C(i,j).任意两个点的容量S(i,j)定义为i与j唯一通路上容量的最小值.找一个点, ...

随机推荐

  1. jQuery 遍历祖先

    祖先是父.祖父或曾祖父等等. 通过 jQuery,您能够向上遍历 DOM 树,以查找元素的祖先. 向上遍历 DOM 树 这些 jQuery 方法很有用,它们用于向上遍历 DOM 树: parent() ...

  2. Gulp那些好用的插件 2016.04.20

    开始接触LESS.组件化编程后,慢慢意识到需要一个提高工作效率的构建工具,就此接触到了Gulp. Gulp的好处在这里就不细说啦,只有四个API接口学起来简直爽歪歪,减少了大量的I/O操作,用起来很畅 ...

  3. struts2 测试错题解析

    解析:$.parseJSON()方法是将字符串转换成Json类型数据,$.getJSON()方法是获取JSON数据,两者不用联合使用. 解析: A:ActionContext接口没有getReques ...

  4. 数据库(学习整理)----3--Oracle创建表和设置约束

    BBS论坛表设计 包含的表:BBSusers(用户表),BBSsection(版块表),BBStopic(主贴表),BBSreply(跟帖表) 表结构 1)BBSusers 字段名 字段说明 数据类型 ...

  5. Constructor and destructor -- Initialization & Cleanup in C++

    Why need initialization and cleanup? A large segment of C bugs occur when the programmer forgets to ...

  6. ARM开发板系统移植-----u-boot的编译

    本文和另外两篇姊妹篇都是为了说明如何裁剪出适合在mini2440 开发板上运行的Linux系统,以记录自己的学习成果.其中本文先介绍了嵌入式系统的软件组成部分,然后介绍编译出适合在mini2440开发 ...

  7. ip输入框键入.或者合法数字自动选择下一个输入框效果

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...

  8. Android Handler、Lopper消息驱动机制

    Android应用程序是通过消息来驱动的,系统为每一个应用程序维护一个消息队例(MesageQueue),应用程序的主线程不断地从这个消息队例中获取消息(Mesage),然后对这些消息进行处理(Han ...

  9. 安装python-MySQLdb 出现error: command 'gcc' failed with exit status 1的解决方法

    >>> yum install MySQL-p* >>>yum install python-devel >>>cd MySQL-python-1 ...

  10. windows“画图”工具用法

    图片编辑工具不少,photoshop功能强大,但是并不是那么容易掌握.而且,倘若一个简单的图片处理问题就使用这么强大的工具,有点“杀鸡焉用牛刀”的意思,要知道只打开photoshop就需要一段等待时间 ...