# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
45487 | alenam0161 | Toy Train (IOI17_train) | C++17 | 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 "train.h"
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <unordered_map>
#include <unordered_set>
#include <queue>
using namespace std;
const int N = 5e3 + 7;
int n, m;
unordered_set<int> R;
vector<int> g[N], g1[N];
vector<int> A;
void rev(vector<bool> &fi){
for (auto &it : fi)
it = !it;
}
void Fw(int w, vector<bool> &fi){
static queue<int> q;
for (int i = 0; i < fi.size(); i++)
if (fi[i])
q.push(i);
while (!q.empty()){
auto it = q.front(); q.pop();
for (auto to : g1[it]){
if (fi[to])
continue;
if (A[to] == w || all_of(g[to].begin(), g[to].end(), [&](int x){return fi[x] == 1; }))
{
fi[to] = true;
q.push(to);
}
}
}
}
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
::n = a.size();
::A = a;
::m = u.size();
vector<int> res;
vector<bool> R;
for (auto it : r)
R.push_back(it);
for (int i = 0; i < m; ++i){
g[u[i]].push_back(v[i]);
g1[v[i]].push_back(u[i]);
}
while (true){
static vector<bool> X;
X = R;
Fw(1, X);
rev(X);
Fw(0, X);
int cnt = 0;
for (int i = 0; i < n; i++)
if (R[i] && X[i])
{
cnt++;
R[i] = false;
}
if (cnt == 0){
auto ans = vector<int>(n, 1);
for (int i = 0; i < n; i++)
if (X[i])
ans[i] = 0;
return ans;
}
}
return res;
}