并查集(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 N1 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 n1 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
31 2 12 3 251 2 22 3 52 4 73 5 4
 

Sample Output
Case #1: 1Case #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;
 

Source
Valentine's Day Round


题意:求树上任意两点之间路径上最大值减去最小值的差得和

思路: sigma(maxVal[i]minVal[i])=sigma(maxVal)sigma(minVal);所以我们分别求所有两点路径上的最大值的和,还有最小值的和。再相减就可以了。求最大值的和的方法用带权并查集,把边按权值从小到大排序,一条边一条边的算,当我们算第i条边的时候权值为wi,两点是ui,vi,前面加入的边权值一定是小于等于当前wi的,假设与ui连通的点有a个,与vi连通的点有b个,那么在a个中选一个,在b个中选一个,这两个点的路径上最大值一定是wi,一共有ab个选法,爱情经验值为abwi。求最小值的和的方法类似。


#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