Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.5 kB
2
Indexable
Never
#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;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define db double
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define speed ios_base::sync_with_stdio(false);cin.tie(0), cout.tie(0);
/*
                    " وَأَن لَّيْسَ لِلْإِنسَانِ إِلَّا مَا سَعَى ﴿39﴾ وَأَنَّ سَعْيَهُ سَوْفَ يُرَى ﴿40﴾ ثُمَّ يُجْزَاهُ الْجَزَاء الْأَوْفَى "
*/
const ll N = 1e6 + 5, inf = 2e18, mod = 1e9 + 7;
ll dx[] = {0, 0, -1, 1, -1, 1, 1, -1};
ll dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
char di[] = {'R', 'L', 'U', 'D'};
ll n, m;
vector<ll> adj[N], vis[N], q[N], col(N), parent(N), depth(N), ans(N);
struct item {
    ll from, to, c;
};
vector<item> arr(N);
int up[N][22];

void pre(ll node, ll p, ll d) {
    depth[node] = d;
    parent[node] = p;
    for (auto it: adj[node]) {
        if (it != p)
            pre(it, node, d + 1);
    }
}

void con() {
    for (int i = 0; i < 22; ++i) {
        for (int node = 1; node <= n; ++node) {
            if (!i)
                up[node][0] = parent[node];
            else
                up[node][i] = up[up[node][i - 1]][i - 1];
        }
    }
}

int binlift(ll node, ll u) {
    for (int b = 0; b < 22; ++b) {
        if (u & (1 << b))
            node = up[node][b];
    }
    return node;
}

int lca(ll a, ll b) {
    if (depth[a] < depth[b])
        swap(a, b);
    a = binlift(a, depth[a] - depth[b]);
    if (a == b)
        return a;
    for (int bit = 21; bit >= 0; --bit) {
        if (up[a][bit] == up[b][bit])
            continue;
        a = up[a][bit];
        b = up[b][bit];
    }
    return up[a][0];
}

void dfs(ll node, ll p) {
    vis[col[node]].push_back(depth[node]);
    for (auto idx: q[node]) {
        ll u = arr[idx].from, v = arr[idx].to, c = arr[idx].c, lc = lca(u, v);
        if (u != node)
            swap(v, u);
        ll x = vis[c].size(),y;
        if (x)
             y = vis[c].back();
        if (vis[c].size() and vis[c].back() >= depth[lc])
            ans[idx] = 1;
    }
    for (auto it: adj[node]) {
        if (it != p)
            dfs(it, node);
    }
    vis[col[node]].pop_back();
}

void solve() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> col[i];
    for (int i = 1; i < n; ++i) {
        ll a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    pre(1, 0, 0);
    con();


    for (int i = 0; i < m; i++) {
        cin >> arr[i].from >> arr[i].to >> arr[i].c;
        q[arr[i].from].push_back(i);
        q[arr[i].to].push_back(i);
    }

    dfs(1, 0);

    for (int i = 0; i < m; i++)
        cout << ans[i];
}

void file() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int main() {
    speed
//    file();
    freopen("milkvisits.in", "r", stdin);
    freopen("milkvisits.out", "w", stdout);
    int testcases = 1;
//    cin >> testcases;
    while (testcases--)
        solve();

}
Leave a Comment