题意:
有n个学生,对他们进行询问,得到m组结果,每组结果表示这两个人信同一个宗教
求最大的宗教数目
解法:
并查集合并,每合并一次减1的数目
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;
///宏定义
const int INF = 990000000;
const int maxn = 50010 ;
const int MAXN = maxn;
///全局变量 和 函数
int max(int a, int b)
{
return a > b ? a : b;
}
int n, m;
int parent[maxn];
int find(int x)
{
if (parent[x] != x)
{
int root = find(parent[x]);
return parent[x] = root;
}
else
return x;
}
void union_set(int x, int y)
{
int r1 = find(x);
int r2 = find(y);
if (r1 != r2)
{
parent[r1] = r2;
}
}
int main()
{
///变量定义
int i, j;
int cases = 1;
while(scanf("%d %d", &n, &m) != EOF && (m != 0 && n != 0))
{
for (i = 0; i <= n; i++)
{
parent[i] = i;
}
int cnt = 0;
for (i = 0; i < m; i++)
{
int s1, s2;
scanf("%d %d", &s1, &s2);
int r1, r2;
r1 = find(s1);
r2 = find(s2);
if (r1 != r2)
{
cnt++;
union_set(s1, s2);
}
else
{
}
}
printf("Case %d: %d\n", cases++, n - cnt);
}
///结束
return 0;
}