SHARE
    TWEET
    Vince14

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

    Sep 29th, 2023
    139
    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. int N, K;
    24. vector<pii> adj[MAXN];
    25. int depth[MAXN];
    26. int par[MAXN][22];
    27. int mine[MAXN][22];
    28. int maxe[MAXN][22];
    29. void make_tree(int c, int p, int dep, int dis){
    30. depth[c] = dep;
    31. if(c != 1){
    32. mine[c][0] = dis;
    33. maxe[c][0] = dis;
    34. par[c][0] = p;
    35. }
    36. for(auto x : adj[c]){
    37. if(x.first != p){
    38. make_tree(x.first, c, dep + 1, x.second);
    39. }
    40. }
    41. }
    42. int LCA(int x, int y){
    43. if(depth[x] < depth[y]){
    44. swap(x, y);
    45. }
    46. if(depth[x] != depth[y]){
    47. int k = depth[x] - depth[y];
    48. for(int i = 0; i <= 20; i++){
    49. if((k & (1 << i)) != 0){
    50. x = par[x][i];
    51. }
    52. }
    53. }
    54. if(x == y){
    55. return x;
    56. }
    57. for(int i = 20; i >= 0; i--){
    58. int xx = par[x][i];
    59. int yy = par[y][i];
    60. if(xx != yy){
    61. x = xx;
    62. y = yy;
    63. }
    64. }
    65. return par[x][0];
    66. }
    67. int main() {
    68. FAST;
    69. memset(mine, 2e9, sizeof(mine));
    70. cin >> N;
    71. for(int x, y, z, i = 0; i < N - 1; i++){
    72. cin >> x >> y >> z;
    73. adj[x].push_back({y, z});
    74. adj[y].push_back({x, z});
    75. }
    76. make_tree(1, 0, 1, 0);
    77. for(int j = 1; j <= 20; j++){
    78. for(int i = 2; i <= N; i++){
    79. par[i][j] = par[par[i][j - 1]][j - 1];
    80. mine[i][j] = min(mine[i][j - 1], mine[par[i][j - 1]][j - 1]);
    81. maxe[i][j] = max(maxe[i][j - 1], maxe[par[i][j - 1]][j - 1]);
    82. }
    83. }
    84. cin >> K;
    85. for(int x, y, i = 0; i < K; i++){
    86. cin >> x >> y;
    87. int lca = LCA(x, y);
    88. int mind = 2e9, maxd = 0;
    89. if(depth[y] != depth[lca]){
    90. int k = depth[y] - depth[lca];
    91. for(int j = 0; j <= 20; j++){
    92. if((k & (1 << j)) != 0){
    93. mind = min(mind, mine[y][j]);
    94. maxd = max(maxd, maxe[y][j]);
    95. y = par[y][j];
    96. }
    97. }
    98. }
    99. if(depth[x] != depth[lca]){
    100. int k = depth[x] - depth[lca];
    101. for(int j = 0; j <= 20; j++){
    102. if((k & (1 << j)) != 0){
    103. mind = min(mind, mine[x][j]);
    104. maxd = max(maxd, maxe[x][j]);
    105. x = par[x][j];
    106. }
    107. }
    108. }
    109. cout << mind << " " << maxd << "\n";
    110. }
    111. }
    112. /*
    113. 9
    114. 1 2 2
    115. 2 3 1
    116. 3 4 5
    117. 2 7 4
    118. 1 5 3
    119. 5 6 1
    120. 5 9 2
    121. 1 8 3
    122. 5
    123. 6 9
    124. 7 8
    125. 9 4
    126. 1 2
    127. 7 3
    128. */
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /