1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| #include<bits/stdc++.h> using namespace std; #define rep(i,from,to) for(int i=(int)(from);i<=(int)(to);++i) #define rev(i,from,to) for(int i=(int)(from);i>=(int)(to);--i) #define For(i,to) for(int i=0;i<(int)(to);++i) #define see(x) (cerr<<(#x)<<'='<<(x)<<endl) void dbg() {cout << "\n";} template<typename T, typename... A> void dbg(T a, A... x) {cout << a << ' '; dbg(x...);} #define logs(x...) {cout << #x << " -> "; dbg(x);} typedef long long ll; typedef long double ld; inline ll read(){ ll x=0; ll sign=1; char c=getchar(); while(c>'9' c<'0') {if (c=='-') sign=-1;c=getchar();} while(c>='0' && c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();} return x*sign; } const int N = 212345; int n, m; struct Query{ int q, a;
void output(){ printf("%d\n", a); } };
vector<int> son[N]; int fa[N]; int hson[N], sz[N]; int c[N]; vector<Query> query; vector<int> q[N];
void dfs1(int u){ sz[u]=1; for(int v:son[u]) if(v!=fa[u]){ fa[v]=u; dfs1(v); sz[u]+=sz[v]; if(sz[v]>sz[hson[u]])hson[u]=v; } }
struct TreeSolve{ map<int, int> cnt; map<int, int> cntt;
void add(int u, int skip, int val) { if(u == skip) return;
if (val == -1) cntt[cnt[c[u]]]--; cnt[c[u]] += val; if (val == 1) cntt[cnt[c[u]]]++;
for(int v : son[u]) if (v!=fa[u]){ add(v, skip, val); } }
void get_ans(int u) { for(int i : q[u]) { int k = query[i].q; query[i].a = cntt[k]; } }
void del(int u) { add(u, 0, -1); }
}TS;
void dfs2(int u, bool keep) { for(int v : son[u]) if (v != hson[u] && v != fa[u]) { dfs2(v, 0); } if (hson[u]) dfs2(hson[u], 1); TS.add(u, hson[u], 1); TS.get_ans(u); if (!keep) TS.del(u); }
int main() { n = read(), m = read(); rep(i, 1, n) c[i] = read(); rep(i, 1, n - 1) { int x = read(), y = read(); son[x].push_back(y); son[y].push_back(x); } rep(i, 1, m) { int v = read(), k = read(); query.push_back((Query){k, 0}); q[v].push_back(query.size() - 1); } dfs1(1); dfs2(1, 1);
for(auto &q : query) { q.output(); }
return 0; }
|