POJ2524 Ubiquitous Religions

来源:互联网 发布:考上清华美院知乎 编辑:程序博客网 时间:2024/05/25 23:59
/*并查集*/#include <iostream>using namespace std;const int MAXN = 50005;int father[MAXN];int n, m, cas = 0, sum;void init(){for (int i = 0; i < n; ++i) {father[i] = i;}return;}int getfather(int x){if (x != father[x])father[x] = getfather(father[x]);return father[x];}void merge(int x, int y){int fx = getfather(x);int fy = getfather(y);if (fx != fy) {father[fy] = fx;--sum;}}int main(){while (cin >> n >> m && (n + m)) {init();sum = n;int x, y;for (int i = 0; i < m; ++i) {cin >> x >> y;merge(x, y);}cout << "Case " << ++cas << ": " << sum << endl;}return 0;}

0 0
原创粉丝点击