并查集(hdu5176)
来源:互联网 发布:文案工作 知乎 编辑:程序博客网 时间:2024/05/16 06:26
The Experience of Love
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 340 Accepted Submission(s): 130
Problem Description
A girl named Gorwin and a boy named Vivin is a couple. They arrived at a country named LOVE. The country consisting ofN 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.
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 integerN , 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
For each test case the first line is a integer
[Technical Specification]
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
31 2 12 3 251 2 22 3 52 4 73 5 4
Sample Output
Case #1: 1Case #2: 17Hinthuge 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;
Source
Valentine's Day Round
题意:求树上任意两点之间路径上最大值减去最小值的差得和
思路:
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>#pragma comment(linker,"/STACK:1024000000,1024000000")using namespace std;const int maxn=150010;typedef unsigned long long LL;int N;int head[maxn];struct node{ int u,v,c; node(){} node(int ui,int vi,int ci):u(ui),v(vi),c(ci){} bool operator < (const node &a)const { return c<a.c; }}edge[maxn];int pra[maxn],cnt[maxn];void init(){ for(int i=0;i<=N;i++) pra[i]=i,cnt[i]=1;}int find(int x){ if(x==pra[x])return x; return pra[x]=find(pra[x]);}int main(){ int cas=1; while(scanf("%d",&N)!=EOF) { int u,v,c; for(int i=1;i<N;i++) { scanf("%d%d%d",&u,&v,&c); edge[i]=node(u,v,c); } init(); sort(edge+1,edge+N); LL maxsum=0; for(int i=1;i<N;i++) { int fa=find(edge[i].u); int fb=find(edge[i].v); if(fa!=fb) { maxsum+=(LL)cnt[fa]*cnt[fb]*edge[i].c; pra[fb]=fa; cnt[fa]+=cnt[fb]; } } init(); reverse(edge+1,edge+N); LL minsum=0; for(int i=1;i<N;i++) { int fa=find(edge[i].u); int fb=find(edge[i].v); if(fa!=fb) { minsum+=(LL)cnt[fa]*cnt[fb]*edge[i].c; pra[fb]=fa; cnt[fa]+=cnt[fb]; } } printf("Case #%d: ",cas++); cout<<maxsum-minsum<<endl; } return 0;}
0 0
- 并查集(hdu5176)
- 【并查集】hdu5176
- hdu5176 The Experience of Love[树 并查集]
- 并查集(集并查)
- 并查集(含关系并查集)
- 【并查集】食物链(关系并查集)
- 【并查集】并查集详解(转)
- 分组并查集(种类并查集)
- 并查集, 畅通工程(简单并查集)
- [数据结构]并查集水体POJ2236(并查集)
- 并查集(disjoint_set)
- 并查集(模板)
- 并查集(亲戚)
- POJ2524(并查集)
- POJ_2236(并查集)
- poj2492(并查集)
- HDU1272(并查集)
- 随笔(并查集)
- DP入门50题(5)——NYoj 17 单调递增最长子序列
- Binary Tree Preorder Traversal
- 浅谈Handler
- 1010. Radix (25)
- CF 457A(Golden System-特殊的进制)
- 并查集(hdu5176)
- 程序员必读书单
- "this whole ARM thing is a f*cking pain in the ass"
- Python 开发者应该知道的 7 个开发库(转)
- HDU 2807解题报告
- shell 脚本编程
- Swift - 文本输入框(UITextField)的用法
- Flex之旅:第一部分:flex必备基础知识积累(9)---DateField, DateChooser显示汉字的星期和月份
- Qt 视频播放