Guest User

c.cpp

a guest
Feb 5th, 2026
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define f first
  4. #define s second
  5. #define pb push_back
  6. #define ll long long
  7.  
  8. using namespace std;
  9. const int MAXN = 1e6 + 10;
  10. const ll INF = 1e18 + 7;
  11. bool was[MAXN];
  12. vector <pair <int, int> > g[MAXN];
  13. int up[MAXN][23];
  14. ll d[MAXN];
  15. int tin[MAXN], tout[MAXN], l, n, s, t;
  16. bool upper(int a, int b){
  17.     return (tin[a] <= tin[b] && tout[a] >= tout[b]);
  18. }
  19.  
  20. int timer = 0;
  21. void dfs(int x, ll dis = 0, int p = 0){
  22.     was[x] = 1;
  23.     tin[x] = ++timer;
  24.     up[x][0] = p;
  25.     d[x] = dis;
  26.     for (int i = 1; i <= l; i++){
  27.         up[x][i] = up[up[x][i - 1]][i - 1];
  28.     }
  29.  
  30.     for (int i = 0; i < g[x].size(); i++){
  31.         int to = g[x][i].f;
  32.         int len = g[x][i].s;
  33.         if (!was[to]){
  34.             dfs(to, dis + len, x);
  35.         }
  36.     }
  37.     tout[x] = ++timer;
  38. }
  39.  
  40. int lca(int a, int b){
  41.     if (upper(a, b))
  42.         return a;
  43.     if (upper(b, a))
  44.         return b;
  45.  
  46.     for (int i = l; i >= 0; i--){
  47.         if (!upper(up[a][i], b))
  48.             a = up[a][i];
  49.     }
  50.  
  51.     return up[a][0];
  52. }
  53.  
  54. ll dist(int x, int y){
  55.     return d[x] + d[y] - 2 * d[lca(x, y)];
  56. }
  57.  
  58. bool check(int u, int v, int a1, int b1){
  59.     int lcauv = lca(u, v);
  60.  
  61.     if (upper(a1, lcauv) && upper(b1, lcauv)){
  62.         return 1;
  63.     }
  64.     if (upper(a1, u) && upper(b1, u)){
  65.         return false;
  66.     }
  67.     if (upper(a1, v) && upper(b1, v)){
  68.         return false;
  69.     }
  70.     return true;
  71. }
  72.  
  73. void solve(){
  74.     tout[0] = 1e9 + 7;
  75.     l = 0;
  76.     while ((1 << l) < MAXN){
  77.         l++;
  78.     }
  79.  
  80.     cin >> n >> s >> t;
  81.  
  82.     for (int i = 1; i < n; i++){
  83.         int x, y, cost;
  84.         cin >> x >> y >> cost;
  85.         g[x].pb({y, cost});
  86.         g[y].pb({x, cost});
  87.     }
  88.     dfs(1);
  89.     int q;
  90.     cin >> q;
  91.     while (q--){
  92.         int a1, b1, a2, b2, cost2;
  93.         cin >> a1 >> b1 >> a2 >> b2 >> cost2;
  94.         ll new_dist = INF;
  95.         if (check(s, t, a1, b1)){
  96.             new_dist = min(new_dist, dist(s, t));
  97.         }
  98.         if (check(s, a2, a1, b1) && check(b2, t, a1, b1)){
  99.             new_dist = min(new_dist, dist(s, a2) + dist(b2, t) + cost2);
  100.         }
  101.         if (check(s, b2, a1, b1) && check(a2, t, a1, b1)){
  102.             new_dist = min(new_dist, dist(s, b2) + dist(a2, t) + cost2);
  103.         }
  104.  
  105.         if (new_dist == INF){
  106.             cout << -1 << '\n';
  107.         }
  108.         else {
  109.             cout << new_dist << '\n';
  110.         }
  111.     }
  112. }
  113.  
  114. int main() {
  115.  
  116.     ios::sync_with_stdio(0);
  117.     cin.tie(nullptr);
  118.     cout.tie(nullptr);
  119.  
  120.     int ttt = 1;
  121.     //cin >> ttt;
  122.  
  123.     while (ttt--){
  124.         solve();
  125.     }
  126.     return 0;
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment