HDU 3635 Dragon Balls(帶權並查集),hdu3635
題目地址:HDU 3635
加權並查集水題。
用num數組維護該城市有多少龍珠,用times數組維護每個龍珠運輸了多少次。num數組在合并的時候維護。times數組由於每個都不一樣,所以要在找根的時候遞迴來全部維護。
最終,x龍珠所在的城市就是x節點所在的根,x結點所在的跟的num數組值是該城市的龍珠數。times[x]為該龍珠運輸了多少次。
代碼如下:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longint bin[20000], num[20000], times[20000];int find1(int x){ int y; if(bin[x]!=x) { y=bin[x]; bin[x]=find1(bin[x]); times[x]+=times[y]; } return bin[x];}void join(int x, int y){ int f1=find1(x); int f2=find1(y); if(f1!=f2) { bin[f1]=f2; num[f2]+=num[f1]; times[f1]++; }}int main(){ int t, nn=0, n, m, i, a, b, f; char c; scanf("%d",&t); while(t--) { nn++; scanf("%d%d",&n,&m); for(i=1; i<=n; i++) { bin[i]=i; num[i]=1; times[i]=0; } printf("Case %d:\n",nn); while(m--) { getchar(); scanf("%c",&c); if(c=='T') { scanf("%d%d",&a,&b); join(a,b); } else { scanf("%d",&a); f=find1(a); printf("%d %d %d\n",f,num[f],times[a]); } } } return 0;}
HDU3635 Dragon Balls 那個大俠救救我吧,幫我看看代碼 為何總是Wrong
Sfind中return ancex;的ancex該為ancestor[x];
小弟想知道杭電 hdu上那些題目是只考察並查集的,菜鳥我想練練,希望最好可以把大概難易程度列出謝了
把我知道的都列出來了。。
簡單並查集
1213 How Many Tables
1232 暢通工程
(杭電簡單的並查集不是很多)
簡單最小產生樹
1233 還是暢通工程
1863 暢通工程
1874 暢通工程再續
1875 繼續暢通工程
1162 Eddy's picture
1102 Constructing Roads
1301 Jungle Roads
(可以通過最小產生樹練習並查集)
稍微有點難度並查集
1272 小希的迷宮
1325 Is It A Tree?
1856 More is better
有點難度的並查集
1116Play on Words
1829A Bug's Life
1198Farm Irrigation
3635Dragon Balls
2473Junk-Mail Filter
3172Virtual Friends
3047Zjnu Stadium
3038How Many Answers Are Wrong
1558Segment set
1598find the most comfortable road
3461Code Lock
2818Building Block
3367Pseudoforest
3234Exclusive-OR