1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| #include<bits/stdc++.h> #define PII pair<int, int> using namespace std; template <class T> inline void read(T &res) { res = 0; bool flag = 0; char c = getchar(); while('0' > c || c > '9') { if(c == '-') flag = 1; c = getchar();} while('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar(); if(flag) res = -res; } template <class T, class ...Arg> inline void read(T &res, Arg &...com){ read(res), read(com...);} template <class T> void out(T res) { if(res > 9) out(res / 10); putchar(res % 10 + '0'); } template <class T> inline void write(T res) { if(res < 0) putchar('-'), res = -res; out(res), putchar('\n'); } mt19937 rnd(114515); const int N = 1e5 + 5; int n, m; int sz2[N]; struct Node{ int s[2], rev, val, v, p; #define l(x) tr[x].s[0] #define r(x) tr[x].s[1] #define fa(x) tr[x].p }tr[N];
inline void pushup(int x) { tr[x].val = tr[l(x)].val ^ tr[x].v ^ tr[r(x)].val ^ sz2[x]; }
inline void rev(int x) { swap(l(x), r(x)); tr[x].rev ^= 1; } inline void pushdown(int x) { if(!tr[x].rev) return ; rev(l(x)), rev(r(x)); tr[x].rev = 0; } inline bool isroot(int x) { return l(fa(x)) != x && r(fa(x)) != x; } inline void rotate(int x) { int y = fa(x), z = fa(y); int k = r(y) == x; if(!isroot(y)) tr[z].s[r(z) == y] = x; fa(x) = z; tr[y].s[k] = tr[x].s[k ^ 1], fa(tr[x].s[k ^ 1]) = y; tr[x].s[k ^ 1] = y, fa(y) = x; pushup(y), pushup(x); } int st[N]; inline void splay(int x) { int top = 0, id = x; st[++ top] = x; while(!isroot(id)) st[++ top] = id = fa(id); while(top) pushdown(st[top --]); while(!isroot(x)) { int y = fa(x), z = fa(y); if(!isroot(y)) { if((r(y) == x) ^ (r(z) == y)) rotate(x); else rotate(y); } rotate(x); } }
inline void access(int x) { int z = x; for(int y = 0; x; y = x, x = fa(x)) { splay(x); sz2[x] ^= tr[r(x)].val ^ tr[y].val; r(x) = y, pushup(x); } splay(z); } inline void make_root(int x) { access(x); rev(x); } inline int find_root(int x) { access(x); while(l(x)) pushdown(x), x = l(x); return x; } inline void link(int x, int y) { make_root(x); if(find_root(y) == x) return ; sz2[y] ^= tr[x].val, tr[x].p = y; pushup(y); } inline void cut(int x, int y) { make_root(x); if(find_root(y) != x) return ; fa(x) = l(y) = 0, pushup(x); } inline void split(int x, int y) { make_root(x); access(y); } PII stk[N * 3]; int tot, ans; int shit[N * 3]; inline bool check(int x, int y) { split(x, y); splay(x); return tr[y].val == ans; } int main() { read(n), read(n, m); for(int i = 1, o, u;i < n;i ++) { read(o, u); link(o, u); } int type, x, y, u, v; while(m --) { read(type); if(type == 1) { read(x, y, u, v); cut(x, y), link(u, v); } if(type == 2) { read(x, y); stk[++ tot] = {x, y}; shit[tot] = rnd(); ans ^= shit[tot]; make_root(x), tr[x].v ^= shit[tot], pushup(x); make_root(y), tr[y].v ^= shit[tot], pushup(y); } if(type == 3) { read(x); ans ^= shit[x]; make_root(stk[x].first), tr[stk[x].first].v ^= shit[x], pushup(stk[x].first); make_root(stk[x].second), tr[stk[x].second].v ^= shit[x], pushup(stk[x].second); } if(type == 4) { read(x, y); if(check(x, y)) puts("YES"); else puts("NO"); } } return 0; }
|