func_code_string: #include <bits/stdc++.h>   #pragma GCC optimize(4)   using namespace std;   char _buf[100000], *_p1 = _buf, *_p2 = _buf;   inline int gi() {     int x = 0, f = 1;     char ch = (_p1 == _p2 && (_p2 = (_p1 = _buf) + fread(_buf, 1, 100000, stdin),                               _p1 == _p2)                    ? EOF                    : *_p1++);     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) f = -1;       ch = (_p1 == _p2 && (_p2 = (_p1 = _buf) + fread(_buf, 1, 100000, stdin),                            _p1 == _p2)                 ? EOF                 : *_p1++);     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 3) + (x << 1) + (ch ^ 48);       ch = (_p1 == _p2 && (_p2 = (_p1 = _buf) + fread(_buf, 1, 100000, stdin),                            _p1 == _p2)                 ? EOF                 : *_p1++);     }     return (f == 1) ? x : -x;   }   inline int max(int a, int b) { return a > b ? a : b; }   inline int min(int a, int b) { return a < b ? a : b; }   const int maxn = 5e5 + 5;   int n, p[maxn], sz, maxpos, maxdis, fa[maxn], Q;   struct node {     int u, v, w;   };   vector<node> edg;   struct edge {     int v, w, nxt;     edge(int vv = 0, int ww = 0, int nn = 0) { v = vv, w = ww, nxt = nn; }   } e[maxn << 1];   inline void add(int u, int v, int w) {     e[++sz] = edge(v, w, p[u]);     p[u] = sz;   }   struct segmentree {     static const int maxn = 2e6 + 5;     int rt;     int dep[maxn], tot, dfn[maxn], siz[maxn], len[maxn], rk[maxn];     bool tag[maxn];     int maxv[maxn][2];     inline void pushup(int u) {       for (int i = 0; i <= 1; ++i) {         if (maxv[u << 1][i] > maxv[u << 1 | 1][i])           maxv[u][i] = maxv[u << 1][i];         else           maxv[u][i] = maxv[u << 1 | 1][i];       }     }     inline void pushdown(int u) {       if (tag[u]) {         swap(maxv[u << 1][0], maxv[u << 1][1]);         swap(maxv[u << 1 | 1][0], maxv[u << 1 | 1][1]);         tag[u << 1] ^= 1, tag[u << 1 | 1] ^= 1, tag[u] = 0;       }     }     inline void dfs(int u, int fa) {       dfn[u] = ++tot;       siz[u] = 1;       rk[tot] = u;       for (int i = p[u]; i != -1; i = e[i].nxt) {         int v = e[i].v;         if (v == fa) continue;         if (e[i].w == 1)           len[v] = len[u] ^ 1;         else           len[v] = len[u];         dep[v] = dep[u] + 1;         dfs(v, u);         siz[u] += siz[v];       }     }     inline void build(int u, int l, int r) {       if (l == r) {         maxv[u][len[rk[l]]] = dep[rk[l]];         return;       }       int mid = (l + r) >> 1;       build(u << 1, l, mid);       build(u << 1 | 1, mid + 1, r);       pushup(u);     }     inline int query(int u, int l, int r, int x, int y) {       if (x <= l && y >= r) return maxv[u][0];       int mid = (l + r) >> 1, ret = 0;       pushdown(u);       if (x <= mid) ret = query(u << 1, l, mid, x, y);       if (y > mid) ret = max(ret, query(u << 1 | 1, mid + 1, r, x, y));     }     inline void update(int u, int l, int r, int x, int y) {       if (x <= l && y >= r) {         tag[u] ^= 1;         swap(maxv[u][0], maxv[u][1]);         return;       }       int mid = (l + r) >> 1;       pushdown(u);       if (x <= mid) update(u << 1, l, mid, x, y);       if (y > mid) update(u << 1 | 1, mid + 1, r, x, y);       pushup(u);     }     inline void change(int u, int v) {       if (dep[u] > dep[v]) swap(u, v);       update(1, 1, n, dfn[v], dfn[v] + siz[v] - 1);     }   } tree1, tree2;   inline void dfs2(int u, int f, int dis) {     if (dis > maxdis) {       maxpos = u, maxdis = dis;     }     for (int i = p[u]; i != -1; i = e[i].nxt) {       int v = e[i].v;       if (v == f) continue;       dfs2(v, u, dis + 1);     }   }   inline void init() {     dfs2(1, 0, 0);     tree1.rt = maxpos;     maxdis = 0;     dfs2(maxpos, 0, 0);     tree2.rt = maxpos;     tree1.dfs(tree1.rt, 0);     tree2.dfs(tree2.rt, 0);     tree1.build(1, 1, n);     tree2.build(1, 1, n);   }   inline void input() {     memset(p, -1, sizeof(p));     n = gi();     node x;     edg.push_back(x);     for (int i = 1; i <= n - 1; ++i) {       x.u = gi(), x.v = gi(), x.w = gi();       edg.push_back(x);       add(x.u, x.v, x.w);       add(x.v, x.u, x.w);     }   }   inline void solve() {     Q = gi();     while (Q--) {       int x = gi();       int u = edg[x].u, v = edg[x].v;       tree1.change(u, v);       tree2.change(u, v);       printf( %d n , max(tree1.query(1, 1, n, 1, n), tree2.query(1, 1, n, 1, n)));     }   }   int main() {     input();     init();     solve();     return 0;   }   