当前位置: 代码迷 >> 综合 >> POJ 2524 Ubiquitous Religions - (并查集)
  详细解决方案

POJ 2524 Ubiquitous Religions - (并查集)

热度:94   发布时间:2023-12-21 12:29:10.0

题目链接:http://poj.org/problem?id=2524

#include <stdio.h> 
#include <vector> 
#include <algorithm>
using namespace std;//POJ 2524 Ubiquitous Religionsinline int getFather(int *p, int x)
{if (p[x] == x)return x;p[x] = getFather(p, p[x]);return p[x];
}inline void un(int *p, int x, int y)
{int fx = getFather(p, x);int fy = getFather(p, y);p[fx] = fy;
}inline bool judge(int *p, int x, int y)
{return getFather(p, x) == getFather(p, y);
}int main()
{int count = 0;int n, m;while (~scanf("%d%d", &n, &m)){count++;if (n == 0)break;int *p = new int[n];for (int i = 0; i < n; ++i)p[i] = i;int A, B;for (int i = 0; i < m; ++i){scanf("%d%d", &A, &B);A--; B--;un(p, A, B);}vector<int> diff_religion;for (int i = 0; i < n; ++i)diff_religion.push_back(getFather(p, i));std::sort(diff_religion.begin(), diff_religion.end());vector<int>::iterator it = std::unique(diff_religion.begin(), diff_religion.end());printf("Case %d: %d\n", count, it - diff_religion.begin());}system("pause");return 0;
}