func_code_string: #include <bits/stdc++.h>   #pragma optimize( SEX_ON_THE_BEACH )   #pragma GCC optimize( unroll-loops )   #pragma GCC optimize( O3 )   #pragma GCC optimize( Ofast )   #pragma GCC optimize( fast-math )   #pragma GCC optimize( no-stack-protector )   #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native )   using ll = long long int;   using ull = unsigned long long int;   using dd = double;   using ldd = long double;   namespace someUsefull {   template <typename T1, typename T2>   inline void checkMin(T1& a, T2 b) {     if (a > b) a = b;   }   template <typename T1, typename T2>   inline void checkMax(T1& a, T2 b) {     if (a < b) a = b;   }   const int _bitfunctions_size_ = 16;   const int _bitfunctions_mask_ = (1 << _bitfunctions_size_) - 1;   char* _bits_count_;   inline void _build_bits_count_() {     _bits_count_ = new char[1 << _bitfunctions_size_];     for (int i = 1; i < (1 << _bitfunctions_size_); ++i) {       _bits_count_[i] = _bits_count_[i >> 1];       if (i & 1) ++_bits_count_[i];     }   }   template <typename T1 = char, typename T2 = int>   inline T1 popcnt(T2 x) {     T1 ans = 0;     while (x) {       ans += _bits_count_[x & _bitfunctions_mask_];       x >>= _bitfunctions_size_;     }     return ans;   }   }  // namespace someUsefull   namespace operators {   template <typename T1, typename T2>   std::istream& operator>>(std::istream& in, std::pair<T1, T2>& x) {     in >> x.first >> x.second;     return in;   }   template <typename T1, typename T2>   std::ostream& operator<<(std::ostream& out, std::pair<T1, T2> x) {     out << x.first <<     << x.second;     return out;   }   template <typename T1>   std::istream& operator>>(std::istream& in, std::vector<T1>& x) {     for (auto& i : x) in >> i;     return in;   }   template <typename T1>   std::ostream& operator<<(std::ostream& out, std::vector<T1>& x) {     for (auto& i : x) out << i <<    ;     return out;   }   }  // namespace operators   using namespace std;   using namespace operators;   using namespace someUsefull;   vector<vector<int>> gr;   vector<pair<int, int>> ed;   vector<int> d;   void dfs(int v, int _d = 0, int p = -1) {     d[v] = _d;     for (int to : gr[v]) {       if (to != p) dfs(to, _d + 1, v);     }   }   struct Tree {     const int _size = 1 << 19;     int n, t;     vector<pair<int, int>> tree;     vector<int> upd;     vector<int> d;     vector<int> pre;     vector<int> tin, tout, tos;     void dfs(int v, int _d = 0, int p = -1) {       d[v] = _d;       pre[v] = p;       tin[v] = tos.size();       tos.push_back(v);       for (int to : gr[v]) {         if (to != p) dfs(to, _d + 1, v);       }       tout[v] = tos.size();     }     pair<int, int> merge(pair<int, int> a, pair<int, int> b) {       return {max(a.first, b.first), max(a.second, b.second)};     }     Tree(int v) {       tree.resize(_size << 1, {0, 0});       upd.resize(_size << 1, 0);       n = gr.size();       t = 0;       d.resize(n);       tin.resize(n);       tout.resize(n);       pre.resize(n);       0;       ;       dfs(v);       0;       ;       for (int i = 0; i < n; ++i) tree[_size + i].first = d[tos[i]];       for (int i = _size - 1; i > 0; --i)         tree[i] = merge(tree[i << 1], tree[i << 1 | 1]);     }     void change(int v) {       upd[v] ^= 1;       swap(tree[v].first, tree[v].second);     }     void push(int v) {       if (!upd[v]) return;       change(v << 1);       change(v << 1 | 1);       upd[v] = 0;     }     void update(int v, int l, int r, int fl, int fr) {       if (r <= l || r <= fl || fr <= l) return;       if (fl <= l && r <= fr) {         change(v);         return;       }       push(v);       update(v << 1, l, (r + l) >> 1, fl, fr);       update(v << 1 | 1, (r + l) >> 1, r, fl, fr);       tree[v] = merge(tree[v << 1], tree[v << 1 | 1]);     }     int add(int a, int b) {       if (pre[a] != b) swap(a, b);       update(1, 0, _size, tin[a], tout[a]);       return tree[1].first;     }   };   void solve(int test) {     int n;     cin >> n;     gr.resize(n);     vector<int> type(n - 1);     for (int i = 0; i < n - 1; ++i) {       int a, b;       cin >> a >> b >> type[i];       --a;       --b;       ed.push_back({a, b});       gr[a].push_back(b);       gr[b].push_back(a);     }     d.resize(n);     d.assign(n, -1);     dfs(0);     int v = 0;     for (int i = 0; i < n; ++i)       if (d[v] < d[i]) v = i;     dfs(v);     int L = v;     int R = 0;     for (int i = 0; i < n; ++i)       if (d[R] < d[i]) R = i;     0;     ;     0;     ;     Tree LT(L), RT(R);     0;     ;     for (int i = 0; i < n - 1; ++i) {       if (type[i])         LT.add(ed[i].first, ed[i].second), RT.add(ed[i].first, ed[i].second);     }     int m;     cin >> m;     for (int i = 0; i < m; ++i) {       int x;       cin >> x;       --x;       int ans = 0;       checkMax(ans, LT.add(ed[x].first, ed[x].second));       checkMax(ans, RT.add(ed[x].first, ed[x].second));       cout << ans <<   n ;     }   }   signed main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     _build_bits_count_();     cout << setprecision(12) << fixed;     int t = 1;     for (int i = 0; i < t; ++i) {       0;       ;       solve(i + 1);     }     return 0;   }   