# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
68132 | Just_Solve_The_Problem | Toy Train (IOI17_train) | C++11 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "train.h"
#include "grader.cpp"
#define sz(s) (int)s.size()
#define pb push_back
#define all(s) s.begin(), s.end()
using namespace std;
const int N = (int)5e3 + 7;
int has[N];
vector < int > gr[N], rev[N];
vector < int > a, r;
vector < int > who_wins(vector < int > A, vector < int > R, vector < int > u, vector < int > v) {
a = A; r = R;
int n = sz(a);
int m = sz(u);
for (int i = 0; i < m; i++) {
gr[u[i]].pb(v[i]);
rev[v[i]].pb(u[i]);
}
vector < int > ans(n, 1);
while (1) {
vector < int > fa(n);
queue < int > q;
for (int i = 0; i < n; i++) {
if (ans[i] && r[i]) {
q.push(i);
} else if (a[i]) {
fa[i] = 1;
} else {
fa[i] = sz(gr[i]);
}
}
while (sz(q)) {
int v = q.front();
q.pop();
for (int to : rev[v]) {
if (fa[to] == 1) q.push(to);
fa[to]--;
}
}
vector < int > fb(n);
for (int i = 0; i < n; i++) {
if (fa[i] > 0) {
q.push(i);
} else if (a[i]) {
fb[i] = sz(gr[i]);
} else {
fb[i] = 1;
}
}
while (sz(q)) {
int v = q.front();
q.pop();
for (int to : rev[v]) {
if (fb[to] == 1) q.push(to);
fb[to]--;
}
}
vector < int > prv(n);
for (int i = 0; i < n; i++) prv[i] = (fb[i] > 0);
if (prv == ans) return ans;
else ans = prv;
}
}