#define ii pair<int , int>

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;

vector<ii> edge[N];
int label[N];

bool check[N];

void dfs(int u)
{
    for (auto adj : edge[u])
    {
        int v = adj.first;
        int w = adj.second;

        if (label[v] == 0)
        {
            if (label[u] == w)
            {
                if (w == 1) label[v] = 2;
                else label[v] = 1;
            }
            else
            {
                label[v] = w;
            }
            dfs(v);
        }
    }
}

int main()
{
    // freopen(".inp","r",stdin);
    // freopen(".out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n , m;
    cin >> n >> m;

    while(m--)
    {
        int u , v , w;
        cin >> u >> v >> w;
        edge[u].push_back(ii(v , w));
        edge[v].push_back(ii(u , w));
    }

    label[1] = 1;
    dfs(1);

    for (int i = 1 ; i <= n ; ++i) cout << label[i] << "\n";

}

#define _USE_MATH_DEFINES
#include  <iostream>
#include  <algorithm>
#include  <climits>
#include  <vector>
#include  <string>
#include  <cstring>
#include  <stack>
#include  <queue>
#include  <cmath>
#include  <iomanip>
#include  <set>
#include  <map>
#include  <new>
#include <cstdint>
#include <cctype>
#include  <cstdio>
#include  <sstream>
#include  <bitset>
#include <fstream>
#include <ctime>
using namespace std;
#define rep(i,n)  for(int i = 0; i < n; i++)
#define lrep(i,n,m)  for(int i = n; i < m; i++)
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using Vii = vector<vector<int> >;
using Viii = vector<vector<vector <int> > >;
using Gjudge = vector<vector<bool>>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vd = vector<double>;
using Vld = vector<long double>;
using Vll = vector<vector<ll> >;
using Vlll = vector<vector<vector<ll> > >;
using Vdd = vector<vector<double> >;
using Vldd = vector<vector<ld> >;
using Vs = vector<string>;
using Vb = vector<bool>;
using Vbb = vector<vector<bool> >;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Pli = pair<ll, int>;
using VPi = vector<Pi>;
using VPl = vector<Pl>;
using VPli = vector<Pli>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int MOD = static_cast<int>(1e9 + 7);
const ll MOD2 = 998244353;
const int INF = INT_MAX - 1000000;
const ll LINF = LLONG_MAX - 1000000;
const ll OLINF = ll(2) << 60;
const int OINF = 2 << 29;
const int dx[4] = { 0,-1,0,1 };
const int dy[4] = { -1,0,1,0 };
const int ddx[8] = { -1,-1,0,1,1,1,0,-1 };
const int ddy[8] = { 0,1,1,1,0,-1,-1,-1 };
struct edge { int to; ll cost; };
struct kedge { int u, v, cost; };

Vi C;
Vi Ans;
Vb visited;
set<int> S;
int Dep[100005] = { 0 };

void bfs(int u, Vii& G) {
	visited[u] = true;
	//cout << u << " " << C[u] << endl;
	if (S.find(C[u]) == S.end()) Ans.push_back(u);
	S.insert(C[u]);
	Dep[C[u]]++;
	for (int nu : G[u]) {
		if (visited[nu]) continue;
		bfs(nu, G);
	}
	if (Dep[C[u]] > 1) {
		Dep[C[u]]--;
	}
	else {
		S.erase(S.find(C[u]));
		Dep[C[u]] = 0;
	}
}


int main() {
	int n;
	cin >> n;
	C.resize(n);
	rep(i, n) cin >> C[i];

	Vii G(n);
	rep(i, n - 1) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}

	visited.resize(n, false);
	bfs(0, G);

	sort(Ans.begin(), Ans.end());
	for (int A : Ans) {
		cout << A + 1 << endl;
	}
}