# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
67768 | aquablitz11 | Toy Train (IOI17_train) | C++14 | 83 ms | 1632 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"
using namespace std;
const int N = 5e3+10;
vector<int> G[N], R[N];
vector<int> who_wins(vector<int> A, vector<int> C, vector<int> U, vector<int> V)
{
// input
int n = A.size();
int m = U.size();
for (int i = 0; i < m; ++i) {
G[U[i]].push_back(V[i]);
R[V[i]].push_back(U[i]);
}
vector<int> ans(n, 0);
for (int i = 0; i < n; ++i) if (C[i]) { // i = main recharger we need for cycle
// first step: find all nodes that can be forced to reach i
vector<bool> reach(n, false);
vector<int> cnt(n, 0);
queue<int> Q;
Q.push(i);
reach[i] = true;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (auto p : R[u]) if (!reach[p]) {
++cnt[p];
if ((A[p] && cnt[p] == 1) || (!A[p] && cnt[p] == G[p].size())) {
reach[p] = true;
Q.push(p);
}
}
}
// second step: find if i could be in cycle or not
vector<bool> cycle(n, false);
int cntr = 0;
for (auto v : G[i]) if (reach[v]) ++cntr;
if ((A[i] && cntr > 0) || (!A[i] && cntr == G[i].size()))
cycle[i] = true;
// third step: find all nodes that could reach the cycle
for (int i = 0; i < n; ++i)
ans[i] = reach[i] ? 1 : 0;
/*fill(cnt.begin(), cnt.end(), 0);
if (cycle[i]) Q.push(i);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
ans[u] = 1;
for (auto p : R[u]) if (!cycle[p]) {
++cnt[p];
if ((A[p] && cnt[p] == 1) || (!A[p] && cnt[p] == G[p].size())) {
cycle[p] = true;
Q.push(p);
}
}
}*/
}
return ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |