POJ1988 Cube Stacking

来源:互联网 发布:淘宝店铺收藏的链接 编辑:程序博客网 时间:2024/06/07 10:27
/*并查集*/#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;}

0 0
原创粉丝点击