#include "train.h"
#include <array>
#include <algorithm>
#include <numeric>
#include <utility>
#include <iterator>
#include <iostream>
#include <cassert>
#include <tuple>
#include <string>
#include <set>
#include <vector>
#include <queue>
#include <map>
using std::array;
using std::tuple;
using std::pair;
using std::vector;
using std::string;
using ll = long long;
constexpr int inf = (1 << 30) - 1;
constexpr ll infll = (1ll << 62) - 1;
template <class T> bool setmin(T& x, const T& y) { return x > y && (x = y, true); }
template <class T> bool setmax(T& x, const T& y) { return x < y && (x = y, true); }
template <class F> struct make_fixed : private F {
explicit make_fixed(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
vector<int> who_wins(vector<int> A, vector<int> R, vector<int> U, vector<int> V) {
const int N = (int)A.size();
const int M = (int)U.size();
vector<int> ans(N);
vector<char> alive(N, true);
vector<vector<int>> rG(N);
for (int i = 0; i < M; ++i) {
rG[V[i]].push_back(U[i]);
}
const auto bfs = [&](vector<int> set, int type) {
vector<int> deg(N);
for (int i = 0; i < M; ++i) {
int u = U[i], v = V[i];
if (alive[u] && alive[v]) {
deg[u] += 1;
}
}
vector<char> reach(N);
std::queue<int> que;
for (const int u : set) {
reach[u] = true;
que.push(u);
}
while (!que.empty()) {
const int u = que.front();
que.pop();
for (const int v : rG[u]) {
if (!alive[v] || reach[v]) continue;
if (A[v] == type) {
reach[v] = true;
que.push(v);
} else if (--deg[v] == 0) {
reach[v] = true;
que.push(v);
}
}
}
return reach;
};
while (std::any_of(alive.begin(), alive.end(), [](bool b) { return b; })) {
vector<int> s;
for (int i = 0; i < N; ++i) {
if (alive[i] && R[i]) {
s.push_back(i);
}
}
auto s2 = bfs(std::move(s), 1);
vector<int> t;
for (int i = 0; i < N; ++i) {
if (alive[i] && !s2[i]) {
t.push_back(i);
}
}
if (t.empty()) {
for (int i = 0; i < N; ++i) {
if (alive[i]) {
ans[i] = 1;
alive[i] = false;
}
}
break;
}
auto t2 = bfs(std::move(t), 0);
for (int i = 0; i < N; ++i) {
if (t2[i]) {
alive[i] = false;
}
}
}
return ans;
}
#ifndef EVAL
#include "grader.cpp"
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
724 KB |
Output is correct |
2 |
Correct |
5 ms |
724 KB |
Output is correct |
3 |
Correct |
4 ms |
772 KB |
Output is correct |
4 |
Correct |
4 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
724 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
3 ms |
724 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
1084 KB |
Output is correct |
2 |
Correct |
141 ms |
1084 KB |
Output is correct |
3 |
Correct |
181 ms |
1104 KB |
Output is correct |
4 |
Correct |
5 ms |
980 KB |
Output is correct |
5 |
Correct |
8 ms |
1100 KB |
Output is correct |
6 |
Correct |
7 ms |
980 KB |
Output is correct |
7 |
Correct |
6 ms |
1044 KB |
Output is correct |
8 |
Correct |
5 ms |
1108 KB |
Output is correct |
9 |
Correct |
6 ms |
984 KB |
Output is correct |
10 |
Correct |
5 ms |
980 KB |
Output is correct |
11 |
Correct |
6 ms |
980 KB |
Output is correct |
12 |
Correct |
4 ms |
980 KB |
Output is correct |
13 |
Correct |
6 ms |
980 KB |
Output is correct |
14 |
Correct |
7 ms |
980 KB |
Output is correct |
15 |
Correct |
5 ms |
980 KB |
Output is correct |
16 |
Correct |
5 ms |
980 KB |
Output is correct |
17 |
Correct |
6 ms |
980 KB |
Output is correct |
18 |
Correct |
350 ms |
884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
980 KB |
Output is correct |
2 |
Correct |
5 ms |
980 KB |
Output is correct |
3 |
Correct |
5 ms |
976 KB |
Output is correct |
4 |
Correct |
5 ms |
980 KB |
Output is correct |
5 |
Correct |
6 ms |
980 KB |
Output is correct |
6 |
Correct |
6 ms |
996 KB |
Output is correct |
7 |
Correct |
5 ms |
968 KB |
Output is correct |
8 |
Correct |
5 ms |
980 KB |
Output is correct |
9 |
Correct |
5 ms |
980 KB |
Output is correct |
10 |
Correct |
6 ms |
1088 KB |
Output is correct |
11 |
Correct |
6 ms |
1004 KB |
Output is correct |
12 |
Correct |
7 ms |
1060 KB |
Output is correct |
13 |
Correct |
6 ms |
1100 KB |
Output is correct |
14 |
Correct |
6 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1004 KB |
Output is correct |
2 |
Correct |
5 ms |
1032 KB |
Output is correct |
3 |
Correct |
6 ms |
984 KB |
Output is correct |
4 |
Correct |
5 ms |
988 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
4 ms |
704 KB |
Output is correct |
9 |
Correct |
4 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
724 KB |
Output is correct |
2 |
Correct |
5 ms |
724 KB |
Output is correct |
3 |
Correct |
4 ms |
772 KB |
Output is correct |
4 |
Correct |
4 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
724 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
3 ms |
724 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
70 ms |
1084 KB |
Output is correct |
33 |
Correct |
141 ms |
1084 KB |
Output is correct |
34 |
Correct |
181 ms |
1104 KB |
Output is correct |
35 |
Correct |
5 ms |
980 KB |
Output is correct |
36 |
Correct |
8 ms |
1100 KB |
Output is correct |
37 |
Correct |
7 ms |
980 KB |
Output is correct |
38 |
Correct |
6 ms |
1044 KB |
Output is correct |
39 |
Correct |
5 ms |
1108 KB |
Output is correct |
40 |
Correct |
6 ms |
984 KB |
Output is correct |
41 |
Correct |
5 ms |
980 KB |
Output is correct |
42 |
Correct |
6 ms |
980 KB |
Output is correct |
43 |
Correct |
4 ms |
980 KB |
Output is correct |
44 |
Correct |
6 ms |
980 KB |
Output is correct |
45 |
Correct |
7 ms |
980 KB |
Output is correct |
46 |
Correct |
5 ms |
980 KB |
Output is correct |
47 |
Correct |
5 ms |
980 KB |
Output is correct |
48 |
Correct |
6 ms |
980 KB |
Output is correct |
49 |
Correct |
350 ms |
884 KB |
Output is correct |
50 |
Correct |
4 ms |
980 KB |
Output is correct |
51 |
Correct |
5 ms |
980 KB |
Output is correct |
52 |
Correct |
5 ms |
976 KB |
Output is correct |
53 |
Correct |
5 ms |
980 KB |
Output is correct |
54 |
Correct |
6 ms |
980 KB |
Output is correct |
55 |
Correct |
6 ms |
996 KB |
Output is correct |
56 |
Correct |
5 ms |
968 KB |
Output is correct |
57 |
Correct |
5 ms |
980 KB |
Output is correct |
58 |
Correct |
5 ms |
980 KB |
Output is correct |
59 |
Correct |
6 ms |
1088 KB |
Output is correct |
60 |
Correct |
6 ms |
1004 KB |
Output is correct |
61 |
Correct |
7 ms |
1060 KB |
Output is correct |
62 |
Correct |
6 ms |
1100 KB |
Output is correct |
63 |
Correct |
6 ms |
980 KB |
Output is correct |
64 |
Correct |
7 ms |
1004 KB |
Output is correct |
65 |
Correct |
5 ms |
1032 KB |
Output is correct |
66 |
Correct |
6 ms |
984 KB |
Output is correct |
67 |
Correct |
5 ms |
988 KB |
Output is correct |
68 |
Correct |
1 ms |
344 KB |
Output is correct |
69 |
Correct |
3 ms |
724 KB |
Output is correct |
70 |
Correct |
3 ms |
724 KB |
Output is correct |
71 |
Correct |
4 ms |
704 KB |
Output is correct |
72 |
Correct |
4 ms |
716 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
3 ms |
596 KB |
Output is correct |
75 |
Correct |
122 ms |
1096 KB |
Output is correct |
76 |
Correct |
167 ms |
1200 KB |
Output is correct |
77 |
Correct |
200 ms |
1320 KB |
Output is correct |
78 |
Correct |
199 ms |
1320 KB |
Output is correct |
79 |
Correct |
199 ms |
1312 KB |
Output is correct |
80 |
Correct |
7 ms |
1208 KB |
Output is correct |
81 |
Correct |
6 ms |
1244 KB |
Output is correct |
82 |
Correct |
6 ms |
1212 KB |
Output is correct |
83 |
Correct |
8 ms |
1208 KB |
Output is correct |
84 |
Correct |
7 ms |
1280 KB |
Output is correct |
85 |
Correct |
7 ms |
1248 KB |
Output is correct |
86 |
Correct |
8 ms |
1248 KB |
Output is correct |
87 |
Correct |
5 ms |
1212 KB |
Output is correct |
88 |
Correct |
9 ms |
1244 KB |
Output is correct |
89 |
Correct |
7 ms |
1208 KB |
Output is correct |
90 |
Correct |
14 ms |
1204 KB |
Output is correct |
91 |
Correct |
14 ms |
1240 KB |
Output is correct |
92 |
Correct |
25 ms |
1208 KB |
Output is correct |
93 |
Correct |
24 ms |
1252 KB |
Output is correct |
94 |
Correct |
30 ms |
1236 KB |
Output is correct |
95 |
Correct |
30 ms |
1224 KB |
Output is correct |
96 |
Correct |
80 ms |
1192 KB |
Output is correct |
97 |
Correct |
457 ms |
1296 KB |
Output is correct |
98 |
Correct |
662 ms |
1260 KB |
Output is correct |
99 |
Correct |
698 ms |
1264 KB |
Output is correct |
100 |
Correct |
329 ms |
1016 KB |
Output is correct |