#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   const long double pi = acos(-1.0);   const long double eps = 1e-6;   const int INF = 1E9;   const int MAXN = 5111;   struct node {     pair<int, int> key;     int pr, cnt;     node *l, *r;     node(pair<int, int> _key) {       key = _key;       pr = rand();       cnt = 1;       l = r = NULL;     }   };   int cnt(node *p) { return p ? p->cnt : 0; }   void upd_cnt(node *&p) {     if (p) p->cnt = 1 + cnt(p->l) + cnt(p->r);   }   void split(node *p, pair<int, int> key, node *&l, node *&r) {     if (p == NULL) {       l = r = NULL;       return;     }     if (key < p->key) {       split(p->l, key, l, p->l);       r = p;     } else {       split(p->r, key, p->r, r);       l = p;     }     upd_cnt(p);   }   void merge(node *&p, node *l, node *r) {     if (l == NULL)       p = r;     else if (r == NULL)       p = l;     else if (l->pr < r->pr) {       p = l;       merge(l->r, l->r, r);     } else {       p = r;       merge(r->l, l, r->l);     }     upd_cnt(p);   }   void insert(pair<int, int> key, int pr, node *&p) {     if (p == NULL) {       p = new node(key);       p->pr = pr;       return;     }     if (pr > p->pr) {       if (key > p->key)         insert(key, pr, p->r);       else         insert(key, pr, p->l);     } else {       node *t = new node(key);       t->pr = pr;       split(p, key, t->l, t->r);       p = t;     }     upd_cnt(p);   }   void erase(pair<int, int> key, node *&p) {     if (p == NULL) return;     if (p->key == key)       merge(p, p->l, p->r);     else {       if (key < p->key)         erase(key, p->l);       else         erase(key, p->r);     }     upd_cnt(p);   }   int n, m;   vector<int> color, used;   vector<vector<int> > g, queries;   vector<pair<int, int> > q;   vector<node *> root;   vector<map<int, int> > lib;   vector<int> ans, link;   void init() {     color.resize(n);     used.resize(n, 0);     root.resize(n, NULL);     g.resize(n);     queries.resize(n);     q.resize(m);     ans.resize(m);     link.resize(n);     lib.resize(n);   }   void intersect(int vid, int wid, node *&w) {     if (!w) return;     if (w->l) intersect(vid, wid, w->l);     if (w->r) intersect(vid, wid, w->r);     int ckey = w->key.second;     if (lib[link[vid]].find(ckey) == lib[link[vid]].end()) {       lib[link[vid]][ckey] = w->key.first;       insert(w->key, rand(), root[vid]);     } else {       erase(make_pair(lib[link[vid]][ckey], ckey), root[vid]);       w->key.first += lib[link[vid]][ckey];       insert(w->key, rand(), root[vid]);       lib[link[vid]][ckey] = w->key.first;     }     lib[link[wid]].erase(ckey);     delete w;   }   void dfs(int v, int par) {     used[v] = 1;     for (int i = 0; i < (int)(g[v].size()); i++) {       int w = g[v][i];       if (!used[w]) {         dfs(w, v);       }     }     if (v == 0) {       v = v;     }     int maxid = -1;     for (int i = 0; i < (int)(g[v].size()); i++) {       int w = g[v][i];       if (w != par) {         if (maxid == -1 || cnt(root[w]) > cnt(root[maxid])) maxid = w;       }     }     if (maxid != -1) {       link[v] = link[maxid];       root[v] = root[maxid];       for (int i = 0; i < (int)(g[v].size()); i++) {         int w = g[v][i];         if (w != par && w != maxid) {           intersect(v, w, root[w]);         }       }       if (lib[link[v]].find(color[v]) == lib[link[v]].end()) {         lib[link[v]][color[v]] = 1;         insert(make_pair(1, color[v]), rand(), root[v]);       } else {         erase(make_pair(lib[link[v]][color[v]], color[v]), root[v]);         lib[link[v]][color[v]]++;         insert(make_pair(lib[link[v]][color[v]], color[v]), rand(), root[v]);       }     } else {       root[v] = new node(make_pair(1, color[v]));       link[v] = v;       lib[link[v]][color[v]] = 1;     }     node *t1 = NULL, *t2 = NULL;     for (int i = 0; i < queries[v].size(); i++) {       int qid = queries[v][i];       split(root[v], make_pair(q[qid].second, 0), t1, t2);       ans[qid] = cnt(t2);       merge(root[v], t1, t2);     }   }   int main() {     cin >> n >> m;     init();     for (int i = 0; i < (int)(n); i++) cin >> color[i];     for (int i = 0; i < (int)(n - 1); i++) {       int a, b;       scanf( %d%d , &a, &b);       a--, b--;       g[a].push_back(b);       g[b].push_back(a);     }     for (int i = 0; i < (int)(m); i++) {       cin >> q[i].first >> q[i].second;       q[i].first--;       queries[q[i].first].push_back(i);     }     dfs(0, 0);     for (int i = 0; i < (int)(m); i++) printf( %d n , ans[i]);     return 0;   }   