algorithm_2024/src/20241116/U498412.cpp
2024-11-16 09:36:34 +08:00

48 lines
940 B
C++

#include <cstdint>
#include <iostream>
#include <istream>
#include <set>
#include <vector>
using ll = int64_t;
using std::cin, std::cout;
const ll max_n = 1e5+5, rt{1};
ll n, q, c[max_n], fts[max_n];
std::vector<ll> edges[max_n];
void fd_ft(const ll &ft, const ll &now){
fts[now] = ft;
for(const ll &nxt: edges[now]){
if(nxt==ft)continue;
fd_ft(now, nxt);
}
}
int main(){
std::iostream::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin>>n>>q;
for(ll i{1};i<=n;i++){
cin>>c[i];
}
for(ll i{1};i<n;i++){
ll u, v;
cin>>u>>v;
edges[u].push_back(v);
edges[v].push_back(u);
}
fd_ft(0, rt);
for(ll i{1};i<=q;i++){
ll u, v;
cin>>u>>v;
std::set<ll> pts;
pts.emplace(c[u]);
while(v!=u){
pts.emplace(c[v]);
v=fts[v];
}
cout<<pts.size()<<'\n';
}
}