【POJ-2524】Ubiquitous Religions(並查集),poj-2524ubiquitous
並查集。
#include<cstdio>#include<cstring>using namespace std;const int maxn = 55555;int fa[maxn];int vis[maxn];int n,m,t;void init(){ for(int i = 0; i < n; i++) {fa[i] = i;} memset(vis,0,sizeof(vis));}int find_father(int u){ return fa[u] == u ? u : fa[u] = find_father(fa[u]);}int main(){ int Case = 1; while(scanf("%d%d",&n,&m)){ if(!m && !n) break; init(); for(int i = 0; i < m; i++){ int x,y; scanf("%d%d",&x,&y); int fx = find_father(x); int fy = find_father(y); fa[fx] = fy; } int cnt = 0; for(int i = 1; i <= n; i++){ int t = find_father(i); if(!vis[t]){ vis[t] = 1; cnt ++; } } printf("Case %d: %d\n",Case++,cnt); } return 0;}
北大acm題中有什題使用並查集來做的?
POJ 1611 The Suspects 最基礎的並查集
POJ 2524 Ubiquitous Religions 最基本的並查集
POJ 1182 食物鏈 並查集的拓展
注意: 只有一組資料;
要充分利用題意所給條件:有三類動物A,B,C,這三類動物的食物鏈
構成了有趣的環形。A吃B, B吃C,C吃A。也就是說:只有三個group
POJ 2492 A Bug's Life 並查集的拓展
法一:深度優先遍曆
每次遍曆記錄下該點是男還是女,只有:男-〉女,女-〉男滿足,否則,找到同性戀,結束程式。
法二:二分圖匹配
法三:並查集的拓展:和1182很像,只不過這裡就有兩組,而1182是三組,1611無限制
POJ 1861 Network == zju_1542 並查集+自訂排序+貪心求"最小產生樹"
答案不唯一,不過在ZOJ上用QSORT()和SORT()都能過,在POJ上只有SORT()才能過...
POJ 1703 Find them, Catch them 並查集的拓展
這個和POJ 2492 A Bug's Life很像,就是把代碼稍微修改了一下就AC了!
注意:And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. 就是說只有兩個組。
POJ 2236 Wireless Network 並查集的應用
需要注意的地方:1、並查集;2、N的範圍,可以等於1001;3、從N+1行開始,第一個輸入的可以是字串。
POJ 1988 Cube Stacking 並查集很好的應用
1、與 銀河英雄傳說==NOI2002 Galaxy一樣;2、增加了一個數組behind[x],記錄戰艦x在列中的相對位置;3、詳細解題報告見銀河英雄傳說。
JOJ 1905 Freckles == POJ 2560 最小產生樹
法一:Prim演算法;法二:並查集實現Kruskar演算法求最小產生樹
誰有資料結構的演算法跟POJ題目的對應關係
打星號的表示個人認為比較經典,或是演算法比較好的題目
1195 Mobile phones 樹狀數組
1455
1521 Entropy huffman
1703 Find them, Catch them 並查集
1785 Binary Search Heap Construction
1794 Castle Walls 逆序對
1961 Period KMP重複因子
1984* Navigation Nightmare 並查集+座標平移
1986* Distance Queries LCA
1988* Cube Stacking 並查集應用
1990* MooFest 線段樹
2010* Moo University - Financial Aid 最大堆-最小堆
2182 Lost Cows 線段樹
2183 Bovine Math Geniuses hash
2188 Cow Laundry 逆序對
2227 The Wedding Juicer 堆+floodfill
2236 Wireless Network 並查集
2266* Quadtree 遞迴構造四叉樹
2269* Friends 運算式
2270 Quadtree II or: Florida Jones strikes back 將2266反之
2299 Ultra-QuickSort 歸併排序
2352 Stars 樹狀數組
2395 Out of Hay 並查集
2482 Stars in Your Window 靜態2叉樹
2513 Colored Sticks 並查集
2524 Ubiquitous Religions 並查集
2528 Mayor's posters 線段樹
2567 Code the Tree
2750* Potted Flower 線段樹
2777 Count Color 線段樹
2796 Feel Good RMQ
2823 Sliding Window 堆或雙端隊列
2828 Buy Tickets 線段樹
2886* Who Gets the Most Candies? 線段樹
2892* Tunnel Warfare 樹狀數組
3214* Heap 後序遍曆,每個節點減去相應sub保證屬性,然後對遍曆結果求最長不下降序列
3253 Fence Repair huffman
3263 Tallest Cow 線段樹
3274* Gold Balanced Lineup hash
3277 City Horizon 線段樹
3320 Jessica's Reading Problem 隊列操作或最小堆
3321* Apple Tree 樹狀數組
3332 Parsing Real Numbers DFA
3344 Chessboard Dance 隊列類比
3349 Snowflake Snow Snowflakes hash(or 暴力)
3437 Tree Grafting dfs樹構造
3461 Oulipo KMP
3468 A Simple Problem with Integers 線段樹區間更新,懶操作
3631 Cuckoo Hashing 並查集
3667 Hotel 線段樹
3690 Constellations trie匹配
3695 Rectangles 矩陣切割...餘下全文>>