#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define eb emplace_back
#define F first
#define S second
#define test(x) cout << "Test: " << x << '\n'
#define debug puts("qwq");
#define open(x) fresopen(#x".in", "r", stdin);fresopen(#x".out", "w", stdout);
#define close fclose(stdin);fclose(stdout);
namespace FastIO {
template <typename T = int>
inline T read() {
T s = 0, w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
return s * w;
}
template <typename T>
inline void read(T &s) {
s = 0;
int w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
s = s * w;
}
template <typename T, typename... Arp> inline void read(T &x, Arp &...arp) {
read(x), read(arp...);
}
template <typename T>
inline void write(T x, char ch) {
if (x < 0) x = -x, putchar('-');
static char stk[25];
int top = 0;
do {
stk[top++] = x % 10 + '0', x /= 10;
} while (x);
while (top) putchar(stk[--top]);
putchar(ch);
return;
}
template <typename T>
inline void smax(T &x, T y) {
if (x < y) x = y;
}
template <typename T>
inline void smin(T &x, T y) {
if (x > y) x = y;
}
void quit() {
exit(0);
}
} using namespace FastIO;
const int N = 1e5 + 19;
int dfn[N], dep[N], col[N], fa[N], son[N], top[N], siz[N], cnt, now[N], ans[N];
vector<int> g[N], c[22];
void dfs(int u, int f) {
fa[u] = f; dep[u] = dep[f] + 1;
for (const int i : g[u]) {
if (i != f) {
dfs(i, u);
if (siz[i] > siz[son[u]]) son[u] = i;
siz[u] += siz[i];
}
}
}
void dfs2(int u, int f) {
top[u] = f; dfn[u] = ++cnt; now[cnt] = u;
if (son[u]) dfs2(son[u], f);
for (const int i : g[u]) if (i != fa[u] && i != son[u]) dfs2(i, i);
}
void solve() {
int n = read(), q = read();
for (int i = 1; i <= n; ++i) read(col[i]);
for (int i = 1, u, v; i < n; ++i) {
read(u, v);
g[u].eb(v); g[v].eb(u);
}
dfs(1, 0); dfs2(1, 1);
for (int i = 1; i <= n; ++i) {
c[col[now[i]]].eb(i);
}
while (q--) {
int x, y; read(x, y);
for (int i = 1; i <= 20; ++i) ans[i] = 0;
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) swap(x, y);
for (int i = 1; i <= 20; ++i) {
if (!ans[i]) {
int l = 0, r = c[i].size();
while (l < r) {
int mid((l + r) >> 1);
if (c[i][mid] < dfn[top[y]]) l = mid + 1;
else r = mid;
}
if (l != c[i].size() && c[i][l] <= dfn[y]) ans[i] = 1;
}
}
y = fa[top[y]];
}
if (dep[x] > dep[y]) swap(x, y);
for (int i =1 ; i <= 20; ++i) {
if (!ans[i]) {
int l = 0, r = c[i].size();
while (l < r) {
int mid((l + r) >> 1);
if (c[i][mid] < dfn[top[y]]) l = mid + 1;
else r = mid;
}
if (l != c[i].size() && c[i][l] <= dfn[y]) ans[i] = 1;
}
}
int tot = 0;
for (int i =1 ; i <= 20; ++i) tot += ans[i];
write(tot, '\n');
}
}
signed main() {
int t = 1;
while (t--) solve();
return 0;
}
算出来复杂度是 O(c×nlog2n) 的,由于实现成恼贪了所以没过。