Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("disjoint.in");
- ofstream fout("disjoint.out");
- const int NMAX=100005;
- int n,query,t[NMAX];
- inline void UNION(int x,int y)
- {
- t[y]=x;
- }
- inline int FIND(int x)
- {
- int rad,y;
- rad=x;
- while(t[rad])
- rad=t[rad];
- while(x!=rad)
- {
- y=t[x];
- t[x]=rad;
- x=y;
- }
- return rad;
- }
- inline void READ_SOLVE()
- {
- fin>>n>>query;
- while(query--)
- {
- int op,rad,rad1;
- fin>>op>>rad>>rad1;
- rad=FIND(rad);
- rad1=FIND(rad1);
- if(op==1 && rad!=rad1)
- UNION(rad,rad1);
- else if(op==2)
- {
- if(rad!=rad1)
- fout<<"NU\n";
- else fout<<"DA\n";
- }
- }
- }
- int main()
- {
- READ_SOLVE();
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment