Advertisement
Vince14

/<> 3176 (LCA with two extra dp min/max arrays)

Sep 29th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <numeric>
  14. #include <iomanip>
  15. using namespace std;
  16. #define pii pair<int , int>
  17. #define ll long long
  18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
  19. const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  20. const long long dl[2] = {1, -1};
  21. const long long MOD = 1000000007;
  22. const long long MAXN = 100005;
  23.  
  24. int N, K;
  25. vector<pii> adj[MAXN];
  26. int depth[MAXN];
  27. int par[MAXN][22];
  28. int mine[MAXN][22];
  29. int maxe[MAXN][22];
  30.  
  31. void make_tree(int c, int p, int dep, int dis){
  32.     depth[c] = dep;
  33.     if(c != 1){
  34.         mine[c][0] = dis;
  35.         maxe[c][0] = dis;
  36.         par[c][0] = p;
  37.     }
  38.     for(auto x : adj[c]){
  39.         if(x.first != p){
  40.             make_tree(x.first, c, dep + 1, x.second);
  41.         }
  42.     }
  43. }
  44.  
  45. int LCA(int x, int y){
  46.     if(depth[x] < depth[y]){
  47.         swap(x, y);
  48.     }
  49.     if(depth[x] != depth[y]){
  50.         int k = depth[x] - depth[y];
  51.         for(int i = 0; i <= 20; i++){
  52.             if((k & (1 << i)) != 0){
  53.                 x = par[x][i];
  54.             }
  55.         }
  56.     }
  57.     if(x == y){
  58.         return x;
  59.     }
  60.     for(int i = 20; i >= 0; i--){
  61.         int xx = par[x][i];
  62.         int yy = par[y][i];
  63.         if(xx != yy){
  64.             x = xx;
  65.             y = yy;
  66.         }
  67.     }
  68.     return par[x][0];
  69. }
  70.  
  71.  
  72. int main() {
  73.     FAST;
  74.     memset(mine, 2e9, sizeof(mine));
  75.     cin >> N;
  76.     for(int x, y, z, i = 0; i < N - 1; i++){
  77.         cin >> x >> y >> z;
  78.         adj[x].push_back({y, z});
  79.         adj[y].push_back({x, z});
  80.     }
  81.     make_tree(1, 0, 1, 0);
  82.     for(int j = 1; j <= 20; j++){
  83.         for(int i = 2; i <= N; i++){
  84.             par[i][j] = par[par[i][j - 1]][j - 1];
  85.             mine[i][j] = min(mine[i][j - 1], mine[par[i][j - 1]][j - 1]);
  86.             maxe[i][j] = max(maxe[i][j - 1], maxe[par[i][j - 1]][j - 1]);
  87.         }
  88.     }
  89.     cin >> K;
  90.     for(int x, y, i = 0; i < K; i++){
  91.         cin >> x >> y;
  92.         int lca = LCA(x, y);
  93.         int mind = 2e9, maxd = 0;
  94.         if(depth[y] != depth[lca]){
  95.             int k = depth[y] - depth[lca];
  96.             for(int j = 0; j <= 20; j++){
  97.                 if((k & (1 << j)) != 0){
  98.                     mind = min(mind, mine[y][j]);
  99.                     maxd = max(maxd, maxe[y][j]);
  100.                     y = par[y][j];
  101.                 }
  102.             }
  103.         }
  104.         if(depth[x] != depth[lca]){
  105.             int k = depth[x] - depth[lca];
  106.             for(int j = 0; j <= 20; j++){
  107.                 if((k & (1 << j)) != 0){
  108.                     mind = min(mind, mine[x][j]);
  109.                     maxd = max(maxd, maxe[x][j]);
  110.                     x = par[x][j];
  111.                 }
  112.             }
  113.         }
  114.         cout << mind << " " << maxd << "\n";
  115.     }
  116. }
  117.  
  118. /*
  119. 9
  120. 1 2 2
  121. 2 3 1
  122. 3 4 5
  123. 2 7 4
  124. 1 5 3
  125. 5 6 1
  126. 5 9 2
  127. 1 8 3
  128. 5
  129. 6 9
  130. 7 8
  131. 9 4
  132. 1 2
  133. 7 3
  134.  */
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement