#include "train.h"
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
using namespace std;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
using ll = long long;
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
vector<int> ids(a.size()); iota(all(ids), 0);
vector<int> ans(a.size());
while (true) {
int n = a.size(), m = u.size();
vector<vector<int>> g(n), rg(n);
rep (i, m) {
g[u[i]].push_back(v[i]);
rg[v[i]].push_back(u[i]);
}
auto calc = [&](vector<int> r, bool f) -> vector<int> {
queue<int> que;
rep (i, n) {
if (r[i]) que.push(i);
}
vector<int> deg(n);
rep (i, n) deg[i] = g[i].size();
while (!que.empty()) {
int i = que.front(); que.pop();
for (int j : rg[i]) {
if (r[j]) continue;
if (a[j] == f) {
r[j] = true;
}
else {
if ((--deg[j]) == 0) {
r[j] = true;
}
}
if (r[j]) que.push(j);
}
}
return r;
};
auto r2 = calc(r, 1);
if (count(all(r2), 1) == n) {
rep (i, n) ans[ids[i]] = 1;
break;
}
rep (i, n) r2[i] ^= 1;
auto r3 = calc(r2, 0);
vector<int> idx(n, -1);
vector<int> ids2;
int cnt = 0;
rep (i, n) {
if (!r3[i]) {
idx[i] = cnt++;
ids2.push_back(ids[i]);
}
}
vector<int> a2(cnt), r4(cnt);
rep (i, n) {
if (idx[i] == -1) continue;
a2[idx[i]] = a[i];
r4[idx[i]] = r[i];
}
vector<int> u2, v2;
rep (i, m) {
if (idx[u[i]] == -1 || idx[v[i]] == -1) continue;
u2.push_back(idx[u[i]]);
v2.push_back(idx[v[i]]);
}
rep (i, n) {
if (idx[i] == -1) ans[ids[i]] = 0;
}
a = a2;
r = r4;
u = u2;
v = v2;
ids = ids2;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1112 KB |
Output is correct |
2 |
Correct |
6 ms |
1116 KB |
Output is correct |
3 |
Correct |
5 ms |
1372 KB |
Output is correct |
4 |
Correct |
5 ms |
1372 KB |
Output is correct |
5 |
Correct |
4 ms |
1116 KB |
Output is correct |
6 |
Correct |
6 ms |
1116 KB |
Output is correct |
7 |
Correct |
3 ms |
1176 KB |
Output is correct |
8 |
Correct |
6 ms |
1412 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
4 ms |
1112 KB |
Output is correct |
11 |
Correct |
3 ms |
1320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
365 ms |
2064 KB |
Output is correct |
2 |
Correct |
617 ms |
2032 KB |
Output is correct |
3 |
Correct |
837 ms |
1988 KB |
Output is correct |
4 |
Correct |
8 ms |
1624 KB |
Output is correct |
5 |
Correct |
9 ms |
1708 KB |
Output is correct |
6 |
Correct |
9 ms |
1904 KB |
Output is correct |
7 |
Correct |
6 ms |
1628 KB |
Output is correct |
8 |
Correct |
7 ms |
1880 KB |
Output is correct |
9 |
Correct |
5 ms |
1628 KB |
Output is correct |
10 |
Correct |
9 ms |
1832 KB |
Output is correct |
11 |
Correct |
6 ms |
1872 KB |
Output is correct |
12 |
Correct |
7 ms |
1628 KB |
Output is correct |
13 |
Correct |
6 ms |
1624 KB |
Output is correct |
14 |
Correct |
5 ms |
1604 KB |
Output is correct |
15 |
Correct |
5 ms |
1628 KB |
Output is correct |
16 |
Correct |
6 ms |
1624 KB |
Output is correct |
17 |
Correct |
6 ms |
1720 KB |
Output is correct |
18 |
Correct |
946 ms |
1692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1628 KB |
Output is correct |
2 |
Correct |
6 ms |
1624 KB |
Output is correct |
3 |
Correct |
5 ms |
1372 KB |
Output is correct |
4 |
Correct |
5 ms |
1372 KB |
Output is correct |
5 |
Correct |
6 ms |
1628 KB |
Output is correct |
6 |
Correct |
6 ms |
1628 KB |
Output is correct |
7 |
Correct |
6 ms |
1672 KB |
Output is correct |
8 |
Correct |
5 ms |
1372 KB |
Output is correct |
9 |
Correct |
6 ms |
1628 KB |
Output is correct |
10 |
Correct |
5 ms |
1372 KB |
Output is correct |
11 |
Correct |
6 ms |
1352 KB |
Output is correct |
12 |
Correct |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
6 ms |
1628 KB |
Output is correct |
14 |
Correct |
6 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1628 KB |
Output is correct |
2 |
Correct |
9 ms |
1396 KB |
Output is correct |
3 |
Correct |
9 ms |
1628 KB |
Output is correct |
4 |
Correct |
5 ms |
1464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
4 ms |
1116 KB |
Output is correct |
8 |
Correct |
4 ms |
1112 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1112 KB |
Output is correct |
2 |
Correct |
6 ms |
1116 KB |
Output is correct |
3 |
Correct |
5 ms |
1372 KB |
Output is correct |
4 |
Correct |
5 ms |
1372 KB |
Output is correct |
5 |
Correct |
4 ms |
1116 KB |
Output is correct |
6 |
Correct |
6 ms |
1116 KB |
Output is correct |
7 |
Correct |
3 ms |
1176 KB |
Output is correct |
8 |
Correct |
6 ms |
1412 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
4 ms |
1112 KB |
Output is correct |
11 |
Correct |
3 ms |
1320 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
356 KB |
Output is correct |
32 |
Correct |
365 ms |
2064 KB |
Output is correct |
33 |
Correct |
617 ms |
2032 KB |
Output is correct |
34 |
Correct |
837 ms |
1988 KB |
Output is correct |
35 |
Correct |
8 ms |
1624 KB |
Output is correct |
36 |
Correct |
9 ms |
1708 KB |
Output is correct |
37 |
Correct |
9 ms |
1904 KB |
Output is correct |
38 |
Correct |
6 ms |
1628 KB |
Output is correct |
39 |
Correct |
7 ms |
1880 KB |
Output is correct |
40 |
Correct |
5 ms |
1628 KB |
Output is correct |
41 |
Correct |
9 ms |
1832 KB |
Output is correct |
42 |
Correct |
6 ms |
1872 KB |
Output is correct |
43 |
Correct |
7 ms |
1628 KB |
Output is correct |
44 |
Correct |
6 ms |
1624 KB |
Output is correct |
45 |
Correct |
5 ms |
1604 KB |
Output is correct |
46 |
Correct |
5 ms |
1628 KB |
Output is correct |
47 |
Correct |
6 ms |
1624 KB |
Output is correct |
48 |
Correct |
6 ms |
1720 KB |
Output is correct |
49 |
Correct |
946 ms |
1692 KB |
Output is correct |
50 |
Correct |
7 ms |
1628 KB |
Output is correct |
51 |
Correct |
6 ms |
1624 KB |
Output is correct |
52 |
Correct |
5 ms |
1372 KB |
Output is correct |
53 |
Correct |
5 ms |
1372 KB |
Output is correct |
54 |
Correct |
6 ms |
1628 KB |
Output is correct |
55 |
Correct |
6 ms |
1628 KB |
Output is correct |
56 |
Correct |
6 ms |
1672 KB |
Output is correct |
57 |
Correct |
5 ms |
1372 KB |
Output is correct |
58 |
Correct |
6 ms |
1628 KB |
Output is correct |
59 |
Correct |
5 ms |
1372 KB |
Output is correct |
60 |
Correct |
6 ms |
1352 KB |
Output is correct |
61 |
Correct |
5 ms |
1372 KB |
Output is correct |
62 |
Correct |
6 ms |
1628 KB |
Output is correct |
63 |
Correct |
6 ms |
1628 KB |
Output is correct |
64 |
Correct |
6 ms |
1628 KB |
Output is correct |
65 |
Correct |
9 ms |
1396 KB |
Output is correct |
66 |
Correct |
9 ms |
1628 KB |
Output is correct |
67 |
Correct |
5 ms |
1464 KB |
Output is correct |
68 |
Correct |
1 ms |
348 KB |
Output is correct |
69 |
Correct |
4 ms |
1116 KB |
Output is correct |
70 |
Correct |
4 ms |
1116 KB |
Output is correct |
71 |
Correct |
4 ms |
1112 KB |
Output is correct |
72 |
Correct |
3 ms |
1116 KB |
Output is correct |
73 |
Correct |
1 ms |
604 KB |
Output is correct |
74 |
Correct |
4 ms |
1116 KB |
Output is correct |
75 |
Correct |
619 ms |
2212 KB |
Output is correct |
76 |
Correct |
639 ms |
2248 KB |
Output is correct |
77 |
Correct |
872 ms |
2200 KB |
Output is correct |
78 |
Correct |
851 ms |
2268 KB |
Output is correct |
79 |
Correct |
832 ms |
2232 KB |
Output is correct |
80 |
Correct |
8 ms |
2136 KB |
Output is correct |
81 |
Correct |
7 ms |
1628 KB |
Output is correct |
82 |
Correct |
10 ms |
1884 KB |
Output is correct |
83 |
Correct |
9 ms |
1904 KB |
Output is correct |
84 |
Correct |
14 ms |
2136 KB |
Output is correct |
85 |
Correct |
15 ms |
1984 KB |
Output is correct |
86 |
Correct |
16 ms |
2136 KB |
Output is correct |
87 |
Correct |
7 ms |
1880 KB |
Output is correct |
88 |
Correct |
14 ms |
1884 KB |
Output is correct |
89 |
Correct |
7 ms |
1880 KB |
Output is correct |
90 |
Correct |
24 ms |
2140 KB |
Output is correct |
91 |
Correct |
24 ms |
2140 KB |
Output is correct |
92 |
Correct |
55 ms |
2140 KB |
Output is correct |
93 |
Correct |
52 ms |
2136 KB |
Output is correct |
94 |
Correct |
48 ms |
2196 KB |
Output is correct |
95 |
Correct |
54 ms |
2188 KB |
Output is correct |
96 |
Correct |
259 ms |
2048 KB |
Output is correct |
97 |
Correct |
961 ms |
2188 KB |
Output is correct |
98 |
Correct |
1614 ms |
2148 KB |
Output is correct |
99 |
Correct |
1500 ms |
2128 KB |
Output is correct |
100 |
Correct |
949 ms |
1712 KB |
Output is correct |