我是靠谱客的博主 舒适小馒头,这篇文章主要介绍POJ 1611 The Suspects(并查集),现在分享给大家,希望可以做个参考。

并查集

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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("%dn",ans); } return 0; }


转载于:https://www.cnblogs.com/jzssuanfa/p/6772796.html

最后

以上就是舒适小馒头最近收集整理的关于POJ 1611 The Suspects(并查集)的全部内容,更多相关POJ内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部