Advertisement
Abrar_Al_Samit

Milk Visits (USACO 2019 Dec Gold)

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