#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	int n, q; cin >> n >> q;
	vector<map<int, int>> mp(n);
	vector<int> p(n,-1);
	auto find = [&](auto &&self, int x) -> int{return (p[x] < 0 ? x : p[x] = self(self,p[x]));};
	auto uni = [&](int x, int y){
		x = find(find,x);
		y = find(find,y);
		if(x != y){
			if(p[x] > p[y]) swap(x,y);
			p[x] += p[y];
			p[y] = x;
			for(auto [i,v]: mp[y]) mp[x][i] += v;
		}
	};
	rep(i,n){
		int c; cin >> c;
		mp[i][c-1] = 1;
	}
	rep(i,q){
		int t, a, b; cin >> t >> a >> b; a--; b--;
		if(t == 1) uni(a,b);
		else cout << mp[find(find,a)][b] << "\n";
	}
}
#include <bits/stdc++.h>
using namespace std;
#define mp(a,b) make_pair(a,b)
#define ff first
#define setp(a) setprecision(a)<<fixed
#define ss second
#define fori(v) for(ll i=0; i<v; i++)
#define forj(v) for(ll j=0; j<v; j++)
#define fork(v) for(ll k=0; k<v; k++)
#define forl(v) for(ll l=0; l<v; l++)
#define fort(v) for(ll t=0; t<v; t++)
#define forz(v) for(ll z=0; z<v; z++)
#define forx(v) for(ll x=0; x<v; x++)
#define fory(v) for(ll y=0; y<v; y++)
#define ll long long
#define ld long double
#define pb(a) push_back(a)
#define MAX (int)(pow(10,6) + 10)
const ll INF = 0x3f3f3f3f;
const ll inf = pow(10,18);
ll modulo = pow(10,9)+7;


vector<ll> g[MAX];
ll dp[MAX][2];
ll num[MAX][2];

void dfs(ll hd, ll pr){
	dp[hd][0] = 0;
	num[hd][0] = hd;
	
	dp[hd][1] = -inf;
	num[hd][1] = -1;
	
	for(auto& hr : g[hd]){
		if(hr == pr){
			continue;
		}
		dfs(hr, hd);
		
		if(dp[hr][0] + 1 > dp[hd][0]){
			dp[hd][1] = dp[hd][0];
			num[hd][1] = num[hd][0];
			dp[hd][0] = dp[hr][0] + 1;
			num[hd][0] = num[hr][0];
		}
		else if(dp[hr][0] + 1 > dp[hd][1]){
			dp[hd][1] = dp[hr][0] + 1;
			num[hd][1] = num[hr][0];
		}
		
	}
}

ll tin[MAX];
ll tout[MAX];

void dfsForT(ll hd, ll pr,ll& tim){
	tin[hd] = tim;
	for(auto& hr: g[hd]){
		if(hr == pr){
			continue;
		}
		++tim;
		dfsForT(hr, hd, tim);
	}
	tout[hd] = tim;
}

ll val[MAX];

void dfsFinal(ll hd, ll pr, ll& tar, ll& tim){
	val[hd] = tim;
	
	ll lz = -1;
	
	for(auto& hr: g[hd]){
		if(hr == pr){
			continue;
		}
		if(tin[hr]  <= tin[tar] && tout[hr] >= tin[tar] ){
			lz = hr;
		}
		else{
			++tim;
			dfsFinal(hr, hd, tar, tim);
		}
	}
	
	if(lz!=-1){
		++tim;
		dfsFinal(lz, hd, tar, tim);
	}
	
	++tim;
}

void deal(){
	ll n;
	cin>>n;
	fori(n-1){
		ll ai, bi;
		cin>>ai>>bi;
		--ai, --bi;
		g[ai].pb(bi);
		g[bi].pb(ai);
	}
	
	dfs(0, -1);
	
	ll lz = 0;
	fori(n){
		if(dp[i][0] + dp[i][1] > dp[lz][0] + dp[lz][1]){
			lz = i;
		}
	}
	
	ll root = num[lz][0];
	ll tar = num[lz][1];
	
//	cout<<"root is "<<root+1<<" target is "<<tar+1<<" sum is "<<dp[lz][0]+dp[lz][1]<<" lz is "<<lz+1<<endl;
	
	ll tim = 0;
	
	dfsForT(root, -1, tim);
	
	tim = 1;
	
	dfsFinal(root, -1, tar, tim);
	
	
	fori(n){
		cout<<val[i]<<' ';
	}
	
}	
 
int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	deal();
}













