Advertisement
a53

entries

a53
Nov 24th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin ("entries.in");
  6. ofstream fout ("entries.out");
  7.  
  8. map<int, int> M;
  9. map<int, int> :: iterator it;
  10.  
  11. int t[10005], n;
  12.  
  13. int Find(int x)
  14. {
  15. int z,y;
  16. y = x;
  17. while (t[x]!=0)
  18. x = t[x];
  19.  
  20. while (t[y]!=0)
  21. {
  22. z = t[y];
  23. t[y] = x;
  24. y = z;
  25. }
  26.  
  27. return x;
  28. }
  29.  
  30. inline void Union(int x, int y)
  31. {
  32. t[y] = x;
  33. }
  34.  
  35. int main ()
  36. {
  37. int T, x, y, op, k;
  38. fin >> T;
  39. while (T--)
  40. {
  41. fin >> x >> y >> op;
  42. k = 0;
  43. it = M.find(x);
  44. if (it == M.end()) {k = 1; n++; M[x] = n;}
  45. it = M.find(y);
  46. if (it == M.end()) {k = 1; n++; M[y] = n;}
  47.  
  48. x = M[x];
  49. y = M[y];
  50. x = Find(x);
  51. y = Find(y);
  52. if (op == 1)
  53. {
  54. if (x != y) Union(x, y);
  55. }
  56. else
  57. {
  58. if (k == 1) fout << "0\n";
  59. else if (x == y) fout << "1\n";
  60. else fout << "0\n";
  61. }
  62.  
  63. }
  64. fin.close();
  65. fout.close();
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement