Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MAXN = 100005;
- const int NINF = -500000000;
- int n;
- vector<pair<int, int> > tr[MAXN];
- int sz[MAXN];
- int p[MAXN][18];
- int dis[MAXN];
- int depth[MAXN];
- void dfs(int i, int par, int curDepth, int curDis) {
- p[i][0] = par;
- dis[i] = curDis;
- depth[i] = curDepth;
- for (int j = 1; j < 18; j++) {
- if (p[i][j-1] == -1) {
- p[i][j] = -1;
- } else {
- p[i][j] = p[p[i][j-1]][j-1];
- }
- }
- for (int j = 0; j < tr[i].size(); j++) {
- if (tr[i][j].first == par) continue;
- dfs(tr[i][j].first, i, curDepth + 1, curDis + tr[i][j].second);
- }
- }
- int getAnc(int u, int k) {
- for (int i = 17; i >= 0; i--) {
- if ((1<<i) <= k) {
- k-= (1<<i);
- u = p[u][i];
- }
- }
- return u;
- }
- int getDis(int u, int v) {
- if (u == v) return 0;
- if (depth[u] < depth[v]) swap(u, v);
- int uA = getAnc(u, depth[u] - depth[v]);
- int vA = v;
- for (int i = 17; i >= 0; i--) {
- if (p[uA][i] != p[vA][i]) {
- uA = p[uA][i];
- vA = p[vA][i];
- }
- }
- int lca = uA;
- if (uA != vA) lca = p[uA][0];
- return dis[u] + dis[v] - 2*dis[lca];
- }
- int tr_sz[MAXN], cnt_vers;
- bool used[MAXN];
- void pre_dfs(int u, int pr)
- {
- cnt_vers++;
- tr_sz[u] = 1;
- for (int j = 0; j < tr[u].size(); j++) {
- int v = tr[u][j].first;
- if (!used[v] && v != pr) {
- pre_dfs(v, u);
- tr_sz[u] += tr_sz[v];
- }
- }
- }
- int centroid(int u, int pr)
- {
- for (int j = 0; j < tr[u].size(); j++) {
- int v = tr[u][j].first;
- if(!used[v] && v != pr && tr_sz[v] > cnt_vers / 2)
- return centroid(v, u);
- }
- return u;
- }
- int parC[MAXN];
- void decompose(int u, int pr = -1)
- {
- cnt_vers = 0;
- pre_dfs(u, u);
- int cen = centroid(u, u);
- parC[cen] = pr;
- used[cen] = true;
- for (int j = 0; j < tr[u].size(); j++) {
- int v = tr[u][j].first;
- if(!used[v])
- decompose(v, cen);
- }
- used[cen] = false;
- }
- priority_queue<pair<int, pair<int, int> > > best;
- priority_queue<pair<int, pair<int, int> > > bestOfChild[MAXN];
- map<int, priority_queue<pair<int, int> > > bestPerChild[MAXN];
- int col[MAXN];
- int getMax() {
- while (!best.empty()) {
- if (best.top().second.first != -1 && best.top().second.second != -1
- && col[best.top().second.first] && col[best.top().second.second])
- return best.top().first;
- best.pop();
- }
- return NINF;
- }
- pair<int, int> getMax(int cen, int ch) {
- priority_queue<pair<int, int> > &q = bestPerChild[cen][ch];
- while (!q.empty()) {
- if (q.top().second != -1 && col[q.top().second]) return q.top();
- q.pop();
- }
- return {NINF, -1};
- }
- pair<int, pair<int, int> > getMax2(int cen) {
- priority_queue<pair<int, pair<int, int> > > &q = bestOfChild[cen];
- pair<int, pair<int, int> > mx(NINF, {-1, -1});
- while (!q.empty()) {
- if (q.top().second.second != -1 && col[q.top().second.second]) {
- mx = q.top();
- break;
- }
- q.pop();
- }
- pair<int, pair<int, int> > mx2(NINF, {-1, -1});
- if (!q.empty()) {
- q.pop();
- while (!q.empty()) {
- if (q.top().second.second != -1 && col[q.top().second.second]
- && q.top().second.first != mx.second.first) {
- mx2 = q.top();
- break;
- }
- q.pop();
- }
- q.push(mx);
- return {mx.first + mx2.first, {mx.second.second, mx2.second.second}};
- }
- return {NINF, {-1, -1}};
- }
- void upd(int u, int cen, int ch, bool add) {
- int disCen = getDis(u, cen);
- int newVal = add ? disCen : NINF;
- bestPerChild[cen][ch].push( { newVal, u });
- pair<int, int> bpcMax = getMax(cen, ch);
- bestOfChild[cen].push( { bpcMax.first, { ch, bpcMax.second } });
- best.push(getMax2(cen));
- }
- void upd(int u, bool add) {
- int prev = u;
- int cur = parC[u];
- upd(u, u, u, add);
- while (cur != -1) {
- upd(u, cur, prev, add);
- prev = cur;
- cur = parC[cur];
- }
- }
- int cntW = 0;
- void flip(int u) {
- col[u] = 1 - col[u];
- if (col[u]) cntW++;
- else cntW--;
- upd(u, col[u]);
- }
- int main() {
- scanf("%d", &n);
- for (int i = 1; i < n; i++) {
- int u, v, w;
- scanf("%d %d %d", &u, &v, &w);
- u--;
- v--;
- tr[u].push_back({v, w});
- tr[v].push_back({u, w});
- }
- dfs(0, -1, 0, 0);
- decompose(0);
- for (int i = 0; i < n; i++) flip(i);
- int Q;
- scanf("%d", &Q);
- char T[5];
- while (Q--) {
- scanf("%s", T);
- if (T[0] == 'C') {
- int u;
- scanf("%d", &u);
- flip(u-1);
- } else {
- if (cntW > 0) {
- printf("%d\n", max(0, getMax()));
- } else {
- printf("They have disappeared.\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement