Abrar_Al_Samit

DISQUERY(SPOJ)

Jul 7th, 2021 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 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.  
  13. const int maxn = 1e5 + 5;
  14. vector<pair<int,int>>g[maxn];
  15. void PlayGround() {
  16.     int N; cin >> N;
  17.     for(int i=1; i<N; ++i) {
  18.         int u, v; cin >> u >> v;
  19.         int c; cin >> c;
  20.         g[u].push_back(make_pair(v, c));
  21.         g[v].push_back(make_pair(u, c));
  22.     }
  23.     int l = 20, timer = 0;
  24.     vector<int>tin(N+1), tout(N+1);
  25.     vector<vector<int>>up(N+1, vector<int>(l)), mx(N+1, vector<int>(l)), mn(N+1, vector<int>(l));
  26.     function<void(int, int, int)> DFS = [&] (int node, int parent, int toParent) {
  27.         tin[node] = timer++;
  28.  
  29.         up[node][0] = parent;
  30.         mx[node][0] = node==parent ? INT_MIN : toParent;
  31.         mn[node][0] = node==parent ? INT_MAX : toParent;
  32.         for(int i=1; i<l; ++i) {
  33.             up[node][i] = up[up[node][i-1]][i-1];
  34.             mx[node][i] = max(mx[node][i-1], mx[up[node][i-1]][i-1]);
  35.             mn[node][i] = min(mn[node][i-1], mn[up[node][i-1]][i-1]);
  36.         }
  37.        
  38.         for(auto p : g[node]) if(p.first!=parent) {
  39.             DFS(p.first, node, p.second);
  40.         }
  41.  
  42.         tout[node] = timer++;
  43.     };
  44.     DFS(1, 1, 0);
  45.     auto isAncestor = [=] (int u, int v) {
  46.         return tin[u] <= tin[v] && tout[u] >= tout[v];
  47.     };
  48.     int q; cin >> q;
  49.     while(q--) {
  50.         int u, v; cin >> u >> v;
  51.         int mxEdge = INT_MIN;
  52.         int mnEdge = INT_MAX;
  53.         auto moveToLCA = [&] (int u, int v) {
  54.             for(int i=l-1; i>=0; --i) {
  55.                 if(isAncestor(up[u][i], v)==false) {
  56.                     mxEdge = max(mx[u][i], mxEdge);
  57.                     mnEdge = min(mn[u][i], mnEdge);
  58.                     u = up[u][i];
  59.                 }
  60.             }
  61.             if(isAncestor(u, v)==false) {
  62.                 mxEdge = max(mx[u][0], mxEdge);
  63.                 mnEdge = min(mn[u][0], mnEdge);
  64.                 u = up[u][0];
  65.             }
  66.         };
  67.         moveToLCA(u, v);
  68.         moveToLCA(v, u);
  69.         cout << mnEdge << ' ' << mxEdge << '\n';
  70.     }
  71.  
  72.     #ifndef ONLINE_JUDGE
  73.         cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  74.     #endif
  75. }
  76. int main() {
  77.     ios_base::sync_with_stdio(0);
  78.     cin.tie(0);
  79.     cout.tie(0);
  80.     #ifndef ONLINE_JUDGE
  81.         freopen("input.txt", "r", stdin);
  82.     #endif
  83.     PlayGround();
  84.  
  85.     return 0;
  86. }
Add Comment
Please, Sign In to add comment