#include "train.h"
#include <bits/stdc++.h>
using namespace std;
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
const int n = (int)a.size(), m = (int)u.size();
std::vector<std::vector<int>> graph(n);
for (int i = 0; i < m; ++i) {
graph[u[i]].push_back(v[i]);
}
int cntC = 0;
int chargingS = -1;
for (int i = 0; i < n; ++i) {
if (r[i] == 1) {
++cntC;
if (chargingS == -1) {
chargingS = i;
}
}
}/*
if (cntC == 1) {
// subtask 5
std::vector<int> reachableV(n, -1);
reachableV[chargingS] = 1;
for (int i = 0; i < n; ++i) {
auto itr = std::find(graph[i].begin(), graph[i].end(), i);
if (r[i] == 0 and a[i] == 0 and itr != graph[i].end()) {
reachableV[i] = 0;
}
}
for (int x = 0; x < 2 * n; ++x) {
for (int i = 0; i < n; ++i) {
if (reachableV[i] != -1) {
continue;
}
if (a[i] == 1) {
bool isOk = false, isRa = true;
for (const int t : graph[i]) {
if (reachableV[t] == 1) {
isOk = true;
}
if (reachableV[t] == -1) {
isRa = false;
}
}
if (isOk) {
reachableV[i] = 1;
} else if (isRa) {
reachableV[i] = 0;
}
} else {
bool isOk = true, isRa = true;
for (const int t : graph[i]) {
if (reachableV[t] != 1) {
isOk = false;
}
if (reachableV[t] == -1) {
isRa = false;
}
}
if (isOk) {
reachableV[i] = 1;
} else if (isRa) {
reachableV[i] = 0;
}
}
}
}
for (auto &e : reachableV) {
if (e == -1) {
e = 0;
}
}
reachableV[chargingS] = -1;
if (a[chargingS] == 1) {
bool isOk = false;
for (const int t : graph[chargingS]) {
if (reachableV[t] == 1) {
isOk = true;
} else if (t == chargingS) {
isOk = true;
}
}
if (isOk) {
reachableV[chargingS] = 1;
} else {
reachableV[chargingS] = 0;
}
} else {
bool isOk = true;
for (const int t : graph[chargingS]) {
if (reachableV[t] == 0) {
isOk = false;
}
}
if (isOk) {
reachableV[chargingS] = 1;
} else {
reachableV[chargingS] = 0;
}
}
std::vector<int> answer(n);
for (int i = 0; i < n; ++i) {
answer[i] = (reachableV[i] == 1 and reachableV[chargingS] == 1) ? 1 : 0;
}
return answer;
} else */if (std::all_of(a.begin(), a.end(), [](const int v) {
return v == 0;
})) {
// subtask 4
std::vector<int> answer(n, 1);
std::vector<bool> tCycle(n);
for (int f = 0; f < n; ++f) {
std::vector<int> type(n);
auto dfs = [&](auto &&self, const int v) -> bool {
type[v] = 1;
for (const int t : graph[v]) {
if (r[t] == 1) {
continue;
}
if (type[t] == 0) {
const auto res = self(self, t);
if (res) {
return true;
}
} else if (type[t] == 1) {
return true;
}
}
type[v] = 2;
return false;
};
tCycle[f] = dfs(dfs, f);
}
std::vector<std::vector<bool>> isReachable(n, std::vector<bool>(n));
for (int f = 0; f < n; ++f) {
isReachable[f][f] = true;
std::queue<int> que;
que.push(f);
while (not que.empty()) {
const int x = que.front();
que.pop();
for (const int t : graph[x]) {
if (not isReachable[f][t]) {
isReachable[f][t] = true;
que.push(t);
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (isReachable[i][j] and tCycle[j]) {
answer[i] = 0;
}
}
}
return answer;
} else if (std::all_of(a.begin(), a.end(), [](const int v) {
return v == 1;
})) {
// subtask 3
std::vector<int> answer(n);
std::vector<bool> tCycle(n);
for (int f = 0; f < n; ++f) {
if (r[f] == 0) {
continue;
}
std::vector<int> type(n);
auto dfs = [&](auto &&self, const int v) -> bool {
type[v] = 1;
for (const int t : graph[v]) {
if (type[t] == 0) {
const auto res = self(self, t);
if (res) {
return true;
}
} else if (t == f) {
return true;
}
}
type[v] = 2;
return false;
};
tCycle[f] = dfs(dfs, f);
}
std::vector<std::vector<bool>> isReachable(n, std::vector<bool>(n));
for (int f = 0; f < n; ++f) {
isReachable[f][f] = true;
std::queue<int> que;
que.push(f);
while (not que.empty()) {
const int x = que.front();
que.pop();
for (const int t : graph[x]) {
if (not isReachable[f][t]) {
isReachable[f][t] = true;
que.push(t);
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (isReachable[i][j] and tCycle[j]) {
answer[i] = 1;
}
}
}
return answer;
} else if (n <= 15) {
// subtask 2
std::vector<int> pow3(n + 1);
pow3[0] = 1;
for (int i = 1; i <= n; ++i) {
pow3[i] = pow3[i - 1] * 3;
}
std::vector<std::vector<bool>> dp(pow3[n], std::vector<bool>(n));
for (int bits = pow3[n] - 1; bits >= 0; --bits) {
std::vector<int> status(n);
for (int i = 0; i < n; ++i) {
const int v = bits % pow3[i + 1];
const int c = v / pow3[i];
status[i] = c;
}
auto nextBits = [&](const int a) {
assert(status[a] == 0);
int res = bits;
res += pow3[a];
if (r[a] == 1) {
for (int i = 0; i < n; ++i) {
const int v = res % pow3[i + 1];
const int c = v / pow3[i];
if (c == 1) {
res += pow3[i];
}
}
}
return res;
};
for (int f = 0; f < n; ++f) {
if (status[f] == 0) {
continue;
}
if (a[f] == 1) {
bool isOk = false;
for (const int t : graph[f]) {
if (status[t] == 0) {
isOk |= dp[nextBits(t)][t];
} else if (status[t] == 2) {
isOk = true;
}
}
dp[bits][f] = isOk;
} else {
bool isOk = true;
for (const int t : graph[f]) {
if (status[t] == 0) {
if (not dp[nextBits(t)][t]) {
isOk = false;
}
} else if (status[t] == 1) {
isOk = false;
}
}
dp[bits][f] = isOk;
}
}
}
std::vector<int> answer(n);
for (int i = 0; i < n; ++i) {
answer[i] = dp[pow3[i] * (r[i] + 1)][i] ? 1 : 0;
}
return answer;
} else {
// subtask 1
}
}
Compilation message
train.cpp: In function 'std::vector<int> who_wins(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
train.cpp:8:42: warning: control reaches end of non-void function [-Wreturn-type]
8 | std::vector<std::vector<int>> graph(n);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
1364 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
309 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
4308 KB |
Output is correct |
2 |
Correct |
263 ms |
4436 KB |
Output is correct |
3 |
Correct |
275 ms |
4428 KB |
Output is correct |
4 |
Correct |
1026 ms |
4320 KB |
Output is correct |
5 |
Correct |
670 ms |
4396 KB |
Output is correct |
6 |
Correct |
599 ms |
4300 KB |
Output is correct |
7 |
Correct |
796 ms |
4360 KB |
Output is correct |
8 |
Correct |
404 ms |
4308 KB |
Output is correct |
9 |
Correct |
366 ms |
4308 KB |
Output is correct |
10 |
Correct |
431 ms |
4220 KB |
Output is correct |
11 |
Correct |
407 ms |
4308 KB |
Output is correct |
12 |
Correct |
50 ms |
4308 KB |
Output is correct |
13 |
Correct |
858 ms |
4456 KB |
Output is correct |
14 |
Correct |
851 ms |
4456 KB |
Output is correct |
15 |
Correct |
863 ms |
4436 KB |
Output is correct |
16 |
Correct |
817 ms |
4308 KB |
Output is correct |
17 |
Correct |
831 ms |
4308 KB |
Output is correct |
18 |
Correct |
467 ms |
4140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
732 ms |
4180 KB |
Output is correct |
2 |
Correct |
263 ms |
4168 KB |
Output is correct |
3 |
Correct |
407 ms |
4308 KB |
Output is correct |
4 |
Correct |
559 ms |
4248 KB |
Output is correct |
5 |
Correct |
652 ms |
4236 KB |
Output is correct |
6 |
Correct |
645 ms |
4248 KB |
Output is correct |
7 |
Correct |
671 ms |
4336 KB |
Output is correct |
8 |
Correct |
402 ms |
4308 KB |
Output is correct |
9 |
Correct |
51 ms |
4308 KB |
Output is correct |
10 |
Correct |
901 ms |
4392 KB |
Output is correct |
11 |
Correct |
823 ms |
4308 KB |
Output is correct |
12 |
Correct |
862 ms |
4308 KB |
Output is correct |
13 |
Correct |
596 ms |
4240 KB |
Output is correct |
14 |
Correct |
388 ms |
4184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
7 ms |
1876 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
1364 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |