func_code_string: #include <bits/stdc++.h>   using namespace std;   int n, q;   long long ans = 0;   vector<int> graph[100005];   int depth[100005];   int parent[100005];   int weight[100005];   int ancestor[100005][17];   int comp[100005];   vector<int> comp_members[100005];   int comp_root[100005];   long long ansv[100005];   void merge_comp(int a, int b) {     if (comp_members[a].size() > comp_members[b].size()) swap(a, b);     ans -= (long long)(comp_members[a].size()) * ansv[a];     ans -= (long long)(comp_members[b].size()) * ansv[b];     for (int i = 0; i < comp_members[a].size(); i++) {       int x = comp_members[a][i];       comp_members[b].push_back(x);       comp[x] = b;     }     if (depth[comp_root[a]] < depth[comp_root[b]]) {       long long up_p2_sum = ((long long)(n - 1) * (long long)((n - 1) - 1)) -                             ansv[a] -                             ((long long)(weight[comp_root[b]]) *                              (long long)((weight[comp_root[b]]) - 1));       ansv[b] += ((long long)(n - weight[comp_root[b]]) *                   (long long)((n - weight[comp_root[b]]) - 1)) -                  up_p2_sum;       comp_root[b] = comp_root[a];     } else {       long long down_p2_sum = ((long long)(n - 1) * (long long)((n - 1) - 1)) -                               ansv[a] -                               ((long long)(n - weight[comp_root[a]]) *                                (long long)((n - weight[comp_root[a]]) - 1));       ansv[b] += ((long long)(weight[comp_root[a]]) *                   (long long)((weight[comp_root[a]]) - 1)) -                  down_p2_sum;     }     ans += (long long)(comp_members[b].size()) * ansv[b];     comp_members[a].clear();   }   void merge_chain(int from, int to) {     while (comp[from] != comp[to]) {       from = comp_root[comp[from]];       merge_comp(comp[from], comp[parent[from]]);     }   }   void dfs(int node, int prt) {     ansv[node] += ((long long)(n - 1) * (long long)((n - 1) - 1));     depth[node] = depth[prt] + 1;     parent[node] = prt;     weight[node] = 1;     for (int i = 0; i < graph[node].size(); i++) {       int nxt = graph[node][i];       if (nxt == prt) continue;       dfs(nxt, node);       weight[node] += weight[nxt];       ansv[node] -= ((long long)(weight[nxt]) * (long long)((weight[nxt]) - 1));     }     long long above = n - weight[node];     ansv[node] -= ((long long)(above) * (long long)((above)-1));     ans += ansv[node];   }   int lca(int A, int B) {     if (depth[A] < depth[B]) swap(A, B);     for (int i = 0; i < 17; i++) {       if ((depth[A] - depth[B]) & (1 << i)) A = ancestor[A][i];     }     if (A == B) return A;     for (int i = 17 - 1; i >= 0; i--) {       if (ancestor[A][i] != ancestor[B][i]) {         A = ancestor[A][i];         B = ancestor[B][i];       }     }     return parent[A];   }   void inputtree(int siz, int root = 1) {     for (int i = 1; i < siz; i++) {       int u, v;       scanf( %d%d , &u, &v);       graph[u].push_back(v);       graph[v].push_back(u);     }     dfs(root, 0);     for (int i = 1; i <= siz; i++) ancestor[i][0] = parent[i];     for (int pwr = 1; pwr < 17; pwr++) {       for (int i = 1; i <= siz; i++) {         ancestor[i][pwr] = ancestor[ancestor[i][pwr - 1]][pwr - 1];       }     }   }   int main() {     scanf( %d , &n);     inputtree(n);     for (int i = 1; i <= n; i++) {       comp[i] = i;       comp_members[i].push_back(i);       comp_root[i] = i;     }     printf( %lld n , ans);     scanf( %d , &q);     for (int qry = 1; qry <= q; qry++) {       int u, v;       scanf( %d%d , &u, &v);       int lcanode = lca(u, v);       merge_chain(u, lcanode);       merge_chain(v, lcanode);       printf( %lld n , ans);     }     return 0;   }   