fork download
  1. #include <bits/stdc++.h>
  2. #define endl '\n'
  3.  
  4. //#pragma GCC optimize ("O3")
  5. //#pragma GCC target ("sse4")
  6.  
  7. #define SZ(x) ((int)x.size())
  8. #define ALL(V) V.begin(), V.end()
  9. #define L_B lower_bound
  10. #define U_B upper_bound
  11. #define pb push_back
  12.  
  13. using namespace std;
  14. template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
  15. template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
  16. const int MAXN = (1 << 20);
  17.  
  18. int tr[MAXN];
  19.  
  20. void add(int p, int v) { for(; p < MAXN; p += (p & -p)) tr[p] += v; }
  21.  
  22. int query(int p)
  23. {
  24. int ans = 0;
  25. for(; p >= 1; p -= (p & -p)) ans += tr[p];
  26. return ans;
  27. }
  28.  
  29. int n, m;
  30. vector<int> adj[MAXN];
  31. int st[MAXN], en[MAXN], par[MAXN][20], dfs_time;
  32.  
  33. int A[MAXN], B[MAXN];
  34.  
  35. void read()
  36. {
  37. cin >> n >> m;
  38. m -= n - 1;
  39. for(int i = 0; i < n - 1; i++)
  40. {
  41. int u, v;
  42. cin >> u >> v;
  43. adj[u].pb(v);
  44. adj[v].pb(u);
  45. }
  46.  
  47. for(int i = 0; i < m; i++)
  48. cin >> A[i] >> B[i];
  49. }
  50.  
  51. void pre_dfs(int u, int pr)
  52. {
  53. st[u] = ++dfs_time;
  54. par[u][0] = pr;
  55. for(int i = 1; i < 20; i++)
  56. par[u][i] = par[par[u][i - 1]][i - 1];
  57.  
  58. for(int v: adj[u])
  59. if(v != pr)
  60. pre_dfs(v, u);
  61.  
  62. en[u] = dfs_time;
  63. }
  64.  
  65. bool upper(int u, int v) { return st[u] <= st[v] && st[v] <= en[u]; }
  66.  
  67. int lca(int u, int v)
  68. {
  69. if(upper(u, v)) return u;
  70. if(upper(v, u)) return v;
  71.  
  72. for(int i = 19; i >= 0; i--)
  73. if(!upper(par[u][i], v))
  74. u = par[u][i];
  75.  
  76. return par[u][0];
  77. }
  78.  
  79. int get_u(int u, int anc)
  80. {
  81. if(u == anc) return -1;
  82. for(int i = 19; i >= 0; i--)
  83. if(!upper(par[u][i], anc))
  84. u = par[u][i];
  85.  
  86. return u;
  87. }
  88.  
  89. int64_t answer;
  90. vector<int> li[MAXN];
  91.  
  92. int64_t C2(int x) { return x * 1ll * (x - 1) / 2ll; }
  93.  
  94. void dfs(int u, int pr)
  95. {
  96. map<int, int> M1;
  97. map<pair<int, int>, int> M2;
  98. for(int i: li[u])
  99. {
  100. int x = get_u(A[i], u), y = get_u(B[i], u);
  101.  
  102. if(x > y) swap(x, y);
  103. if(x != -1) answer += query(en[x]) - query(st[x] - 1);
  104. if(y != -1) answer += query(en[y]) - query(st[y] - 1);
  105.  
  106. if(x != -1) M1[x]++;
  107. if(y != -1) M1[y]++;
  108. if(x != -1 && y != -1) M2[{x, y}]++;
  109. }
  110.  
  111. for(auto it: M1) answer += C2(it.second);
  112. for(auto it: M2) answer -= C2(it.second);
  113.  
  114. for(int i: li[u])
  115. {
  116. int x = A[i], y = B[i];
  117. add(st[x], 1);
  118. add(st[y], 1);
  119. }
  120.  
  121. for(int v: adj[u])
  122. if(v != pr)
  123. dfs(v, u);
  124.  
  125. for(int i: li[u])
  126. {
  127. int x = A[i], y = B[i];
  128. add(st[x], -1);
  129. add(st[y], -1);
  130. }
  131. }
  132.  
  133. void solve()
  134. {
  135. pre_dfs(1, 1);
  136.  
  137. for(int i = 0; i < m; i++)
  138. {
  139. int l = lca(A[i], B[i]);
  140. li[l].pb(i);
  141. }
  142.  
  143. dfs(1, 1);
  144. cout << answer << endl;
  145. }
  146.  
  147. int main()
  148. {
  149. freopen("exercise.in", "r", stdin);
  150. freopen("exercise.out", "w", stdout);
  151. ios_base::sync_with_stdio(false);
  152. cin.tie(NULL);
  153.  
  154. read();
  155. solve();
  156. return 0;
  157. }
  158.  
  159.  
Runtime error #stdin #stdout #stderr 0.01s 166784KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::ios_base::failure[abi:cxx11]'
  what():  basic_filebuf::underflow error reading the file: iostream error