Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, c[501];
  6.  
  7. void Init()
  8. {
  9. for(int i = 1; i <= n; i++)
  10. c[i] = i;
  11. }
  12.  
  13. void Update(int x, int y)
  14. {
  15. int a = c[x], b = c[y];
  16. if(a > b)
  17. swap(a,b);
  18. for(int i = 1; i <= n; i++)
  19. if(c[i] == b)
  20. c[i] = a;
  21. }
  22.  
  23. bool Query(int x, int y)
  24. {
  25. return c[x] == c[y];
  26. }
  27.  
  28. int main()
  29. {
  30. int m;
  31. cin >> n >> m;
  32. Init();
  33. for(int i = 1; i <= m; i++)
  34. {
  35. int x, y, op;
  36. cin >> op >> x >> y;
  37. if(op == 1)
  38. Update(x,y);
  39. else
  40. {
  41. if(Query(x,y))
  42. cout << "DA\n";
  43. else
  44. cout << "NU\n";
  45. }
  46. }
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement