/*并查集*/
#include <iostream>
#include <string>
using namespace std;
const int MAXN = 30005;
int father[MAXN], Rank[MAXN], up[MAXN];
int p;
void init()
{
for (int i = 0; i < MAXN; ++i) {
father[i] = i;
Rank[i] = 1;
up[i] = 0;
}
}
int getfather(int x)
{
int num;
if (father[x] != x) {
num = father[x];
father[x] = getfather(father[x]);
up[x] += up[num];
}
return father[x];
}
void merge(int x, int y)
{
int a = getfather(x);
int b = getfather(y);
father[b] = a;
up[b] = Rank[a];
Rank[a] += Rank[b];
}
int main()
{
while (cin >> p) {
init();
int x, y, z;
string s;
for (int k = 0; k < p; ++k) {
cin >> s;
if (s == "M") {
cin >> x >> y;
merge(x, y);
}
else if (s == "C") {
cin >> z;
int tmp = getfather(z);
cout << Rank[tmp] - up[z] - 1 << endl;
}
}
}
return 0;
}
POJ1988 Cube Stacking
最新推荐文章于 2022-03-12 13:41:10 发布