Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define HOISE cerr << "hoise" << endl
  5. #define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
  6. #define READ freopen("alu.txt", "r", stdin);
  7. #define WRITE freopen("vorta.txt", "w", stdout);
  8. #define MP make_pair
  9. #define PB push_back
  10. #define INF 1e9
  11. typedef long long ll;
  12. typedef pair<int, int> pii;
  13.  
  14. const int MAX = 1e5 + 5;
  15. const int MAXLOG = log2(MAX) + 2;
  16.  
  17. int n, m;
  18. vector<pii> G[MAX];
  19. int cost[MAX], par[MAX], d[MAX];
  20. pii P[MAX][MAXLOG];
  21.  
  22. struct edge{
  23.     int u, v, w;
  24.     edge(int u_, int v_, int w_){ u = u_; v = v_; w = w_; }
  25. };
  26. bool operator < (edge x, edge y)
  27. {
  28.     return x.w > y.w;
  29. }
  30.  
  31. inline void init()
  32. {
  33.     for(int i = 0; i < MAX; i++) G[i].clear();
  34.     memset(par, -1, sizeof par);
  35. }
  36. inline void MST()
  37. {
  38.     bool taken[n+5];
  39.     memset(taken, false, sizeof taken);
  40.  
  41.     for(int i = 0; i < MAX; i++)
  42.         cost[i] = INF;
  43.  
  44.     priority_queue<edge> pq;
  45.     pq.push(edge(0, 1, 0));
  46.  
  47.     while(!pq.empty()){
  48.         edge e = pq.top();
  49.         pq.pop();
  50.  
  51.         if(cost[e.v] > e.w){
  52.             cost[e.v] = e.w;
  53.             par[e.v] = e.u;
  54.             taken[e.v] = true;
  55.         }
  56.  
  57.         for(pii nxt : G[e.v]){
  58.             int u = e.v;
  59.             int v = nxt.first;
  60.             int w = nxt.second;
  61.             if(!taken[v] && w < cost[v])
  62.                 pq.push(edge(u, v, w));
  63.         }
  64.     }
  65. }
  66. inline void precalcLCS()
  67. {
  68.     memset(P, -1, sizeof P);
  69.  
  70.     for(int i = 1; i <= n; i++){
  71.         P[i][0].first = par[i];
  72.         P[i][0].second = cost[i];
  73.     }
  74.  
  75.     for(int j = 1; (1<<j) < n; j++)
  76.         for(int i = 1; i <= n; i++)
  77.             if(P[i][j-1].first != -1){
  78.                 P[i][j].first = P[ P[i][j-1].first ][j-1].first;
  79.                 P[i][j].second = max(P[i][j-1].second, P[ P[i][j-1].first ][j-1].second);
  80.             }
  81. }
  82. inline int lcs(int u, int v, int &ans)
  83. {
  84.     if(d[u] > d[v]) swap(u, v);
  85.  
  86.     int dif = d[v] - d[u];
  87.     for(int j = MAXLOG-1; j >= 0 && dif > 0; j--)
  88.         if((1<<j) <= dif){
  89.             dif -= (1<<j);
  90.             ans = max(ans, P[v][j].second);
  91.             v = P[v][j].first;
  92.         }
  93.  
  94.     if(u == v) return u;
  95.  
  96.     for(int j = MAXLOG-1; j >= 0; j--){
  97.         if(P[u][j].first != -1 && P[u][j].first != P[v][j].first){
  98.             ans = max(ans, P[u][j].second);
  99.             ans = max(ans, P[v][j].second);
  100.             u = P[u][j].first;
  101.             v = P[v][j].first;
  102.         }
  103.     }
  104.  
  105.     ans = max(ans, cost[u]);
  106.     ans = max(ans, cost[v]);
  107.     return par[u];
  108. }
  109. void dfs(int u, int depth)
  110. {
  111.     d[u] = depth;
  112.     for(pii nxt : G[u]){
  113.         int v = nxt.first;
  114.         if(v == par[u]) continue;
  115.         if(u == par[v]) dfs(v, depth+1);
  116.     }
  117. }
  118.  
  119. int main()
  120. {
  121. //    READ WRITE
  122.  
  123.     int tc;
  124.     scanf("%d", &tc);
  125.     for(int nc = 1; nc <= tc; nc++){
  126.         init();
  127.  
  128.         scanf("%d %d", &n, &m);
  129.         for(int i = 0; i < m; i++){
  130.             int u, v, w;
  131.             scanf("%d %d %d", &u, &v, &w);
  132.             G[u].PB(MP(v, w));
  133.             G[v].PB(MP(u, w));
  134.         }
  135.  
  136.         MST();
  137.         precalcLCS();
  138.         dfs(1, 0);
  139.  
  140.         printf("Case %d:\n", nc);
  141.  
  142.         int q;
  143.         scanf("%d", &q);
  144.         while(q--){
  145.             int u, v;
  146.             scanf("%d %d", &u, &v);
  147.             int ans = 0;
  148.             int l = lcs(u, v, ans);
  149. //            cerr << "lcs = " << l << endl;
  150.             printf("%d\n", ans);
  151.         }
  152.     }
  153.  
  154.     return 0;
  155. }
  156.  
  157. /*
  158. 1
  159. 6 6
  160. 1 2 73
  161. 1 3 64
  162. 3 4 10
  163. 1 5 36
  164. 2 6 55
  165. 1 3 89
  166. 1
  167. 3 6
  168. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement