题目链接: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. oracle 权限管理

    系统权限 系统权限需要授予者有进行系统级活动的能力,如连接数据库,更改用户会话.建立表或建立用户等等.你可以在数据字典视图SYSTEM_PRIVILEGE_MAP上获得完整的系统权限.对象权限和系统权 ...

  2. 对select into表复制的一点思考

    操作系统:Windows 2007 数据库版本:SQL Server 2008 R2 今天写存储过程遇到一个问题,用"Select 1 id,'Boss_he' into A"这样 ...

  3. 求最低分最高分---c#(冒泡排序的运用)

    // 输入10个人的分数,去掉两个最高两个最低,求平均分 Console.Write("请输入人数:"); int renshu = int.Parse(Console.ReadL ...

  4. MSSQL Server语句

    随机从数据库中取出20条数据:select top 20 * from 表名 order by newid()

  5. 理解MySQL——索引与优化(转)

    写 在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页 面大小为4K,并存储100条记录.如果没有索引,查 ...

  6. Linux权限体系总结

    总结 1.  LINUX9位权限及rwx字符的作用.对应的数字及对应的用户和用户组图解. 2.  rwx对文件来说代表什么意思. 3.  rwx对目录来说代表什么意思. 4.  企业生产环境目录设置权 ...

  7. MySQL设置

    在MySQL的使用中很容易出现乱码的情况. 实际上在MySQL中有个地方表明了系统中所用到的所有的字符集. 例如: 从中可以看出,对于server和database的默认字符集都是latin1,这样很 ...

  8. 纯JS写出日历

    封装代码: (function(cmf){ cmf.showDcalendar=function(){ var fnname=cmf.fn var id="cmfrili2" va ...

  9. C#中标准Dispose模式的实现

    http://www.cnblogs.com/luminji/archive/2011/03/29/1997812.html 需要明确一下C#程序(或者说.NET)中的资源.简单的说来,C#中的每一个 ...

  10. Sevlet局部变量初始化

    //java 代码部分package com.servlet; import java.io.IOException; import java.io.PrintWriter; import java. ...