#include "train.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uset = unordered_set<int>;
const int nmax = 5e3 + 5;
vector<int> g[nmax], t[nmax];
vector<int> own, charge, rez, degree;
int n, m;
uset S, notS;
int cnt = 0;
void solve(uset& V) {
if(V.size() == 0)
return;
S.clear();
notS.clear();
for(auto x : V) {
if(charge[x])
S.insert(x);
for(int i = 0; i < g[x].size(); i++) {
if(V.count(g[x][i]) == 0)
swap(g[x][i], g[x].back()), g[x].pop_back(), i--;
}
for(int i = 0; i < t[x].size(); i++) {
if(V.count(t[x][i]) == 0)
swap(t[x][i], t[x].back()), t[x].pop_back(), i--;
}
}
if(S.size() == 0) {
for(auto x : V)
rez[x] = 0;
return;
}
for(auto x : V) {
if(charge[x])
degree[x] = 0;
if(own[x] == 1)
degree[x] = 1;
else
degree[x] = g[x].size();
}
queue<int> que;
function<void()> markall = [&]() {
while (!que.empty()) {
int node = que.front();
for(auto x : t[node]) {
degree[x]--;
if(degree[x] == 0 && !S.count(x))
S.insert(x),
que.push(x);
}
que.pop();
}
que = queue<int>();
return;
};
for(auto x : S)
que.push(x);
markall();
if(S.size() == V.size()) {
for(auto x : V)
rez[x] = 1;
return;
}
for(auto x : V)
if(S.count(x) == 0)
notS.insert(x);
for(auto x : V) {
if(notS.count(x) == 1)
degree[x] = 0;
if(own[x] == 0)
degree[x] = 1;
else
degree[x] = g[x].size();
}
swap(S, notS);
for(auto x : S)
que.push(x);
markall();
for(auto x : S)
rez[x] = 0, // formality
V.erase(x);
solve(V);
}
std::vector<int> who_wins(std::vector<int> A, std::vector<int> R, std::vector<int> U, std::vector<int> V) {
own = move(A);
charge = move(R);
n = own.size();
m = U.size();
degree.resize(n, 0);
rez.resize(n, 0);
for(int i = 0; i < m; i++)
g[U[i]].push_back(V[i]),
t[V[i]].push_back(U[i]);
uset Vertex;
for(int i = 0; i < n; i++)
Vertex.insert(i);
solve(Vertex);
return rez;
}
Compilation message
train.cpp: In function 'void solve(uset&)':
train.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < g[x].size(); i++) {
| ~~^~~~~~~~~~~~~
train.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i = 0; i < t[x].size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1492 KB |
Output is correct |
2 |
Correct |
8 ms |
1492 KB |
Output is correct |
3 |
Correct |
7 ms |
1492 KB |
Output is correct |
4 |
Correct |
8 ms |
1532 KB |
Output is correct |
5 |
Correct |
6 ms |
1492 KB |
Output is correct |
6 |
Correct |
6 ms |
1492 KB |
Output is correct |
7 |
Correct |
6 ms |
1492 KB |
Output is correct |
8 |
Correct |
8 ms |
1492 KB |
Output is correct |
9 |
Correct |
7 ms |
1520 KB |
Output is correct |
10 |
Correct |
6 ms |
1492 KB |
Output is correct |
11 |
Correct |
5 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
419 ms |
2152 KB |
Output is correct |
2 |
Correct |
771 ms |
2508 KB |
Output is correct |
3 |
Correct |
984 ms |
2820 KB |
Output is correct |
4 |
Correct |
9 ms |
1748 KB |
Output is correct |
5 |
Correct |
13 ms |
1748 KB |
Output is correct |
6 |
Correct |
11 ms |
1740 KB |
Output is correct |
7 |
Correct |
8 ms |
1708 KB |
Output is correct |
8 |
Correct |
9 ms |
1748 KB |
Output is correct |
9 |
Correct |
8 ms |
1756 KB |
Output is correct |
10 |
Correct |
9 ms |
1764 KB |
Output is correct |
11 |
Correct |
9 ms |
1748 KB |
Output is correct |
12 |
Correct |
8 ms |
1620 KB |
Output is correct |
13 |
Correct |
9 ms |
1756 KB |
Output is correct |
14 |
Correct |
9 ms |
1748 KB |
Output is correct |
15 |
Correct |
10 ms |
1748 KB |
Output is correct |
16 |
Correct |
9 ms |
1748 KB |
Output is correct |
17 |
Correct |
8 ms |
1748 KB |
Output is correct |
18 |
Correct |
983 ms |
3472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1620 KB |
Output is correct |
2 |
Correct |
9 ms |
1620 KB |
Output is correct |
3 |
Correct |
10 ms |
1776 KB |
Output is correct |
4 |
Correct |
10 ms |
1876 KB |
Output is correct |
5 |
Correct |
9 ms |
1808 KB |
Output is correct |
6 |
Correct |
11 ms |
1804 KB |
Output is correct |
7 |
Correct |
10 ms |
1828 KB |
Output is correct |
8 |
Correct |
10 ms |
1748 KB |
Output is correct |
9 |
Correct |
9 ms |
1728 KB |
Output is correct |
10 |
Correct |
9 ms |
1748 KB |
Output is correct |
11 |
Correct |
10 ms |
1748 KB |
Output is correct |
12 |
Correct |
9 ms |
1688 KB |
Output is correct |
13 |
Correct |
9 ms |
1748 KB |
Output is correct |
14 |
Correct |
10 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1748 KB |
Output is correct |
2 |
Correct |
9 ms |
1756 KB |
Output is correct |
3 |
Correct |
10 ms |
1792 KB |
Output is correct |
4 |
Correct |
9 ms |
1608 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
5 ms |
1364 KB |
Output is correct |
7 |
Correct |
6 ms |
1108 KB |
Output is correct |
8 |
Correct |
5 ms |
1108 KB |
Output is correct |
9 |
Correct |
6 ms |
1108 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
6 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1492 KB |
Output is correct |
2 |
Correct |
8 ms |
1492 KB |
Output is correct |
3 |
Correct |
7 ms |
1492 KB |
Output is correct |
4 |
Correct |
8 ms |
1532 KB |
Output is correct |
5 |
Correct |
6 ms |
1492 KB |
Output is correct |
6 |
Correct |
6 ms |
1492 KB |
Output is correct |
7 |
Correct |
6 ms |
1492 KB |
Output is correct |
8 |
Correct |
8 ms |
1492 KB |
Output is correct |
9 |
Correct |
7 ms |
1520 KB |
Output is correct |
10 |
Correct |
6 ms |
1492 KB |
Output is correct |
11 |
Correct |
5 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
468 KB |
Output is correct |
32 |
Correct |
419 ms |
2152 KB |
Output is correct |
33 |
Correct |
771 ms |
2508 KB |
Output is correct |
34 |
Correct |
984 ms |
2820 KB |
Output is correct |
35 |
Correct |
9 ms |
1748 KB |
Output is correct |
36 |
Correct |
13 ms |
1748 KB |
Output is correct |
37 |
Correct |
11 ms |
1740 KB |
Output is correct |
38 |
Correct |
8 ms |
1708 KB |
Output is correct |
39 |
Correct |
9 ms |
1748 KB |
Output is correct |
40 |
Correct |
8 ms |
1756 KB |
Output is correct |
41 |
Correct |
9 ms |
1764 KB |
Output is correct |
42 |
Correct |
9 ms |
1748 KB |
Output is correct |
43 |
Correct |
8 ms |
1620 KB |
Output is correct |
44 |
Correct |
9 ms |
1756 KB |
Output is correct |
45 |
Correct |
9 ms |
1748 KB |
Output is correct |
46 |
Correct |
10 ms |
1748 KB |
Output is correct |
47 |
Correct |
9 ms |
1748 KB |
Output is correct |
48 |
Correct |
8 ms |
1748 KB |
Output is correct |
49 |
Correct |
983 ms |
3472 KB |
Output is correct |
50 |
Correct |
9 ms |
1620 KB |
Output is correct |
51 |
Correct |
9 ms |
1620 KB |
Output is correct |
52 |
Correct |
10 ms |
1776 KB |
Output is correct |
53 |
Correct |
10 ms |
1876 KB |
Output is correct |
54 |
Correct |
9 ms |
1808 KB |
Output is correct |
55 |
Correct |
11 ms |
1804 KB |
Output is correct |
56 |
Correct |
10 ms |
1828 KB |
Output is correct |
57 |
Correct |
10 ms |
1748 KB |
Output is correct |
58 |
Correct |
9 ms |
1728 KB |
Output is correct |
59 |
Correct |
9 ms |
1748 KB |
Output is correct |
60 |
Correct |
10 ms |
1748 KB |
Output is correct |
61 |
Correct |
9 ms |
1688 KB |
Output is correct |
62 |
Correct |
9 ms |
1748 KB |
Output is correct |
63 |
Correct |
10 ms |
1748 KB |
Output is correct |
64 |
Correct |
10 ms |
1748 KB |
Output is correct |
65 |
Correct |
9 ms |
1756 KB |
Output is correct |
66 |
Correct |
10 ms |
1792 KB |
Output is correct |
67 |
Correct |
9 ms |
1608 KB |
Output is correct |
68 |
Correct |
1 ms |
596 KB |
Output is correct |
69 |
Correct |
5 ms |
1364 KB |
Output is correct |
70 |
Correct |
6 ms |
1108 KB |
Output is correct |
71 |
Correct |
5 ms |
1108 KB |
Output is correct |
72 |
Correct |
6 ms |
1108 KB |
Output is correct |
73 |
Correct |
2 ms |
724 KB |
Output is correct |
74 |
Correct |
6 ms |
1024 KB |
Output is correct |
75 |
Correct |
660 ms |
2652 KB |
Output is correct |
76 |
Correct |
763 ms |
2696 KB |
Output is correct |
77 |
Correct |
1012 ms |
3320 KB |
Output is correct |
78 |
Correct |
1014 ms |
3124 KB |
Output is correct |
79 |
Correct |
1016 ms |
3108 KB |
Output is correct |
80 |
Correct |
11 ms |
2004 KB |
Output is correct |
81 |
Correct |
10 ms |
1968 KB |
Output is correct |
82 |
Correct |
12 ms |
2004 KB |
Output is correct |
83 |
Correct |
12 ms |
1960 KB |
Output is correct |
84 |
Correct |
15 ms |
1948 KB |
Output is correct |
85 |
Correct |
11 ms |
1968 KB |
Output is correct |
86 |
Correct |
13 ms |
1996 KB |
Output is correct |
87 |
Correct |
10 ms |
2004 KB |
Output is correct |
88 |
Correct |
19 ms |
1972 KB |
Output is correct |
89 |
Correct |
11 ms |
1980 KB |
Output is correct |
90 |
Correct |
24 ms |
1992 KB |
Output is correct |
91 |
Correct |
22 ms |
1876 KB |
Output is correct |
92 |
Correct |
47 ms |
1964 KB |
Output is correct |
93 |
Correct |
44 ms |
2004 KB |
Output is correct |
94 |
Correct |
42 ms |
1976 KB |
Output is correct |
95 |
Correct |
46 ms |
2000 KB |
Output is correct |
96 |
Correct |
184 ms |
1996 KB |
Output is correct |
97 |
Correct |
830 ms |
2988 KB |
Output is correct |
98 |
Correct |
1211 ms |
3544 KB |
Output is correct |
99 |
Correct |
1299 ms |
3904 KB |
Output is correct |
100 |
Correct |
968 ms |
3496 KB |
Output is correct |