Advertisement
Guest User

Untitled

a guest
May 26th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:67108864")
  2. #include <vector>
  3. #include <list>
  4. #include <iostream>
  5. #include <algorithm>
  6. const int MAX_N = 200001;
  7. int n, logn;
  8. std::vector<int> parent(MAX_N);
  9. std::vector<int> depth_level(MAX_N);
  10. std::vector<std::vector<int>> ancestor(MAX_N);
  11. std::vector<std::vector<int>> distance(MAX_N);
  12.  
  13. void preprocess_dfs(int node, int parent,int depth, int dist_to_parent, std::vector<std::list<std::pair<int, int>>> &adjacency_list)
  14. {
  15.     depth_level[node] = depth;
  16.     ancestor[node][0] = parent;
  17.     distance[node][0] = dist_to_parent;
  18.     for(int i=1;i<logn;i++)
  19.         ancestor[node][i] = ancestor[ancestor[node][i - 1]][i - 1];
  20.     for(int i=1; i<=logn;i++)
  21.         distance[node][i] = std::max(distance[node][i - 1], distance[ancestor[node][i - 1]][i - 1]);
  22.     for (auto neighbour : adjacency_list[node])
  23.         if (neighbour.first != parent)
  24.             preprocess_dfs(neighbour.first, node, depth + 1, neighbour.second, adjacency_list);
  25. }
  26.  
  27.  
  28. int lca(int v, int u)
  29. {
  30.  
  31.     int max_distance = INT_MIN;
  32.  
  33.     if (v == u)
  34.         return  0;
  35.  
  36.     if (depth_level[v] > depth_level[u])
  37.         std::swap(v, u);
  38.  
  39.     int depth_diff = depth_level[u] - depth_level[v];
  40.     int pow = 1 << logn;
  41.     int i = logn;
  42.     for (; i >= 0;)
  43.     {
  44.         if (pow <= depth_diff)
  45.         {
  46.             max_distance = std::max(max_distance, distance[u][i]);
  47.             u = ancestor[u][i];
  48.             depth_diff = depth_level[u] - depth_level[v];
  49.         }
  50.         pow = pow >> 1;
  51.         i--;
  52.     }
  53.  
  54.     if(u != v)
  55.     {
  56.         int j = logn;
  57.         while(j>=0)
  58.         {
  59.             if(ancestor[u][j]!=ancestor[v][j])
  60.             {
  61.                 max_distance = std::max(max_distance, distance[u][j]);
  62.                 max_distance = std::max(max_distance, distance[v][j]);
  63.                 u = ancestor[u][j];
  64.                 v = ancestor[v][j];
  65.  
  66.             }
  67.             j--;
  68.         }
  69.         if (u != v)
  70.         {
  71.             max_distance = std::max(max_distance, distance[u][0]);
  72.             max_distance = std::max(max_distance, distance[v][0]);
  73.         }
  74.  
  75.     }
  76.     return max_distance;
  77.  
  78.  
  79. }
  80.  
  81.  
  82. int main()
  83. {
  84.     std::ios_base::sync_with_stdio(false);
  85.     freopen("input.txt", "r", stdin);
  86.     freopen("output.txt", "w", stdout);
  87.     int  a, b;
  88.     int cost;
  89.  
  90.     std::cin >> n;
  91.  
  92.     int pow = 1;
  93.     logn = 1;
  94.     while (pow <= n) {
  95.         pow *= 2;
  96.         logn++;
  97.     }
  98.  
  99.     for(int i=0;i<n;i++)
  100.     {
  101.         ancestor[i].resize(logn + 1, 0);
  102.         distance[i].resize(logn + 1, INT_MIN);
  103.     }
  104.  
  105.     std::vector< std::list< std::pair<int,int>>> adjacency_list(n);
  106.     for (int i = 0; i<n - 1; i++)
  107.     {
  108.         scanf("%d %d %d", &a, &b, &cost);
  109.         adjacency_list[a - 1].push_back(std::make_pair(b - 1,cost));
  110.         adjacency_list[b - 1].push_back(std::make_pair(a - 1,cost));
  111.     }
  112.  
  113.     preprocess_dfs(0, 0, 0, INT_MIN,adjacency_list);
  114.  
  115.     int query_num;
  116.     std::cin >> query_num;
  117.     for(int i=0;i<query_num; i++)
  118.     {
  119.         scanf("%d %d", &a, &b);
  120.         std::cout << lca(a - 1, b - 1) << std::endl;
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement