Advertisement
Abrar_Al_Samit

Trucks (UVA 12655)

Jul 11th, 2021
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8.  
  9. #define debug(x) cerr << '[' << (#x) << "] = " << x << '\n';
  10.  
  11. template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
  12. const int maxn = 2e4 + 4;
  13. int sz[maxn], parent[maxn];
  14. void PlayGround(int N) {
  15.     int M, S;
  16.     cin >> M >> S;
  17.     for(int i=1; i<=N; ++i) {
  18.         sz[i] = 1, parent[i] = i;
  19.     }
  20.     vector<vector<pair<int,int>>>MST(N+1, vector<pair<int,int>>());
  21.     vector<pair<int,pair<int,int>>>Edges(M);
  22.     for(auto& it : Edges) {
  23.         cin >> it.second.first >> it.second.second >> it.first;
  24.     }
  25.     function<int(int)> find_set = [&] (int v) {
  26.         if(parent[v]==v)
  27.             return v;
  28.         return parent[v] = find_set(parent[v]);
  29.     };
  30.     auto Unite = [&] (int u, int v) {
  31.         u = find_set(u);
  32.         v = find_set(v);
  33.         if(sz[u]<sz[v]) swap(u, v);
  34.         sz[u] += sz[v];
  35.         parent[v] = u;
  36.     };
  37.     sort(Edges.rbegin(), Edges.rend());
  38.     for(auto it : Edges) {
  39.         int u = it.second.first, v = it.second.second, w = it.first;
  40.         if(find_set(u)!=find_set(v)) {
  41.             Unite(u, v);
  42.             MST[u].push_back(make_pair(v, w));
  43.             MST[v].push_back(make_pair(u, w));
  44.         }
  45.     }
  46.     int l = 20, timer = 0;
  47.     vector<vector<int>>up(N+1, vector<int>(l+1)), mn(N+1, vector<int>(l+1));
  48.     vector<int>tin(N+1), tout(N+1);
  49.     function<void(int,int,int)> DFS = [&] (int node, int parent, int toParent) {
  50.         tin[node] = timer++;
  51.         up[node][0] = parent;
  52.         mn[node][0] = toParent;
  53.         for(int i=1; i<=l; ++i) {
  54.             up[node][i] = up[up[node][i-1]][i-1];
  55.             mn[node][i] = min(mn[up[node][i-1]][i-1], mn[node][i-1]);
  56.         }
  57.         for(auto it : MST[node]) if(it.first!=parent) {
  58.             DFS(it.first, node, it.second);
  59.         }
  60.         tout[node] = timer++;
  61.     };
  62.     DFS(1, 1, INT_MAX);
  63.  
  64.     auto isAncestor = [=] (int u, int v) {
  65.         return tin[u] <= tin[v] && tout[u] >= tout[v];
  66.     };
  67.     auto ClimbToLCA = [=] (int u, int v, int& answer) {
  68.         for(int i=l; i>=0; --i) if(!isAncestor(up[u][i], v)) {
  69.             answer = min(answer, mn[u][i]);
  70.             u = up[u][i];
  71.         }
  72.         if(!isAncestor(u, v)) {
  73.             answer = min(answer, mn[u][0]);
  74.         }
  75.     };
  76.     while(S--) {
  77.         int u, v; cin >> u >> v;
  78.         int answer = INT_MAX;
  79.         ClimbToLCA(u, v, answer);
  80.         ClimbToLCA(v, u, answer);
  81.         cout << answer << '\n';
  82.     }
  83.  
  84.     #ifndef ONLINE_JUDGE
  85.         cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  86.     #endif
  87. }
  88. int main() {
  89.     ios_base::sync_with_stdio(0);
  90.     cin.tie(0);
  91.     cout.tie(0);
  92.     #ifndef ONLINE_JUDGE
  93.         freopen("input.txt", "r", stdin);
  94.     #endif
  95.     int N;
  96.     while(cin >> N) {
  97.         PlayGround(N);
  98.     }
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement