POJ 1611 The Suspects(並查集),pojsuspects

來源:互聯網
上載者:User

POJ 1611 The Suspects(並查集),pojsuspects

並查集

#include<bits/stdc++.h>using namespace std;int f[30020];int find(int x){    if(f[x]!=x)        f[x]=find(f[x]);    return f[x];}void make(int a,int b){    int f1=find(a);    int f2=find(b);    if(f1!=f2)        f[f2]=f1;}int main(){    int n,m,a,b,k;    int kase=1;    while(scanf("%d%d",&n,&m)==2)    {        if(n==0&&m==0)            break;        for(int i=0;i<=n;i++)            f[i]=i;        for(int i=1;i<=m;i++)        {            scanf("%d%d",&k,&a);            for(int i=1;i<=k-1;i++)            {                scanf("%d",&b);                make(a,b);            }        }        int ans=0;        for(int i=0;i<=n;i++)        {            if(find(i)==find(0))                ans++;        }        printf("%d\n",ans);    }    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演算法求最小產生樹
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.