fork download
  1. /*
  2. * Author: Geeza
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. #define ld long double
  8. #define ll long long
  9. #define pb push_back
  10. #define fin(a, n) for(int i = a; i < n; i++)
  11. #define fjn(a, n) for(int j = a; j < n; j++)
  12. #define all(a) a.begin(),a.end()
  13. #define allr(a) a.rbegin(),a.rend()
  14. #define FAST ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
  15.  
  16. using namespace std;
  17.  
  18. const double PI = acos(-1);
  19. const int N = 1e5+20;
  20. const ll oo = 0x3f3f3f3f3f3f3f3f;
  21. const int MOD = 1000000007, inf = 1e6;
  22.  
  23. string di[] = {"D","L", "U", "R", "UL", "UR", "DL", "DR"};
  24. int dx[] = {+1, +0, +0, -1, -1, -1, +1, +1};
  25. int dy[] = {+0, -1, +1, +0, -1, +1, -1, +1};
  26. char dc[] = {'D', 'L', 'R', 'U'};
  27.  
  28. struct DSU {
  29. vector<ll> parent, size, black;
  30. map<ll, int> color;
  31. DSU(ll n) : parent(n + 1), size(n + 1, 1), black(n+1, 0) {
  32. iota(parent.begin(), parent.end(), 0);
  33. for (ll x = 1; x <= n+1; x++)
  34. color[x] = 0;
  35. }
  36.  
  37. ll find(ll x) {
  38. if (x == parent[x]) return x;
  39. return parent[x] = find(parent[x]);
  40. }
  41.  
  42. bool unite(ll x, ll y) {
  43. x = find(x);
  44. y = find(y);
  45. if (x == y) return false;
  46. if (size[x] < size[y]) swap(x, y);
  47. black[x] += black[y];
  48. parent[y] = x;
  49. size[x] += size[y];
  50. return true;
  51. }
  52. };
  53.  
  54. void solve() {
  55. ll n, q; cin >> n >> q;
  56. DSU dsu(n);
  57.  
  58. fin(0, q) {
  59. int t; cin >> t;
  60. if (t == 1) {
  61. ll u, v; cin >> u >> v;
  62. dsu.unite(u, v);
  63. }
  64. else {
  65. ll u; cin >> u;
  66. ll leader = dsu.find(u);
  67. if (t == 2) {
  68. if (dsu.color[u] == 0) {
  69. if (leader != u) dsu.black[leader]++;
  70. dsu.black[u]++;
  71. }
  72. else {
  73. if (leader != u) dsu.black[leader]--;
  74. dsu.black[u]--;
  75. }
  76. dsu.color[u] ^= 1;
  77. }
  78. else {
  79. if (dsu.black[leader] > 0 || dsu.black[u] > 0) cout << "Yes\n";
  80. else cout << "No\n";
  81. }
  82. }
  83. }
  84. }
  85.  
  86. int main() {
  87. FAST;
  88. #ifndef ONLINE_JUDGE
  89. freopen("input.txt","r",stdin);
  90. freopen("output.txt","w",stdout);
  91. #endif
  92. int tt = 1; //cin >> tt;
  93. while(tt--){
  94. solve();
  95. }
  96. return 0;
  97. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty