日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

發布時間:2025/1/21 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

傳送門

The Experience of Love

Time Limit: 4000/2000 MS (Java/Others)????Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 221????Accepted Submission(s): 91


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;

?

Source Valentine's Day Round

?

Recommend hujie???|???We have carefully selected several similar problems for you:??5177?5175?5173?5170?5169?

轉一發官方題解:http://bestcoder.hdu.edu.cn/

?

?

題意:給一棵樹,求任意{兩點路徑上的最大邊權值-最小邊權值}的總和。 解法:sigma(maxVal[i]?minVal[i])=sigma(maxVal)?sigma(minVal) ;所以我們分別求所有兩點路徑上的最大值的和,還有最小值的和。再相減就可以了。求最大值的和的方法用帶權并查集,把邊按權值從小到大排序,一條邊一條邊的算,當我們算第i 條邊的時候權值為wi ,兩點是ui,vi ,前面加入的邊權值一定是小于等于當前wi 的,假設與ui 連通的點有a 個,與vi 連通的點有b 個,那么在a 個中選一個,在b 個中選一個,這兩個點的路徑上最大值一定是wi ,一共有a?b 個選法,愛情經驗值為a?b?wi 。求最小值的和的方法類似。 槽點: 一:這題做數據的時候突然想到的把數據范圍設在 unsigned long long 范圍內,要爆 long long,這樣選手在wa了之后可能心態不好找不到這個槽點,當是鍛煉大家的心態和出現wa時的找錯能力了,把這放在pretest..很良心的。 二,并查集的時候,用是遞歸的需要擴棧,一般上10w 的遞歸都需要,所以看見有幾個FST在棧溢出的,好桑心。

?

129575652015-02-16 11:18:47Accepted5176842MS6820K2033 BG++czy

?

1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<string> 12 13 #define N 150005 14 #define M 10005 15 //#define mod 10000007 16 //#define p 10000007 17 #define mod2 1000000000 18 #define ll long long 19 #define ull unsigned long long 20 #define LL long long 21 #define eps 1e-6 22 //#define inf 2147483647 23 #define maxi(a,b) (a)>(b)? (a) : (b) 24 #define mini(a,b) (a)<(b)? (a) : (b) 25 26 using namespace std; 27 28 int n; 29 int f[N]; 30 ull cou[N]; 31 ull suma,sumi; 32 int cnt; 33 34 typedef struct 35 { 36 int a; 37 int b; 38 ull c; 39 }PP; 40 41 PP p[N]; 42 43 bool cmp(PP x,PP y) 44 { 45 return x.c<y.c; 46 } 47 48 int find(int x) 49 { 50 int fa; 51 if(x!=f[x]) 52 { 53 fa=find(f[x]); 54 f[x]=fa; 55 } 56 return f[x]; 57 } 58 59 void merge(int x,int y) 60 { 61 int a,b; 62 a=find(x); 63 b=find(y); 64 if(a==b) return; 65 f[b]=a; 66 cou[a]=cou[a]+cou[b]; 67 } 68 69 void ini() 70 { 71 suma=sumi=0; 72 int i; 73 for(i=0;i<=n;i++){ 74 f[i]=i; 75 cou[i]=1; 76 } 77 for(i=1;i<n;i++){ 78 scanf("%d%d%I64u",&p[i].a,&p[i].b,&p[i].c); 79 } 80 sort(p+1,p+n,cmp); 81 } 82 83 void solve() 84 { 85 int i; 86 int aa,bb; 87 for(i=1;i<n;i++){ 88 aa=find(p[i].a); 89 bb=find(p[i].b); 90 suma+=cou[aa]*cou[bb]*p[i].c; 91 merge(p[i].a,p[i].b); 92 } 93 for(i=0;i<=n;i++){ 94 f[i]=i; 95 cou[i]=1; 96 } 97 for(i=n-1;i>=1;i--){ 98 aa=find(p[i].a); 99 bb=find(p[i].b); 100 sumi+=cou[aa]*cou[bb]*p[i].c; 101 merge(p[i].a,p[i].b); 102 } 103 } 104 105 void out() 106 { 107 printf("Case #%d: %I64u\n",cnt,suma-sumi); 108 cnt++; 109 } 110 111 int main() 112 { 113 cnt=1; 114 //freopen("data.in","r",stdin); 115 //freopen("data.out","w",stdout); 116 //scanf("%d",&T); 117 //for(int ccnt=1;ccnt<=T;ccnt++) 118 //while(T--) 119 //scanf("%d%d",&n,&m); 120 while(scanf("%d",&n)!=EOF) 121 { 122 ini(); 123 solve(); 124 out(); 125 } 126 return 0; 127 }

?

轉載于:https://www.cnblogs.com/njczy2010/p/4293905.html

總結

以上是生活随笔為你收集整理的Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。