【POJ】The Suspects(裸並查集),pojthesuspects

來源:互聯網
上載者:User

【POJ】The Suspects(裸並查集),pojthesuspects

並查集的模板題,為了避免麻煩,合并的時候根節點大的合并到小的結點。

#include<cstdio>#include<algorithm>using namespace std;const int maxn = 33333;int fa[maxn];int num[maxn];int n,m,t;void init(){    for(int i = 0; i < n; i++) {fa[i] = i; num[i] = 1;}}int find_father(int u){    return fa[u] == u ? u : fa[u] = find_father(fa[u]);}int main(){    while(scanf("%d%d",&n,&m)){        if(!m && !n) break;        init();        for(int i = 0; i < m; i++){            scanf("%d",&t);            int x,y;            for(int j = 0; j < t; j++){                scanf("%d",&y);                if(!j) x = y;                else{                    int fy = find_father(y);                    int fx = find_father(x);                    if(fy > fx) swap(fy,fx); //為了避免麻煩,統一讓小的根合并到大的根                    if(fy != fx){                        fa[fy] = fx;                        num[fx] += num[fy];                    }                }            }        }        t = find_father(0);        printf("%d\n",num[t]);    }    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演算法求最小產生樹
 
.One of the suspects________down Zhongshan Road at 9:00 last night. A.was seen to run B.wa

答案: B
原因:1. 用 was 而不用 were, 是因為這個句子的主語是 one of the suspects, 嫌疑人中的一個人,是單數。所以,首先排除了C和D。然後看是 to run ,還是 running。2. 首先這個句子中給出了非常具體的時間點,所以這就暗示了,被看到正在做run這個動作,即應該是進行時。其次,句中已經說了是被看到,換做任何一個人,在非常精確的時間點,看到什麼,肯定是這個瞬間,人物正在做某事,同樣,是進行時。O了,不知道說明白沒。
希望對你有協助。。。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.