fork download
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <vector>
  5. #include <set>
  6. #include <numeric>
  7. #include <map>
  8. #include <unordered_map>
  9. using namespace std;
  10. #define all(a) a.begin(), a.end()
  11. #define ll long long
  12. #define fo(i,n) for (long long i = 0; i < n; i++)
  13.  
  14. int main()
  15. {
  16. ll n,q,op,a,b;
  17. cin >> n >> q;
  18. vector<vector<ll>> nest(n,vector<ll>(1));
  19. map<ll,ll> locations;
  20. fo(j,n)
  21. {
  22. nest[j][0] = j;
  23. locations[j] = j;
  24. }
  25. for (int j = 0; j < q; j++)
  26. {
  27. cin >> op;
  28. if (op == 1)
  29. {
  30. cin >> a >> b;
  31. ll spot = locations[a-1];
  32. nest[spot].erase(find(all(nest[spot]),a-1));
  33. nest[b-1].push_back(a-1);
  34. locations[a-1] = b-1;
  35. }
  36. if (op == 2)
  37. {
  38. cin >> a >> b;
  39. for (ll item : nest[a-1])
  40. {
  41. locations[item] = b-1;
  42. }
  43. for (ll item : nest[b-1])
  44. {
  45. locations[item] = a-1;
  46. }
  47. nest[a-1].swap(nest[b-1]);
  48. }
  49. if (op == 3)
  50. {
  51. cin >> a;
  52. cout << locations[a-1]+1 << '\n';
  53. }
  54. }
  55.  
  56. }
Success #stdin #stdout 0.01s 5292KB
stdin
30 15
3 3
2 8 30
2 12 15
2 2 17
1 19 1
2 7 30
3 12
3 8
2 25 26
1 13 10
1 16 10
2 16 29
2 1 21
2 6 11
1 21 8
stdout
3
15
7