#include <bits/stdc++.h>
using namespace std;
using ll = int;
using vll = vector<ll>;
using vvl = vector<vll>;
using pll = pair<ll,ll>;
using vpl = vector<pll>;
using vvp = vector<vpl>;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(),v.end()
vvl g, gb;
vll col;ll n;
vll rea(vll sp){
vll am(n), ans(n);
queue<ll> pq;
for(ll i = 0; i < n; ++i){
if(sp[i]==1){
for(ll j:gb[i]){
pq.push(j);
}
}
}
while(!pq.empty()){
ll cur = pq.front();
pq.pop();
am[cur]++;
if((col[cur]==1 && am[cur]==1) || (col[cur]==0 && am[cur]==g[cur].size())){
ans[cur]=1;
if(sp[cur])continue;
for(ll j:gb[cur]){
pq.push(j);
}
}
}
return ans;
}
vll who_wins(vll a, vll r, vll u, vll v) {
col=a;n=a.size();
g=gb=vvl(n);
for(ll i = 0; i < u.size(); ++i){
g[u[i]].pb(v[i]);
gb[v[i]].pb(u[i]);
}
vll ans;
for(ll i = 0; i < n; ++i){
ans = rea(r);
for(ll j = 0; j < n; ++j)r[j]=r[j]&ans[j];
}
return ans;
}
// int main() {
// int n, m;
// assert(2 == scanf("%d %d", &n, &m));
// vector<int> a(n), r(n), u(m), v(m);
// for(int i = 0; i < n; i++)
// assert(1 == scanf("%d", &a[i]));
// for(int i = 0; i < n; i++)
// assert(1 == scanf("%d", &r[i]));
// for(int i = 0; i < m; i++)
// assert(2 == scanf("%d %d", &u[i], &v[i]));
// vector<int> res = who_wins(a, r, u, v);
// for(int i = 0; i < (int)res.size(); i++)
// printf(i ? " %d" : "%d", res[i]);
// printf("\n");
// return 0;
// }