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 182
|
#include <bits/stdc++.h> using namespace std; #define int long long #define resetIO(x) \ freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout) #define debug(fmt, ...) \ fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) template <class _Tp> inline _Tp& read(_Tp &x) { bool sign = false; char ch = getchar(); long double tmp = 1; for (; !isdigit(ch); ch = getchar()) sign |= (ch == '-'); for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48); if (ch == '.') for (ch = getchar(); isdigit(ch); ch = getchar()) tmp /= 10.0, x += tmp * (ch ^ 48); return sign ? (x = -x) : x; } template <class _Tp> inline void write(_Tp x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar((x % 10) ^ 48); } const int MAXN = 1e5 + 10; const int LOGN = 18; const int INF = 0x3f3f3f3f3f3f3f3f; struct heap { priority_queue<int> q, p; void push(int x) { q.push(x); } void erase(int x) { p.push(x); } int top() { while (p.size() && q.size() && p.top() == q.top()) p.pop(), q.pop(); return q.top(); } void pop() { while (p.size() && q.size() && p.top() == q.top()) p.pop(), q.pop(); q.pop(); } int size() { return q.size() - p.size(); } int sectop() { int t = top(); pop(); int r = top(); push(t); return r; } }; int n, q, fa[MAXN], dep[MAXN], fq[MAXN][LOGN]; int scnt, siz[MAXN], mxsiz[MAXN]; bool vis[MAXN], opn[MAXN]; vector<int> g[MAXN]; heap shp, hp[MAXN][2]; char buf[4]; int getroot(int u, int f, int sz) { int ret = 0; siz[u] = 1, mxsiz[u] = 0; for (auto v : g[u]) { if (v == f || vis[v]) continue; int tmp = getroot(v, u, sz); if (mxsiz[tmp] < mxsiz[ret]) ret = tmp; siz[u] += siz[v]; mxsiz[u] = max(mxsiz[u], siz[v]); } mxsiz[u] = max(mxsiz[u], sz - siz[u]); if (mxsiz[u] < mxsiz[ret]) ret = u; return ret; } void build1(int u, int f) { fq[u][0] = f, dep[u] = dep[f] + 1; for (int i = 1; i < LOGN; ++i) fq[u][i] = fq[fq[u][i - 1]][i - 1]; for (auto v : g[u]) if (v != f) build1(v, u); } void build2(int u, int f, int sz) { vis[u] = true, fa[u] = f; for (auto v : g[u]) { if (v == f || vis[v]) continue; int ns = (siz[u] > siz[v]) ? siz[v] : sz - siz[u]; int rt = getroot(v, u, ns); build2(rt, u, ns); } } int lca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); int t = dep[u] - dep[v]; for (int i = LOGN - 1; ~i; --i) if ((t >> i) & 1) u = fq[u][i]; if (u == v) return u; for (int i = LOGN - 1; ~i; --i) if (fq[u][i] != fq[v][i]) u = fq[u][i], v = fq[v][i]; return fq[u][0]; } int dist(int u, int v) { return dep[u] + dep[v] - (dep[lca(u, v)] << 1); } void update(int x) { if (hp[x][0].size() >= 2) shp.erase(hp[x][0].top() + hp[x][0].sectop()); if (opn[x]) hp[x][0].push(0); else hp[x][0].erase(0); if (hp[x][0].size() >= 2) shp.push(hp[x][0].top() + hp[x][0].sectop()); for (int u = x; fa[u]; u = fa[u]) { if (hp[fa[u]][0].size() >= 2) shp.erase(hp[fa[u]][0].top() + hp[fa[u]][0].sectop()); if (hp[u][1].size()) hp[fa[u]][0].erase(hp[u][1].top()); if (opn[x]) hp[u][1].push(dist(fa[u], x)); else hp[u][1].erase(dist(fa[u], x)); if (hp[u][1].size()) hp[fa[u]][0].push(hp[u][1].top()); if (hp[fa[u]][0].size() >= 2) shp.push(hp[fa[u]][0].top() + hp[fa[u]][0].sectop()); } } int query() { return shp.size() ? shp.top() : (scnt ? 0 : -1); } signed main() { read(n); for (int i = 1; i < n; ++i) { int u, v; read(u), read(v); g[u].emplace_back(v); g[v].emplace_back(u); } mxsiz[0] = INF; build1(1, 0); build2(getroot(1, 0, n), 0, n); for (int i = 1; i <= n; ++i) opn[i] = true, update(i), ++scnt; read(q); while (q--) { scanf("%s", buf); if (buf[0] == 'G') { write(query()), putchar('\n'); } else { int x; read(x); opn[x] ? --scnt : ++scnt; opn[x] = !opn[x]; update(x); } } return 0; }
|