Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #define debug(x) cerr << '[' << (#x) << "] = " << x << '\n';
- template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
- const int maxn = 1e5 + 5, l = 20;
- vector<int>T(maxn), g[maxn], level(maxn), tin(maxn), tout(maxn);
- vector<pair<int,pair<int,int>>>asking[maxn];
- vector<bool>ok(maxn, false);
- vector<vector<int>>up(maxn, vector<int>(l+1));
- stack<int> st[maxn];
- int getLCA(int u, int v) {
- auto isAncestor = [=] (int u, int v) {
- return tin[u] <= tin[v] && tout[u] >= tout[v];
- };
- if(isAncestor(u, v)) return u;
- if(isAncestor(v, u)) return v;
- for(int i=l; i>-1; --i) if(!isAncestor(up[u][i], v)) u = up[u][i];
- return up[u][0];
- }
- void DFS(int node, int parent, int lv) {
- level[node] = lv;
- st[T[node]].push(lv);
- for(auto it : asking[node]) {
- if(st[it.second.second].empty()) continue;
- if(level[getLCA(node, it.second.first)] <= st[it.second.second].top())
- ok[it.first] = true;
- }
- for(auto it : g[node]) if(it!=parent) DFS(it, node, lv+1);
- st[T[node]].pop();
- }
- void PlayGround() {
- int N, M;
- cin >> N >> M;
- for(int i=1; i<=N; ++i) {
- cin >> T[i];
- }
- for(int i=1; i<N; ++i) {
- int u, v; cin >> u >> v;
- g[u].push_back(v);
- g[v].push_back(u);
- }
- for(int i=0; i<M; ++i) {
- int u, v, c; cin >> u >> v >> c;
- asking[u].push_back(make_pair(i, make_pair(v, c)));
- asking[v].push_back(make_pair(i, make_pair(u, c)));
- }
- int timer = 0;
- function<void(int,int)> Prepare = [&] (int node, int parent) {
- tin[node] = timer++;
- up[node][0] = parent;
- for(int i=1; i<=l; ++i) {
- up[node][i] = up[up[node][i-1]][i-1];
- }
- for(auto it : g[node]) if(it!=parent) Prepare(it, node);
- tout[node] = timer++;
- };
- Prepare(1, 1);
- DFS(1, 1, 0);
- for(int i=0; i<M; ++i) cout << ok[i];
- cout << '\n';
- #ifndef ONLINE_JUDGE
- cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
- #endif
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- #ifndef ONLINE_JUDGE
- //freopen("input.txt", "r", stdin);
- freopen("milkvisits.in", "r", stdin);
- freopen("milkvisits.out", "w", stdout);
- #endif
- PlayGround();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement