Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int long long
- using namespace std;
- const int N = 100005;
- int n,m;
- int cha[N],hang[N];
- int getroot(int u)
- {
- if (cha[u] != u) cha[u] = getroot(cha[u]);
- return cha[u];
- }
- void join (int u, int v)
- {
- u = getroot(u); v = getroot(v);
- if (u==v) return;
- if (hang[u] == hang[v]) hang[u]++;
- if (hang[u] < hang[v]) cha[u] = v;
- else cha[v]=u;
- }
- signed main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);cout.tie(0);
- freopen("DISJOINT.INP","r",stdin);
- freopen("DISJOINT.OUT","w",stdout);
- cin >> n >> m;
- for(int i=1; i<=n; ++i)
- {
- cha[i]=i;
- hang[i]=0;
- }
- for(int i=1; i<=m; ++i)
- {
- int type,u,v;
- cin >> type >> u >> v;
- if (type==1)
- {
- join(u,v);
- }
- else
- {
- if (getroot(u) == getroot(v)) cout << 1 << endl;
- else cout << 0 << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement