tanasaradu

Untitled

Oct 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("disjoint.in");
  4. ofstream fout("disjoint.out");
  5. const int NMAX=100005;
  6. int n,query,t[NMAX];
  7. inline void UNION(int x,int y)
  8. {
  9. t[y]=x;
  10. }
  11. inline int FIND(int x)
  12. {
  13. int rad,y;
  14. rad=x;
  15. while(t[rad])
  16. rad=t[rad];
  17. while(x!=rad)
  18. {
  19. y=t[x];
  20. t[x]=rad;
  21. x=y;
  22. }
  23. return rad;
  24. }
  25. inline void READ_SOLVE()
  26. {
  27. fin>>n>>query;
  28. while(query--)
  29. {
  30. int op,rad,rad1;
  31. fin>>op>>rad>>rad1;
  32. rad=FIND(rad);
  33. rad1=FIND(rad1);
  34. if(op==1 && rad!=rad1)
  35. UNION(rad,rad1);
  36. else if(op==2)
  37. {
  38. if(rad!=rad1)
  39. fout<<"NU\n";
  40. else fout<<"DA\n";
  41. }
  42. }
  43. }
  44. int main()
  45. {
  46. READ_SOLVE();
  47. fin.close();
  48. fout.close();
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment