Advertisement
Guest User

Untitled

a guest
Nov 28th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.32 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAXN = 100005;
  5. const int NINF = -500000000;
  6. int n;
  7. vector<pair<int, int> > tr[MAXN];
  8. int sz[MAXN];
  9. int p[MAXN][18];
  10. int dis[MAXN];
  11. int depth[MAXN];
  12. void dfs(int i, int par, int curDepth, int curDis) {
  13.     p[i][0] = par;
  14.     dis[i] = curDis;
  15.     depth[i] = curDepth;
  16.     for (int j = 1; j < 18; j++) {
  17.         if (p[i][j-1] == -1) {
  18.             p[i][j] = -1;
  19.         } else {
  20.             p[i][j] = p[p[i][j-1]][j-1];
  21.         }
  22.     }
  23.     for (int j = 0; j < tr[i].size(); j++) {
  24.         if (tr[i][j].first == par) continue;
  25.         dfs(tr[i][j].first, i, curDepth + 1, curDis + tr[i][j].second);
  26.     }
  27. }
  28. int getAnc(int u, int k) {
  29.     for (int i = 17; i >= 0; i--) {
  30.         if ((1<<i) <= k) {
  31.             k-= (1<<i);
  32.             u = p[u][i];
  33.         }
  34.     }
  35.     return u;
  36. }
  37. int getDis(int u, int v) {
  38.     if (u == v) return 0;
  39.     if (depth[u] < depth[v]) swap(u, v);
  40.     int uA = getAnc(u, depth[u] - depth[v]);
  41.     int vA = v;
  42.     for (int i = 17; i >= 0; i--) {
  43.         if (p[uA][i] != p[vA][i]) {
  44.             uA = p[uA][i];
  45.             vA = p[vA][i];
  46.         }
  47.     }
  48.  
  49.     int lca = uA;
  50.     if (uA != vA) lca = p[uA][0];
  51.     return dis[u] + dis[v] - 2*dis[lca];
  52. }
  53.  
  54. int tr_sz[MAXN], cnt_vers;
  55. bool used[MAXN];
  56.  
  57. void pre_dfs(int u, int pr)
  58. {
  59.     cnt_vers++;
  60.     tr_sz[u] = 1;
  61.     for (int j = 0; j < tr[u].size(); j++) {
  62.         int v = tr[u][j].first;
  63.         if (!used[v] && v != pr) {
  64.             pre_dfs(v, u);
  65.             tr_sz[u] += tr_sz[v];
  66.         }
  67.     }
  68. }
  69.  
  70. int centroid(int u, int pr)
  71. {
  72.     for (int j = 0; j < tr[u].size(); j++) {
  73.         int v = tr[u][j].first;
  74.         if(!used[v] && v != pr && tr_sz[v] > cnt_vers / 2)
  75.             return centroid(v, u);
  76.     }
  77.     return u;
  78. }
  79.  
  80. int parC[MAXN];
  81.  
  82. void decompose(int u, int pr = -1)
  83. {
  84.     cnt_vers = 0;
  85.  
  86.     pre_dfs(u, u);
  87.     int cen = centroid(u, u);
  88.     parC[cen] = pr;
  89.  
  90.     used[cen] = true;
  91.     for (int j = 0; j < tr[u].size(); j++) {
  92.         int v = tr[u][j].first;
  93.         if(!used[v])
  94.             decompose(v, cen);
  95.     }
  96.     used[cen] = false;
  97. }
  98. priority_queue<pair<int, pair<int, int> > > best;
  99. priority_queue<pair<int, pair<int, int> > > bestOfChild[MAXN];
  100. map<int, priority_queue<pair<int, int> > > bestPerChild[MAXN];
  101. int col[MAXN];
  102. int getMax() {
  103.     while (!best.empty()) {
  104.         if (best.top().second.first != -1 && best.top().second.second != -1
  105.                 && col[best.top().second.first] && col[best.top().second.second])
  106.             return best.top().first;
  107.         best.pop();
  108.     }
  109.     return NINF;
  110. }
  111.  
  112. pair<int, int> getMax(int cen, int ch) {
  113.     priority_queue<pair<int, int> > &q = bestPerChild[cen][ch];
  114.     while (!q.empty()) {
  115.         if (q.top().second != -1 && col[q.top().second]) return q.top();
  116.         q.pop();
  117.     }
  118.     return {NINF, -1};
  119. }
  120.  
  121. pair<int, pair<int, int> > getMax2(int cen) {
  122.     priority_queue<pair<int, pair<int, int> > > &q = bestOfChild[cen];
  123.     pair<int, pair<int, int> > mx(NINF, {-1, -1});
  124.     while (!q.empty()) {
  125.         if (q.top().second.second != -1 && col[q.top().second.second]) {
  126.             mx = q.top();
  127.             break;
  128.         }
  129.         q.pop();
  130.     }
  131.     pair<int, pair<int, int> > mx2(NINF, {-1, -1});
  132.     if (!q.empty()) {
  133.         q.pop();
  134.         while (!q.empty()) {
  135.             if (q.top().second.second != -1 && col[q.top().second.second]
  136.                     && q.top().second.first != mx.second.first) {
  137.                 mx2 = q.top();
  138.                 break;
  139.             }
  140.             q.pop();
  141.         }
  142.         q.push(mx);
  143.         return {mx.first + mx2.first, {mx.second.second, mx2.second.second}};
  144.     }
  145.     return {NINF, {-1, -1}};
  146. }
  147. void upd(int u, int cen, int ch, bool add) {
  148.     int disCen = getDis(u, cen);
  149.     int newVal = add ? disCen : NINF;
  150.     bestPerChild[cen][ch].push( { newVal, u });
  151.     pair<int, int> bpcMax = getMax(cen, ch);
  152.     bestOfChild[cen].push( { bpcMax.first, { ch, bpcMax.second } });
  153.     best.push(getMax2(cen));
  154. }
  155. void upd(int u, bool add) {
  156.     int prev = u;
  157.     int cur = parC[u];
  158.     upd(u, u, u, add);
  159.     while (cur != -1) {
  160.         upd(u, cur, prev, add);
  161.         prev = cur;
  162.         cur = parC[cur];
  163.     }
  164. }
  165. int cntW = 0;
  166. void flip(int u) {
  167.     col[u] = 1 - col[u];
  168.     if (col[u]) cntW++;
  169.     else cntW--;
  170.     upd(u, col[u]);
  171. }
  172. int main() {
  173.     scanf("%d", &n);
  174.     for (int i = 1; i < n; i++) {
  175.         int u, v, w;
  176.         scanf("%d %d %d", &u, &v, &w);
  177.         u--;
  178.         v--;
  179.         tr[u].push_back({v, w});
  180.         tr[v].push_back({u, w});
  181.     }
  182.     dfs(0, -1, 0, 0);
  183.     decompose(0);
  184.     for (int i = 0; i < n; i++) flip(i);
  185.     int Q;
  186.     scanf("%d", &Q);
  187.     char T[5];
  188.     while (Q--) {
  189.         scanf("%s", T);
  190.         if (T[0] == 'C') {
  191.             int u;
  192.             scanf("%d", &u);
  193.             flip(u-1);
  194.         } else {
  195.             if (cntW > 0) {
  196.                 printf("%d\n", max(0, getMax()));
  197.             } else {
  198.                 printf("They have disappeared.\n");
  199.             }
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement