This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ngthanhtrung23/ACM_Notebook_new
#define PROBLEM "https://judge.yosupo.jp/problem/unionfind" #include <bits/stdc++.h> using namespace std; #include "../DSU/DisjointSet.h" int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; DSU dsu(n); while (q--) { int typ, u, v; cin >> typ >> u >> v; if (typ == 0) dsu.merge(u, v); else { cout << (dsu.getRoot(u) == dsu.getRoot(v) ? 1 : 0) << '\n'; } } return 0; }
#line 1 "DataStructure/test/dsu.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/unionfind" #include <bits/stdc++.h> using namespace std; #line 1 "DataStructure/DSU/DisjointSet.h" // DisjointSet {{{ struct DSU { vector<int> lab; DSU(int n) : lab(n+1, -1) {} int getRoot(int u) { if (lab[u] < 0) return u; return lab[u] = getRoot(lab[u]); } bool merge(int u, int v) { u = getRoot(u); v = getRoot(v); if (u == v) return false; if (lab[u] > lab[v]) swap(u, v); lab[u] += lab[v]; lab[v] = u; return true; } bool same_component(int u, int v) { return getRoot(u) == getRoot(v); } int component_size(int u) { return -lab[getRoot(u)]; } }; // }}} #line 7 "DataStructure/test/dsu.test.cpp" int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; DSU dsu(n); while (q--) { int typ, u, v; cin >> typ >> u >> v; if (typ == 0) dsu.merge(u, v); else { cout << (dsu.getRoot(u) == dsu.getRoot(v) ? 1 : 0) << '\n'; } } return 0; }