CodeForces - 566D Restructuring Company——并查集
#include using namespace std;const int maxn = 200000 + 10;int n, q, par[maxn], next[maxn];int seek(int x) { return par[x] == x ? x : par[x] = seek(par[x]); }int main() { scanf("%d %d", &n, &q); for (int i = 0; i <= n; i++) par[i] = i, next[i] = i + 1; while (q--) { int op, x, y; scanf("%d %d %d", &op, &x, &y); if (op == 1) par[seek(x)] = seek(y); else if (op == 2) { while (x < y) { par[seek(x)] = seek(y); int t = next[x]; next[x] = next[y]; x = t; } } else { puts(seek(x) == seek(y) ? "YES" : "NO"); } }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~