The Experience of Love

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 645    Accepted Submission(s): 216

Problem Description
A girl named Gorwin and a boy named Vivin is a couple. They arrived at a country named LOVE. The country consisting of N cities and only N−1
edges (just like a tree), every edge has a value means the distance of
two cities. They select two cities to live,Gorwin living in a city and
Vivin living in another. First date, Gorwin go to visit Vivin, she would
write down the longest edge on this path(maxValue).Second date, Vivin
go to Gorwin, he would write down the shortest edge on this
path(minValue),then calculate the result of maxValue subtracts minValue
as the experience of love, and then reselect two cities to live and
calculate new experience of love, repeat again and again.

Please help them to calculate the sum of all experience of love after they have selected all cases.

 
Input
There will be about 5 cases in the input file.
For each test case the first line is a integer N, Then follows n−1 lines, each line contains three integers a, b, and c, indicating there is a edge connects city a and city b with distance c.

[Technical Specification]
1<N<=150000,1<=a,b<=n,1<=c<=109

 
Output
For
each case,the output should occupies exactly one line. The output
format is Case #x: answer, here x is the data number, answer is the sum
of experience of love.
 
Sample Input
3
1 2 1
2 3 2
5
1 2 2
2 3 5
2 4 7
3 5 4
 
Sample Output
Case #1: 1
Case #2: 17

Hint

huge input,fast IO method is recommended.

In the first sample:
The maxValue is 1 and minValue is 1 when they select city 1 and city 2, the experience of love is 0.
The maxValue is 2 and minValue is 2 when they select city 2 and city 3, the experience of love is 0.
The maxValue is 2 and minValue is 1 when they select city 1 and city 3, the experience of love is 1.
so the sum of all experience is 1;

 
 
题意:给出一棵树n个结点n-1条边,找到所有的两个点之间的最大值和最小值,求 sum(MAX-MIN).
题解:sum(MAX-MIN) = sum(MAX)-sum(MIN)很巧妙的思路,并查集将n-1条边添加进去,按照边权从小到大排序,如果现在找到了点 a ,b 那么a的所有子节点和 b的所有子节点中的最大权边必定为 edge[a][b] 所以我们根据乘法原理可以得出当前的所有最大权为 edge[a][b]  的点,他们对最大值结果的贡献为 cnt[a]*cnt[b]*edge[a][b].最小值的话从大到小选就行了。最后结果会爆long long ,所以要开 unsigned long long ,输入输出用 %I64u
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <math.h>
  7. using namespace std;
  8. typedef unsigned long long LL;
  9. const int N = ;
  10. struct Edge
  11. {
  12. LL u,v;
  13. LL w;
  14. } edge[N];
  15. LL father[N];
  16. LL cnt[N];
  17. LL MAX,MIN;
  18. LL _find(LL x)
  19. {
  20. if(x!=father[x]){
  21. father[x] = _find(father[x]);
  22. }
  23. return father[x];
  24. }
  25. void init(int n)
  26. {
  27. for(int i=; i<=n; i++)
  28. {
  29. father[i] = i;
  30. cnt[i] = ;
  31. }
  32. }
  33. void Union(LL a,LL b,LL w,int flag)
  34. {
  35. LL x = _find(a);
  36. LL y = _find(b);
  37. if(flag)
  38. {
  39. MAX+=cnt[x]*cnt[y]*w;
  40. }
  41. else
  42. {
  43. MIN+=cnt[x]*cnt[y]*w;
  44. }
  45. father[x] = y;
  46. cnt[y]+=cnt[x];
  47. }
  48. int cmp(Edge a,Edge b)
  49. {
  50. return a.w<b.w;
  51. }
  52. int main()
  53. {
  54. int n;
  55. int t = ;
  56. while(scanf("%d",&n)!=EOF)
  57. {
  58. init(n);
  59. for(int i=; i<n; i++)
  60. {
  61. scanf("%I64u%I64u%I64u",&edge[i].u,&edge[i].v,&edge[i].w);
  62. }
  63. MAX = ,MIN = ;
  64. sort(edge+,edge+n,cmp);
  65. for(int i=; i<n; i++)
  66. {
  67. Union(edge[i].u,edge[i].v,edge[i].w,);
  68. }
  69. init(n);
  70. for(int i=n-; i>=; i--)
  71. {
  72. Union(edge[i].u,edge[i].v,edge[i].w,);
  73. }
  74. printf("Case #%d: ",t++);
  75. printf("%I64u\n",MAX-MIN);
  76. }
  77. return ;
  78. }

hdu 5176(并查集)的更多相关文章

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  3. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  4. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  5. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  6. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  7. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  8. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. Python调用MYSQL,将文件名和路径批量入库用法小结

    最近项目需要将大量的压缩文件导入到数据库中,所以开始总结用Python批量处理的办法,本次是首先将这些压缩文件的文件名提取出来,然后导入到数据库中. 由于涉及到路径的读取处理,所以方法有os模块和co ...

  2. Python两个内置函数——locals 和globals (学习笔记)

    这两个函数主要提供,基于字典的访问局部和全局变量的方式.在理解这两个函数时,首先来理解一下python中的名字空间概念.Python使用叫做名字空间的东西来记录变量的轨迹.名字空间只是一个字典,它的键 ...

  3. 玩转VFS(二)

    关于VFS的第一篇中已经太长了 http://www.cnblogs.com/honpey/p/6348914.html 另起一篇: 1)如何在kernel里找到目前文件系统中的根目录: 2) 如何能 ...

  4. 深入理解Netscaler INat

    深入理解Netscaler INat http://blog.51cto.com/caojin/1898173 Netscaler的INat主要是用作基于目的地址的转换,将client访问的公网IP通 ...

  5. [洛谷P2384]最短路

    题目大意:给你一个图,要你求出其中1->n路径中乘积最小的一条路 题解:用$log_2$把乘法变成加法,然后记录每个点的前驱,最后求出答案 C++ Code: #include<cstdi ...

  6. CF#366 704D Captain America 上下界网络流

    CF上的题,就不放链接了,打开太慢,直接上题面吧: 平面上有n个点, 第 i 个点的坐标为 ($X_i ,Y_i$), 你需要把每个点染成红色或者蓝色, 染成红色的花费为 r , 染成蓝色的花费为 b ...

  7. [luoguP3644] [APIO2015]八邻旁之桥(权值线段树)

    传送门 首先如果起点终点都在同一侧可以直接处理,如果需要过桥答案再加1 对于k等于1的情况 桥的坐标为x的话,a和b为起点和终点坐标 $ans=\sum_{1}^{n} abs(a_{i}-x)+ab ...

  8. [Leetcode] count and say 计数和说

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  9. 解决perm size out of memeory的问题

    在idea中配置如下即可 -Xms1024m -Xmx1024m -XX:MaxNewSize=512m -XX:MaxPermSize=512m 如下图所示:

  10. Error in deleting blocks.

    2014-08-24 22:15:21,714 WARN org.apache.hadoop.hdfs.server.datanode.DataNode: Error processing datan ...