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
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include <math.h> using namespace std; typedef unsigned long long LL; const int N = 150005; struct Edge { LL u,v; LL w; } edge[N]; LL father[N]; LL cnt[N]; LL MAX,MIN; LL _find(LL x) { if(x!=father[x]){ father[x] = _find(father[x]); } return father[x]; } void init(int n) { for(int i=1; i<=n; i++) { father[i] = i; cnt[i] = 1; } } void Union(LL a,LL b,LL w,int flag) { LL x = _find(a); LL y = _find(b); if(flag) { MAX+=cnt[x]*cnt[y]*w; } else { MIN+=cnt[x]*cnt[y]*w; } father[x] = y; cnt[y]+=cnt[x]; } int cmp(Edge a,Edge b) { return a.w<b.w; } int main() { int n; int t = 1; while(scanf("%d",&n)!=EOF) { init(n); for(int i=1; i<n; i++) { scanf("%I64u%I64u%I64u",&edge[i].u,&edge[i].v,&edge[i].w); } MAX = 0,MIN = 0; sort(edge+1,edge+n,cmp); for(int i=1; i<n; i++) { Union(edge[i].u,edge[i].v,edge[i].w,1); } init(n); for(int i=n-1; i>=1; i--) { Union(edge[i].u,edge[i].v,edge[i].w,0); } printf("Case #%d: ",t++); printf("%I64u\n",MAX-MIN); } return 0; }