【题目链接】
学习了一发可删堆。
找重心那里忘了写!del[g[i].v],RE无数发。
/* Pigonometry */ #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn = 100005, maxm = maxn << 1, maxk = 21, inf = 0x3f3f3f3f; int n, head[maxn], cnt; int root, clo, tot, bin[maxk], lg[maxm]; int size[maxn], depth[maxn], sm[maxn], pre[maxn], st[maxk][maxm], dfn[maxn], del[maxn], col[maxn]; struct _heap { priority_queue<int> A, B; void update() { for(; B.size() && A.top() == B.top(); A.pop(), B.pop()); } void push(int x) { A.push(x); } void pop() { update(); A.pop(); } void erase(int x) { B.push(x); } int size() { return A.size() - B.size(); } int first() { update(); return A.size() ? A.top() : 0; } int second() { if(size() < 2) return 0; int x = first(); pop(); int y = first(); push(x); return y; } } A, B[maxn], C[maxn]; struct _edge { int v, next; } g[maxn << 1]; inline int iread() { int f = 1, x = 0; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return f * x; } inline void add(int u, int v) { g[cnt] = (_edge){v, head[u]}; head[u] = cnt++; } inline void dfs(int x, int f) { st[0][++clo] = depth[x]; dfn[x] = clo; for(int i = head[x]; ~i; i = g[i].next) if(g[i].v ^ f) { depth[g[i].v] = depth[x] + 1; dfs(g[i].v, x); st[0][++clo] = depth[x]; } } inline int getlca(int u, int v) { u = dfn[u]; v = dfn[v]; if(u > v) swap(u, v); int t = lg[v - u + 1]; return min(st[t][u], st[t][v - bin[t] + 1]); } inline int dis(int u, int v) { return depth[u] + depth[v] - (getlca(u, v) << 1); } inline void find(int x, int f) { size[x] = 1; sm[x] = 0; for(int i = head[x]; ~i; i = g[i].next) if(g[i].v ^ f && !del[g[i].v]) { find(g[i].v, x); size[x] += size[g[i].v]; sm[x] = max(sm[x], size[g[i].v]); } sm[x] = max(sm[x], tot - size[x]); if(sm[x] < sm[root]) root = x; } inline void divide(int x, int f) { pre[x] = f; del[x] = 1; for(int i = head[x]; ~i; i = g[i].next) if(g[i].v ^ f && !del[g[i].v]) { root = 0; tot = size[g[i].v]; find(g[i].v, x); divide(root, x); } } inline void turnoff(int x, int f) { if(x == f) { B[x].push(0); if(B[x].size() == 2) A.push(B[x].first()); } if(!pre[f]) return; int p = pre[f], d = dis(x, p), tmp = C[f].first(); C[f].push(d); if(d > tmp) { int mx = B[p].first() + B[p].second(), sz = B[p].size(); B[p].push(d); if(tmp) B[p].erase(tmp); int now = B[p].first() + B[p].second(); if(now > mx) { if(sz >= 2) A.erase(mx); if(B[p].size() >= 2) A.push(now); } } turnoff(x, p); } inline void turnon(int x, int f) { if(x == f) { if(B[x].size() == 2) A.erase(B[x].first()); B[x].erase(0); } if(!pre[f]) return; int p = pre[f], d = dis(x, p), tmp = C[f].first(); C[f].erase(d); if(d == tmp) { int mx = B[p].first() + B[p].second(), sz = B[p].size(); B[p].erase(d); if(C[f].first()) B[p].push(C[f].first()); int now = B[p].first() + B[p].second(); if(now < mx) { if(sz >= 2) A.erase(mx); if(B[p].size() >= 2) A.push(now); } } turnon(x, p); } int main() { n = iread(); for(int i = 0; i <= n; i++) head[i] = -1; cnt = 0; bin[0] = 1; for(int i = 1; i < maxk; i++) bin[i] = bin[i - 1] << 1; lg[0] = -1; for(int i = 1; i < maxm; i++) lg[i] = lg[i >> 1] + 1; for(int i = 1; i < n; i++) { int u = iread(), v = iread(); add(u, v); add(v, u); } dfs(1, 0); for(int j = 1; j < maxk; j++) for(int i = 1; i <= clo; i++) if(i + bin[j] - 1 <= clo) st[j][i] = min(st[j - 1][i], st[j - 1][i + bin[j - 1]]); sm[0] = inf; root = 0; tot = n; find(1, 0); divide(root, 0); for(int i = 1; i <= n; i++) C[i].push(0), col[i] = 1; for(int i = 1; i <= n; i++) turnoff(i, i); int node = n; for(int T = iread(); T; T--) { char ch = getchar(); for(; ch != 'C' && ch != 'G'; ch = getchar()); if(ch == 'G') { if(node <= 1) printf("%d\n", node - 1); else printf("%d\n", A.first()); } else if(ch == 'C') { int u = iread(); if(col[u] == 1) node--, turnon(u, u); else node++, turnoff(u, u); col[u] ^= 1; } } return 0; }