POJ2524 - Ubiquitous Religions (并查集的基础应用)
来源:互联网 发布:ml域名s是哪的 编辑:程序博客网 时间:2024/05/27 09:44
题目链接
思路
并查集,询问有多少个集合。
代码
#include <iostream>#include <cstring>#include <cstdio>#include <set>using namespace std;const int maxn = 50100;int par[maxn];int find(int a){ if(par[a]<0) return a; else return par[a] = find(par[a]);}void merge(int a, int b){ int pa = find(a); int pb = find(b); if(pa!=pb) par[pb] = pa;}bool query(int a, int b){ return find(a) == find(b);}int main(){ int n, m; int a, b; int tt = 1; while(scanf("%d%d", &n, &m) && n) { memset(par, -1, sizeof(par)); for(int i=0; i<m; i++) { scanf("%d%d", &a, &b); merge(a, b); } set<int> ans; for(int i=1; i<=n; i++) { ans.insert(find(i)); } printf("Case %d: %d\n", tt++, ans.size()); } return 0;}
0 0
- POJ2524 - Ubiquitous Religions (并查集的基础应用)
- POJ2524--Ubiquitous Religions(并查集)
- POJ2524——Ubiquitous Religions 并查集基础
- POJ2524 Ubiquitous Religions 并查集
- poj2524 Ubiquitous Religions 并查集入门
- POJ2524 Ubiquitous Religions 并查集
- 【并查集】:poj2524,Ubiquitous Religions
- 并查集-poj2524-Ubiquitous Religions
- POJ2524 Ubiquitous Religions(并查集)
- POJ2524 Ubiquitous Religions 并查集
- POJ2524 Ubiquitous Religions(简单并查集)
- POJ2524 Ubiquitous Religions (并查集)
- POJ2524 Ubiquitous Religions (并查集)
- POJ2524 Ubiquitous Religions (并查集)
- POJ2524——Ubiquitous Religions(并查集)
- poj2524——Ubiquitous Religions(并查集)
- POJ2524 Ubiquitous Religions 暨并查集入门
- POJ2524 Ubiquitous Religions —— 并查集
- 【snmp+ssh+linux】实现自动化运维与监控系统--持续更新中
- POJ1611 - The Suspects (并查集的应用)
- 1加到100的结果
- qt之hello world
- C++ String
- POJ2524 - Ubiquitous Religions (并查集的基础应用)
- serialVersionUID作用
- UESTC 84 Binary Operations (dp+二进制枚举)
- 【线性代数公开课MIT Linear Algebra】 第六课 AX=b与列空间、零空间
- POJ1861 - Network (并查集,最小生成树)
- 11个可能不知道的Python库
- JavaWeb三层结构实例
- netty的对象的生命周期管理——引用计数
- HDU 4020 Ads Proposal