自己不能使自己的父亲了,找个父亲,使自己永远是孩子。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <string>
using namespace std;
const int MAXN = 1000000;
int pre[MAXN * 2];
int n, m;
int finds(int x)
{
return x == pre[x] ? x : (pre[x] = finds(pre[x]));
}
void init()
{
for (int i = 0; i < n; i++)
{
pre[i] = i + n;
}
for (int i = n; i < n + n + m; i++)
{
pre[i] = i;
}
}
char ch[3];
int ans[MAXN];
void input()
{
int u, v, w, cases = 0;
while (scanf("%d %d", &n, &m) != EOF)
{
if (n == 0 && m == 0) break;
init();
int f = n + n;
for (int i = 0; i < m; i++)
{
scanf("%s", ch);
if (ch[0] == 'M')
{
scanf("%d %d", &u, &v);
int fa = finds(u), fb = finds(v);
if (fa != fb) pre[fa] = fb;
}
else
{
scanf("%d", &u);
pre[u] = f++;
}
}
for (int i = 0; i < n; i++)
{
ans[i] = finds(i);
}
sort(ans, ans + n);
int res = 1;
for (int i = 1; i < n; i++)
{
if (ans[i] != ans[i - 1]) res++;
}
cout << "Case #" << ++cases << ": " << res << endl;
}
}
int main()
{
input();
return 0;
}