fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<int, int> pii;
  6. typedef vector<int> vi;
  7. typedef vector<vi> vvi;
  8.  
  9. #define mp make_pair
  10. #define pb(x) push_back(x)
  11. #define rep(i, n) for (int i = 0; i < (n); i++)
  12. #define repp(i, s, e) for (int i = (s); i < (e); i++)
  13. #define all(x) x.begin(), x.end()
  14.  
  15. #define INF 2123456789
  16. #define IINF 9123456789123456789
  17.  
  18. void use_cio() {
  19. ios_base::sync_with_stdio(0);
  20. cin.tie(0);
  21. cout.tie(0);
  22. }
  23. template <typename... Args>
  24. void dbg(Args... args) {
  25. ((cout << args << " "), ...);
  26. cout << endl;
  27. }
  28. //--------------------------------------------------//
  29. int n, q;
  30. vector<pii> adj[100010];
  31. int chk[100010], dep[100010], par[100010];
  32. int p[100010][30];
  33. ll dist1[100010], dist2[100010], tot_dist, conn[100010];
  34. vi cycle;
  35. void dfs(int cur, int par) {
  36. chk[cur] = par;
  37. for (auto [nxt, w] : adj[cur]) {
  38. if (nxt == par) continue;
  39. if (chk[nxt] != 0) {
  40. tot_dist = dist1[cur] + w - dist1[nxt];
  41. while (1) {
  42. cycle.push_back(cur);
  43. cur = chk[cur];
  44. if (cur == nxt || cur == -1) break;
  45. }
  46. cycle.push_back(nxt);
  47. return;
  48. }
  49. dist1[nxt] = dist1[cur] + w;
  50. dfs(nxt, cur);
  51. if (!cycle.empty()) return;
  52. }
  53. }
  54. int lca(int u, int v) {
  55. if (dep[u] < dep[v]) {
  56. swap(u, v);
  57. }
  58. int lg = 1;
  59. for (lg = 1; (1 << lg) <= dep[u]; lg++)
  60. ;
  61. lg -= 1;
  62. for (int i = lg; i >= 0; i--) {
  63. if (dep[u] - (1 << i) >= dep[v]) {
  64. u = p[u][i];
  65. }
  66. }
  67. if (u == v) {
  68. return u;
  69. } else {
  70. for (int i = lg; i >= 0; i--) {
  71. if (p[u][i] != 0 && p[u][i] != p[v][i]) {
  72. u = p[u][i];
  73. v = p[v][i];
  74. }
  75. }
  76. return par[u];
  77. }
  78. }
  79.  
  80. void solve() {
  81. rep(i, 100010) adj[i].clear(), dep[i] = par[i] = chk[i] = dist1[i] = dist2[i] = conn[i] = 0;
  82. memset(p, 0, sizeof(p));
  83. cycle.clear();
  84. tot_dist = 0;
  85.  
  86. cin >> n >> q;
  87. rep(i, n) {
  88. int u, v, w;
  89. cin >> u >> v >> w;
  90. adj[u].push_back({v, w});
  91. adj[v].push_back({u, w});
  92. }
  93. dfs(1, -1);
  94. memset(chk, 0, sizeof(chk));
  95. queue<int> que;
  96. for (auto x : cycle) {
  97. dep[x] = 0;
  98. chk[x] = 1;
  99. par[x] = 0;
  100. conn[x] = x;
  101. que.push(x);
  102. }
  103. while (!que.empty()) {
  104. int x = que.front();
  105. que.pop();
  106. for (auto [y, w] : adj[x]) {
  107. if (!chk[y]) {
  108. dep[y] = dep[x] + 1;
  109. chk[y] = 1;
  110. par[y] = x;
  111. conn[y] = conn[x];
  112. dist2[y] = dist2[x] + w;
  113. que.push(y);
  114. }
  115. }
  116. }
  117. for (int i = 1; i <= n; i++) p[i][0] = par[i];
  118. for (int j = 1; (1 << j) < n; j++) {
  119. for (int i = 1; i <= n; i++) {
  120. if (p[i][j - 1] != 0) {
  121. p[i][j] = p[p[i][j - 1]][j - 1];
  122. }
  123. }
  124. }
  125. while (q--) {
  126. int u, v;
  127. cin >> u >> v;
  128. if (conn[u] != conn[v]) {
  129. ll ans = dist2[u] + dist2[v];
  130. u = conn[u], v = conn[v];
  131. ll dd = abs(dist1[u] - dist1[v]);
  132. dd = min(dd, tot_dist - dd);
  133. cout << ans + dd << "\n";
  134. } else {
  135. int foo = lca(u, v);
  136. cout << dist2[u] + dist2[v] - 2 * dist2[foo] << "\n";
  137. }
  138. }
  139. }
  140. int main() {
  141. use_cio();
  142. int tc;
  143. cin >> tc;
  144. while (tc--) solve();
  145. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void dbg(Args ...)’:
prog.cpp:25:29: warning: fold-expressions only available with -std=c++1z or -std=gnu++1z
     ((cout << args << " "), ...);
                             ^~~
prog.cpp: In function ‘void dfs(int, int)’:
prog.cpp:37:15: error: expected unqualified-id before ‘[’ token
     for (auto [nxt, w] : adj[cur]) {
               ^
prog.cpp:37:15: error: expected ‘;’ before ‘[’ token
prog.cpp:37:16: error: ‘nxt’ was not declared in this scope
     for (auto [nxt, w] : adj[cur]) {
                ^~~
prog.cpp:37:21: error: ‘w’ was not declared in this scope
     for (auto [nxt, w] : adj[cur]) {
                     ^
prog.cpp: In lambda function:
prog.cpp:37:24: error: expected ‘{’ before ‘:’ token
     for (auto [nxt, w] : adj[cur]) {
                        ^
prog.cpp: In function ‘void dfs(int, int)’:
prog.cpp:37:24: error: expected ‘;’ before ‘:’ token
prog.cpp:37:24: error: expected primary-expression before ‘:’ token
prog.cpp:37:24: error: expected ‘)’ before ‘:’ token
prog.cpp:37:24: error: expected primary-expression before ‘:’ token
prog.cpp: In function ‘void solve()’:
prog.cpp:106:19: error: expected unqualified-id before ‘[’ token
         for (auto [y, w] : adj[x]) {
                   ^
prog.cpp:106:19: error: expected ‘;’ before ‘[’ token
prog.cpp:106:20: error: ‘y’ was not declared in this scope
         for (auto [y, w] : adj[x]) {
                    ^
prog.cpp:106:23: error: ‘w’ was not declared in this scope
         for (auto [y, w] : adj[x]) {
                       ^
prog.cpp: In lambda function:
prog.cpp:106:26: error: expected ‘{’ before ‘:’ token
         for (auto [y, w] : adj[x]) {
                          ^
prog.cpp: In function ‘void solve()’:
prog.cpp:106:26: error: expected ‘;’ before ‘:’ token
prog.cpp:106:26: error: expected primary-expression before ‘:’ token
prog.cpp:106:26: error: expected ‘)’ before ‘:’ token
prog.cpp:106:26: error: expected primary-expression before ‘:’ token
stdout
Standard output is empty