/*** author: yuji9511 ***/
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
using vll = vector<ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
ostream& operator<<(ostream& os, lpair& h){ os << "(" << h.first << ", " << h.second << ")"; return os;}
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

void solve(){
    ll N,M;
    cin >> N >> M;
    vll a(M), b(M);
    vector<vll> tree(N);
    rep(i,0,M){
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
        tree[a[i]].push_back(b[i]);
    }

    ll ans = 0;
    rep(i,0,N){
        vll check(N, 0);
        auto dfs = [&](auto dfs, ll cur) -> void {
            if(check[cur]) return;
            check[cur]++;
            for(auto &e: tree[cur]){
                if(!check[e]) dfs(dfs, e);
            }
        };
        dfs(dfs, i);
        rep(j,0,N){
            if(check[j]) ans++;
        }
    }
    print(ans);
    

}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pii;
#define vec(s) vector<s>
#define vvec(s) vector<vector<s>>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Yes(x) cout << (x ? "Yes" : "No") << endl;
#define YES(x) cout << (x ? "YES" : "NO") << endl;
#define out(s) cout << s << endl;
#define Reverse(x) reverse(all(x));
#define Sort(x) sort(all(x));
#define pb(s) push_back(s);
#define sp " "
#define INF 10000000000
#define LINF 9000000000000000000
#define all(s) s.begin(), s.end()
void vout(vi v)
{
    for (lli i = 0; i < v.size(); i++)
        cout << v.at(i) << endl;
}
vec(bool) node;
vvi path;
vi color;
vi ans;
vi c(1e5, 0);
void dfs(lli nd)
{
    node[nd] = true;
    if (c[color[nd] - 1] == 0)
    {
        ans.pb(nd + 1);
    }
    c[color[nd] - 1]++;
    for (lli a : path[nd])
    {
        if (!node[a])
        {
            dfs(a);
        }
    }
    c[color[nd] - 1]--;
}
int main()
{
    lli n;
    cin >> n;
    node = vec(bool)(n, false);
    color = vi(n, 0);
    path = vvi(n);
    rep(i, n)
    {
        cin >> color[i];
    }
    rep(i, n - 1)
    {
        lli a, b;
        cin >> a >> b;
        a--;
        b--;
        path[a].pb(b);
        path[b].pb(a);
    }
    dfs(0);
    Sort(ans);
    vout(ans);
}