这是ac的代码
#include <bits/stdc++.h>
using namespace std;
const int N = 50010;
int p[N];
int n,m;
int find(int x)
{
if (p[x] != x)p[x] = find(p[x]);
return p[x];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
while (cin >> n >> m , n && m)
{
int res = n;
for (int i = 1;i <= n;i ++)p[i] = i;
while (m --)
{
int a,b;
cin >> a >> b;
int px = find(a),py = find(b);
if (px == py)continue;
else
{
p[find(a)] = find(b);
res --;
}
}
printf("Case %d: %d\n",t,res);
t ++;
}
return 0;
}
但是我在主函数中的第二层while循环里面将a,b的输入改为scanf就会re