题目链接:codeforces 1620E
题目思路:
倒序遍历每次的操作,对于操作
2
2
2,相当于把 x
加入到代表元是 y
的集合。
参考代码:
#include<iostream>
using namespace std;
const int N = 5e5 + 10;
int a[N], x[N], y[N], fa[N];
int main () {
int q, n = 0; cin >> q;
for (int i = 1; i <= q; i++) {
int opt; cin >> opt >> x[i];
if (opt == 2) cin >> y[i];
}
for (int i = 1; i <= 500000; i++) fa[i] = i;
for (int i = q; i >= 1; i--) {
if (!y[i]) a[++n] = fa[x[i]];
else fa[x[i]] = fa[y[i]];
}
while (n) cout << a[n--] << ' ';
return 0;
}