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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poj 1611 The Suspects // hoj 1564 The Suspects 并查集

發布時間:2023/12/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 1611 The Suspects // hoj 1564 The Suspects 并查集 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/*

題目:

?? 是說學生0懷疑有SARS病,跟他接觸過的俱樂部的所有人以及他接觸過的人再與別人接觸,

?? 都有可能有SARS病,要你求出給出的所有俱樂部人的名單,要你求出所有的嫌疑犯。。。

分析:

?? 用并查集的方法做,具體實現是先定義n個森林,然后再把同一的人所在的森林合并在一起,

?? 而并查集的改善方法有兩種,我的使用的是壓縮路徑的方法

*/

#include <iostream>

#include <cstring>

#include <cstdio>

using namespace std;

const int X = 30010;

int parent[X],a[X],Rank[X],m,n,t;

int find_set(int x)?????????????? //路徑壓縮

{

?? if(x!=parent[x])

????? parent[x] = find_set(parent[x]);

?? return parent[x];

}

void union_set(int i,int j)

{

?? i = find_set(i);

?? j = find_set(j);

?? if(i!=j)

????? if(Rank[i]>Rank[j])???????? //等級越高,所在的樹的高度越高

???????? parent[j] = i;

????? else

????? {

???????? parent[i] = j;

???????? if(Rank[i]==Rank[j])

??????????? Rank[j]++;

????? }

}

int main()

{

?? freopen("sum.in","r",stdin);

?? freopen("sum.out","w",stdout);

?? int x,pre;

?? while(cin>>n>>m,n||m)

?? {

????? for(int i=0;i<n;i++)

????? {

???????? parent[i] = i;

???????? Rank[i] = 0;

????? }

????? for(int i=0;i<m;i++)

????? {

???????? scanf("%d",&t);

???????? if(t)????????????? //只需找出俱樂部的第一個人與其他人并起來即可

??????????? scanf("%d",&pre);

???????? for(int i=0;i<t-1;i++)

???????? {

??????????? scanf("%d",&x);

??????????? union_set(pre,x);

???????? }

????? }

????? pre = find_set(0);??? //找到0所在的樹的根節點編號,再用它與其他的人相比較

????? int ans = 1;

????? for(int i=1;i<n;i++)

???????? if(pre==find_set(i))

??????????? ans++;

????? cout<<ans<<endl;

?? }

?

?? return 0;

}

?

?

轉載于:https://www.cnblogs.com/yejinru/archive/2012/03/22/2412068.html

總結

以上是生活随笔為你收集整理的poj 1611 The Suspects // hoj 1564 The Suspects 并查集的全部內容,希望文章能夠幫你解決所遇到的問題。

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