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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| #include <queue> #include <bitset> #include <cstdio> #include <vector> #include <utility> #include <algorithm>
using namespace std;
const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; }; class delheap { priority_queue<int> has, del; bool modified = false; int secmax = 0; void maintain() { while(!del.empty() && has.top() == del.top()) { has.pop(); del.pop(); } } public: void insert(int x) { modified = true; has.push(x); } void erase(int x) { modified = true; del.push(x); } int qmax() { maintain(); return has.top(); } int qmps() { int t = qmax(); if(!modified) return t + secmax; modified = false; erase(t); secmax = qmax(); insert(t); return t + secmax; } }; delheap chxs[100005], gmr[100005], gans; vector<pair<unsigned, int>> web[100005]; unsigned decfa[100005]; bitset<100005> stat; unsigned dep[100005]; unsigned fa[20][100005]; int fv[20][100005];
#ifndef __linux__ #define getchar_unlocked _getchar_nolock #endif
char ibf[50000005]; unsigned li = 0;
int qread() { int ans = 0, fw = 1; char ch; while((ch = ibf[li++]) < '0' || ch > '9') if(ch == '-') fw = -1; do { ans = ans * 10 + (ch - '0'); } while((ch = ibf[li++]) >= '0' && ch <= '9'); return ans * fw; } unsigned uqread() { unsigned ans = 0; char ch; while((ch = ibf[li++]) < '0' || ch > '9'); do { ans = ans * 10 + (ch - '0'); } while((ch = ibf[li++]) >= '0' && ch <= '9'); return ans; }
int main() { fread(ibf, 1, sizeof ibf, stdin); unsigned n = uqread(); for(unsigned i=1;i<n;i++) { unsigned u, v; int w; u = uqread(); v = uqread(); w = qread(); web[u].push_back({v, w}); web[v].push_back({u, w}); } auto cent = [](unsigned rt) -> int { unsigned tot = 0; U([&](auto &&self, unsigned u, unsigned ff) -> void { tot++; for(const auto &[v, w] : web[u]) if(v != ff && !stat[v]) self(self, v, u); })(rt, 0); return -U([&](auto &&self, unsigned u, unsigned ff) -> int { unsigned sum = 1, maxn = 0; for(const auto &[v, w] : web[u]) if(v != ff && !stat[v]) { int res = self(self, v, u); if(res < 0) return res; sum += (unsigned)res; maxn = max(maxn, (unsigned)res); } maxn = max(maxn, tot - sum); if(maxn <= tot / 2) return -u; else return sum; })(rt, 0); }; U([&](auto &&self, unsigned u) -> void { for(unsigned i=1;(1u<<i)<=dep[u];i++) { fa[i][u] = fa[i-1][fa[i-1][u]]; fv[i][u] = fv[i-1][u] + fv[i-1][fa[i-1][u]]; } for(const auto &[v, w] : web[u]) { if(v != fa[0][u]) { dep[v] = dep[u] + 1; fa[0][v] = u; fv[0][v] = w; self(self, v); } } })(1); auto qsum = [](unsigned u, unsigned v) -> int { int sum = 0; for(int i=16;i>=0;i--) { if(dep[u] >= dep[v] + (1u << i)) { sum += fv[i][u]; u = fa[i][u]; } else if(dep[v] >= dep[u] + (1u << i)) { sum += fv[i][v]; v = fa[i][v]; } if(u == v) break; } if(u == v) return sum; for(int i=16;i>=0;i--) { if(fa[i][u] != fa[i][v]) { sum += fv[i][u] + fv[i][v]; u = fa[i][u]; v = fa[i][v]; } } return sum + fv[0][u] + fv[0][v]; }; U([&](auto &&self, unsigned u, unsigned ffa) -> unsigned { u = cent(u); gmr[u].insert(-0x3f3f3f3f); if(ffa) U([&](auto &&sel, unsigned x, unsigned ff) -> void { gmr[u].insert(qsum(x, ffa)); for(const auto &[y, w] : web[x]) if(y != ff && !stat[y]) sel(sel, y, x); })(u, 0); stat[u] = true; chxs[u].insert(-0x3f3f3f3f); chxs[u].insert(0); for(const auto &[v, w] : web[u]) { if(!stat[v]) { unsigned res = self(self, v, u); decfa[res] = u; chxs[u].insert(gmr[res].qmax()); } } gans.insert(chxs[u].qmps()); return u; })(1, 0); unsigned q = uqread(); unsigned cnt = n; while(q--) { char op; while((op = ibf[li++]) != 'A' && op != 'C'); if(op == 'A') { if(!cnt) puts("They have disappeared."); else printf("%d\n", max(0, gans.qmax())); } else { unsigned u; u = uqread(); if(stat[u]) { cnt--; unsigned x = u; while(x) { if(decfa[x]) { int yy = chxs[decfa[x]].qmps(); int xx = gmr[x].qmax(); gmr[x].erase(qsum(u, decfa[x])); if(xx != gmr[x].qmax()) { chxs[decfa[x]].erase(xx); chxs[decfa[x]].insert(gmr[x].qmax()); } if(yy != chxs[decfa[x]].qmps()) { gans.erase(yy); gans.insert(chxs[decfa[x]].qmps()); } } x = decfa[x]; } stat[u] = false; int xx = chxs[u].qmps(); chxs[u].erase(0); if(chxs[u].qmps() != xx) { gans.erase(xx); gans.insert(chxs[u].qmps()); } } else { cnt++; unsigned x = u; while(x) { if(decfa[x]) { int yy = chxs[decfa[x]].qmps(); int xx = gmr[x].qmax(); gmr[x].insert(qsum(u, decfa[x])); if(xx != gmr[x].qmax()) { chxs[decfa[x]].erase(xx); chxs[decfa[x]].insert(gmr[x].qmax()); } if(yy != chxs[decfa[x]].qmps()) { gans.erase(yy); gans.insert(chxs[decfa[x]].qmps()); } } x = decfa[x]; } stat[u] = true; int xx = chxs[u].qmps(); chxs[u].insert(0); if(chxs[u].qmps() != xx) { gans.erase(xx); gans.insert(chxs[u].qmps()); } } } } return 0; }
|