并检查集合

侧降序,每增加一个侧面应该推断,其中基本建设方.....

Conquer a New Region

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1169    Accepted Submission(s): 373

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

There are N towns (numbered from 1 to N) in this region connected by several roads. It's confirmed that there is exact one route between any two towns. Traffic is important while controlled colonies are far away from the local country. We define the capacity
C(i, j) of a road indicating it is allowed to transport at most C(i, j) goods between town i and town j if there is a road between them. And for a route between i and j, we define a value S(i, j) indicating the maximum traffic capacity between i and j which
is equal to the minimum capacity of the roads on the route. 

Our king wants to select a center town to restore his war-resources in which the total traffic capacities from the center to the other N - 1 towns is maximized. Now, you, the best programmer in the kingdom, should help our king to select this center.
 
Input
There are multiple test cases.

The first line of each case contains an integer N. (1 <= N <= 200,000)

The next N - 1 lines each contains three integers a, b, c indicating there is a road between town a and town b whose capacity is c. (1 <= a, b <= N, 1 <= c <= 100,000)
 
Output
For each test case, output an integer indicating the total traffic capacity of the chosen center town.
 
Sample Input
4
1 2 2
2 4 1
2 3 1
4
1 2 1
2 4 1
2 3 1
 
Sample Output
4
3
 
Source
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=200200; typedef long long int LL; struct Edge
{
LL u,v,w;
}edge[maxn]; bool cmp(Edge x,Edge y)
{
return x.w>y.w;
} LL n;
LL fa[maxn];
LL value[maxn];
LL sz[maxn]; LL find(LL x)
{
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
} int main()
{
while(scanf("%I64d",&n)!=EOF)
{
for(LL i=0;i<n-1;i++)
{
LL a,b,c;
scanf("%I64d%I64d%I64d",&a,&b,&c);
edge[i].u=a; edge[i].v=b; edge[i].w=c;
fa[i]=i;sz[i]=1;value[i]=0;
}
fa[n-1]=n-1; sz[n-1]=1; value[n-1]=0;
fa[n]=n;sz[n]=1;value[n]=0;
sort(edge,edge+n-1,cmp);
for(LL i=0;i<n-1;i++)
{
LL u=edge[i].u,v=edge[i].v; LL w=edge[i].w;
LL U=find(u),V=find(v);
if(U==V) continue;
LL VVV=max(value[U]+w*sz[V],value[V]+w*sz[u]);
fa[U]=V;
value[V]=VVV;
sz[V]=sz[V]+sz[U];
}
cout<<value[find(1)]<<endl;
}
return 0;
}

版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss

HDOJ 4424 Conquer a New Region的更多相关文章

  1. HDU 4424 Conquer a New Region

    http://acm.hdu.edu.cn/showproblem.php?pid=4424 [题目大意] 给你N个点和N-1条边的连通图,也就是说任意两点间的路径是唯一的.每条边有个权值,从一点到另 ...

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

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

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

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

  4. HDU 4424 Conquer a New Region 最大生成树

    给你一颗树 每条边有一个权值 选择一个点为中心 定义S值为中心到其它n-1个点的路径上的最小边权 求全部点S值的和 从大到小排序 每次合并2棵树 设为A集合 B集合 设A集合的最大S值的和为sumA ...

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

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

  7. ZOJ3659 Conquer a New Region 并查集

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

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

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

  9. zoj 3659 Conquer a New Region

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

随机推荐

  1. Error : APP-FND-01926: The custom event WHEN-LOGON-CHANGED raised unhandled exception: ORA-06502: PL

    In this Document   _afrLoop=440418974213449&id=1508865.1&_afrWindowMode=0&_adf.ctrl-stat ...

  2. SSDTHook实例--编写稳定的Hook过滤函数

    解说怎样写Hook过滤函数,比方NewZwOpenProcess.打开进程. 非常多游戏保护都会对这个函数进行Hook. 因为我们没有游戏保护的代码,无法得知游戏公司是怎样编写这个过滤函数. 我看到非 ...

  3. leetcode第一刷_Permutations II

    当有反复元素的时候呢? 不用拍脑袋都会想到一种方法,也是全部有反复元素时的通用处理方法,维护一个set,假设这个元素没增加过就增加,增加过了的忽略掉.可是,在这道题上这个通用方法竟然超时了! 怎么办? ...

  4. JDBC数据库连接

    JDBC是什么? Java Data Base Connectivity JDBC是: 以统一方式訪问数据库的API,能够訪问不论什么类型表列数据.特别是存储在关系数据中的数据.JDBC代表Java数 ...

  5. 基于FFMPEG和SDL实现视频播放器

    这个是雷大牛实现的project. http://download.csdn.net/detail/leixiaohua1020/5122959 有兴趣的能够好好研究研究.

  6. 在Java中如何使用jdbc连接Sql2008数据库(转)

    我们在javaEE的开发中,肯定是要用到数据库的,那么在javaEE的开发中,是如何使用代码实现和SQL2008的连接的呢?在这一篇文章中,我将讲解如何最简单的使用jdbc进行SQL2008的数据库的 ...

  7. codeforces 112APetya and Strings(字符串水题)

    A. Petya and Strings 点击打开题目 time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. 设置 zend studio 默认编码为UTF8

    今天用zend studio 打开文件时发现为乱码,这肯定是编码出了问题,我看了一下果然是编码出了问题,默认的是以GBK编码方式打开,我换utf8编码打开就好了,换编码打开的方法是: 1点击工具栏中的 ...

  9. hdu 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】(转)

    HDU 动态规划(46道题目)倾情奉献~ [只提供思路与状态转移方程] Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包 ...

  10. C++结构体:默认构造函数,复制构造函数,重载=运算符

    C++结构体提供了比C结构体更多的功能,如默认构造函数,复制构造函数,运算符重载,这些功能使得结构体对象能够方便的传值. 比如,我定义一个简单的结构体,然后将其作为vector元素类型,要使用的话,就 ...