fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct pack { int u, c; };
  6. typedef vector<vector<pack>> dsk;
  7.  
  8. int d[1001], bac[1001], cha[1001];
  9.  
  10. void dfs(int u, const dsk &ke) {
  11. for (pack p: ke[u]) if (p.u != cha[u]) {
  12. int v = p.u, c = p.c;
  13. d[v] = c;
  14. cha[v] = u;
  15. bac[v] = bac[u] + 1;
  16. dfs(v, ke);
  17. }
  18. }
  19.  
  20. int solve(int u, int v) {
  21. #define up(u) sum += d[u], u = cha[u]
  22. int sum = 0;
  23. while (bac[u] > bac[v]) up(u);
  24. while (bac[v] > bac[u]) up(v);
  25. while (u != v) up(u), up(v);
  26. return sum;
  27. }
  28.  
  29. int main() {
  30. ios::sync_with_stdio(false); cin.tie(0);
  31. int n, q; cin >> n >> q;
  32. dsk ke(n+1);
  33. for (int i=1; i<n; i++) {
  34. int u, v, c;
  35. cin >> u >> v >> c;
  36. ke[u].push_back({v, c});
  37. ke[v].push_back({u, c});
  38. }
  39. dfs(1, ke);
  40. while (q--) {
  41. int u, v; cin >> u >> v;
  42. cout << solve(u, v) << '\n';
  43. }
  44. return 0;
  45. }
  46.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6: error: 'dsk' was not declared in this scope
prog.cpp:6: error: '>>' should be '> >' within a nested template argument list
prog.cpp: In function 'void dfs(int, const dsk&)':
prog.cpp:11: error: a function-definition is not allowed here before ':' token
prog.cpp:18: error: expected primary-expression before '}' token
prog.cpp:18: error: expected `;' before '}' token
prog.cpp:18: error: expected primary-expression before '}' token
prog.cpp:18: error: expected `)' before '}' token
prog.cpp:18: error: expected primary-expression before '}' token
prog.cpp:18: error: expected `;' before '}' token
prog.cpp: In function 'int main()':
prog.cpp:36: error: expected primary-expression before '{' token
prog.cpp:37: error: expected primary-expression before '{' token
stdout
Standard output is empty