#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define debug false
const int MAXN = 5000 + 17;
vector<int> graf[MAXN];
vector<int> akt;
vector<int> vec;
int deg[MAXN];
bool odw[MAXN];
bool odw2[MAXN];
vector<int> who_wins(vector<int> a, vector<int> r, vector<int> u, vector<int> v) {
vector<int> wyn = {};
int n = int(a.size());
int m = int(u.size());
for (int i = 0; i < m; i ++) {
graf[v[i]].pb(u[i]);
}
for (int i = 0; i < n; i ++) {
wyn.pb(1);
akt.pb(i);
}
while (true) {
for (auto x : akt) {
deg[x] = 0;
odw[x] = 0;
odw2[x] = 0;
}
for (auto x : akt) {
deg[x] = 0;
}
for (auto x : akt) {
if (r[x]) {
odw[x] = 1;
vec.pb(x);
}
for (auto y : graf[x]) {
deg[y] ++;
}
}
while (!vec.empty()) {
int v = vec.back();
vec.pop_back();
for (auto x : graf[v]) {
if (wyn[x] == 0) {
continue;
}
deg[x] --;
if (a[x] == 0) {
if (!odw[x] && deg[x] == 0) {
odw[x] = 1;
vec.pb(x);
}
} else if (!odw[x]) {
odw[x] = 1;
vec.pb(x);
}
}
}
for (auto x : akt) {
deg[x] = 0;
}
for (auto x : akt) {
if (!odw[x]) {
vec.pb(x);
odw2[x] = 1;
}
for (auto y : graf[x]) {
deg[y] ++;
}
}
while (!vec.empty()) {
int v = vec.back();
vec.pop_back();
for (auto x : graf[v]) {
if (wyn[x] == 0) {
continue;
}
deg[x] --;
if (a[x] == 1) {
if (!odw2[x] && deg[x] == 0) {
odw2[x] = 1;
vec.pb(x);
}
} else if (!odw2[x]) {
odw2[x] = 1;
vec.pb(x);
}
}
}
vector<int> vec2 = {};
bool kon = true;
for (auto x : akt) {
if (!odw2[x]) {
vec2.pb(x);
} else {
wyn[x] = 0;
kon = false;
}
}
if (kon) {
break;
}
swap(akt, vec2);
}
return wyn;
}
/*int main () {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
vector<int> v = who_wins({0, 1}, {1, 0}, {0, 0, 1, 1}, {0, 1, 0, 1});
for (auto x : v) {
cout << x << "\n";
}
return 0;
}*/