#include "train.h"
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;
const int maxN = 5000;
vector<int> nei[maxN];
int charge[maxN], a[maxN];
int ans[maxN];
int col[maxN], isCyc[maxN];
int n;
vector<int> rnei[maxN];
bool used1[maxN];
void DFS1(int v, vector<int>& sorted)
{
used1[v] = true;
for (int to: nei[v])
if (!used1[to])
DFS1(to, sorted);
sorted.push_back(v);
}
void DFS2(int v, int curcol)
{
col[v] = curcol;
for (int to: rnei[v])
if (!col[to])
{
DFS2(to, curcol);
isCyc[curcol] = true;
}
else if (col[to] == curcol)
isCyc[curcol] = true;
}
void SCC()
{
vector<int> sorted;
for (int i = 0; i < n; ++i)
if (!used1[i])
DFS1(i, sorted);
reverse(sorted.begin(), sorted.end());
int cols = 0;
for (int v: sorted)
if (!col[v])
DFS2(v, ++cols);
}
bool used3[maxN];
void DFS3(int v)
{
used3[v] = true;
ans[v] = 1;
for (int to: rnei[v])
if (!used3[to])
DFS3(to);
}
vector<int> Solve3()
{
SCC();
vector<bool> goodcol(n);
for (int v = 0; v < n; ++v)
if (charge[v])
goodcol[col[v]] = true;
for (int v = 0; v < n; ++v)
if (goodcol[col[v]] && isCyc[col[v]] && !used3[v])
DFS3(v);
return vector<int>(ans, ans + n);
}
bool OK(vector<char>& mask)
{
queue<int> cycle;
for (int i = 0; i < n; ++i)
if (mask[i])
cycle.push(i);
int cnt = cycle.size();
while (cycle.size())
{
int u = cycle.front();
cycle.pop();
if (!mask[u]) continue;
bool g = false;
if (a[u])
{
g = true;
for (int to: nei[u])
if (!mask[to] && ans[to] != 0)
g = false;
}
else
{
g = false;
for (int to: nei[u])
if (mask[to] || ans[to] == 0)
g = true;
}
if (!g)
{
--cnt;
mask[u] = 0;
for (int to: rnei[u])
if (mask[to])
cycle.push(to);
}
}
return cnt;
}
void Add(vector<char>& mask)
{
queue<int> cycle;
for (int i = 0; i < n; ++i)
if (mask[i])
for (int to: rnei[i])
cycle.push(to);
while (cycle.size())
{
int u = cycle.front();
cycle.pop();
if (!ans[u]) continue;
bool g = false;
if (a[u])
{
g = true;
for (int to: nei[u])
if (ans[to] != 0)
g = false;
}
else
{
g = false;
for (int to: nei[u])
if (ans[to] == 0)
g = true;
}
if (g)
{
ans[u] = 0;
for (int to: rnei[u])
if (ans[to])
cycle.push(to);
}
}
}
vector<int> Solve()
{
for (int i = 0; i < n; ++i)
ans[i] = 1;
vector<char> stmask(n);
for (int i = 0; i < n; ++i)
if (!charge[i])
stmask[i] = 1;
while (true)
{
auto mask = stmask;
if (!OK(mask))
break;
for (int i = 0; i < n; ++i)
if (mask[i])
{
ans[i] = 0;
stmask[i] = 0;
}
Add(mask);
}
return vector<int>(ans, ans + n);
}
std::vector<int> who_wins(std::vector<int> a_, std::vector<int> charge_, std::vector<int> u, std::vector<int> v) {
n = a_.size();
for (int i = 0; i < n; ++i)
a[i] = a_[i], charge[i] = charge_[i];
for (int i = 0; i < u.size(); ++i)
nei[u[i]].push_back(v[i]);
for (int v = 0; v < n; ++v)
for (int to: nei[v])
rnei[to].push_back(v);
if (!count(a, a + n, 0))
return Solve3();
return Solve();
}
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:173:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < u.size(); ++i)
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1152 KB |
Output is correct |
2 |
Correct |
10 ms |
1152 KB |
Output is correct |
3 |
Correct |
9 ms |
1152 KB |
Output is correct |
4 |
Correct |
9 ms |
1152 KB |
Output is correct |
5 |
Correct |
9 ms |
1152 KB |
Output is correct |
6 |
Correct |
10 ms |
1152 KB |
Output is correct |
7 |
Correct |
11 ms |
1152 KB |
Output is correct |
8 |
Correct |
9 ms |
1152 KB |
Output is correct |
9 |
Correct |
9 ms |
1152 KB |
Output is correct |
10 |
Correct |
8 ms |
1152 KB |
Output is correct |
11 |
Correct |
9 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
640 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1664 KB |
Output is correct |
2 |
Correct |
13 ms |
1664 KB |
Output is correct |
3 |
Correct |
12 ms |
1664 KB |
Output is correct |
4 |
Correct |
14 ms |
1664 KB |
Output is correct |
5 |
Correct |
14 ms |
1536 KB |
Output is correct |
6 |
Correct |
13 ms |
1536 KB |
Output is correct |
7 |
Correct |
15 ms |
1536 KB |
Output is correct |
8 |
Correct |
14 ms |
1536 KB |
Output is correct |
9 |
Correct |
12 ms |
1536 KB |
Output is correct |
10 |
Correct |
13 ms |
1536 KB |
Output is correct |
11 |
Correct |
13 ms |
1408 KB |
Output is correct |
12 |
Correct |
12 ms |
1536 KB |
Output is correct |
13 |
Correct |
14 ms |
1664 KB |
Output is correct |
14 |
Correct |
14 ms |
1664 KB |
Output is correct |
15 |
Correct |
14 ms |
1664 KB |
Output is correct |
16 |
Correct |
14 ms |
1664 KB |
Output is correct |
17 |
Correct |
16 ms |
1664 KB |
Output is correct |
18 |
Correct |
10 ms |
1280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
1280 KB |
Output is correct |
2 |
Correct |
14 ms |
1408 KB |
Output is correct |
3 |
Correct |
13 ms |
1408 KB |
Output is correct |
4 |
Correct |
14 ms |
1408 KB |
Output is correct |
5 |
Correct |
16 ms |
1536 KB |
Output is correct |
6 |
Correct |
17 ms |
1408 KB |
Output is correct |
7 |
Correct |
15 ms |
1408 KB |
Output is correct |
8 |
Correct |
13 ms |
1408 KB |
Output is correct |
9 |
Correct |
12 ms |
1408 KB |
Output is correct |
10 |
Correct |
13 ms |
1408 KB |
Output is correct |
11 |
Correct |
13 ms |
1408 KB |
Output is correct |
12 |
Correct |
13 ms |
1408 KB |
Output is correct |
13 |
Correct |
14 ms |
1408 KB |
Output is correct |
14 |
Correct |
14 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
1408 KB |
Output is correct |
2 |
Correct |
14 ms |
1408 KB |
Output is correct |
3 |
Correct |
15 ms |
1408 KB |
Output is correct |
4 |
Correct |
14 ms |
1408 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
9 ms |
1152 KB |
Output is correct |
7 |
Correct |
11 ms |
1152 KB |
Output is correct |
8 |
Correct |
12 ms |
1152 KB |
Output is correct |
9 |
Correct |
12 ms |
1152 KB |
Output is correct |
10 |
Correct |
7 ms |
768 KB |
Output is correct |
11 |
Correct |
15 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1152 KB |
Output is correct |
2 |
Correct |
10 ms |
1152 KB |
Output is correct |
3 |
Correct |
9 ms |
1152 KB |
Output is correct |
4 |
Correct |
9 ms |
1152 KB |
Output is correct |
5 |
Correct |
9 ms |
1152 KB |
Output is correct |
6 |
Correct |
10 ms |
1152 KB |
Output is correct |
7 |
Correct |
11 ms |
1152 KB |
Output is correct |
8 |
Correct |
9 ms |
1152 KB |
Output is correct |
9 |
Correct |
9 ms |
1152 KB |
Output is correct |
10 |
Correct |
8 ms |
1152 KB |
Output is correct |
11 |
Correct |
9 ms |
1152 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
4 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
512 KB |
Output is correct |
28 |
Correct |
5 ms |
512 KB |
Output is correct |
29 |
Correct |
6 ms |
512 KB |
Output is correct |
30 |
Correct |
5 ms |
640 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
13 ms |
1664 KB |
Output is correct |
33 |
Correct |
13 ms |
1664 KB |
Output is correct |
34 |
Correct |
12 ms |
1664 KB |
Output is correct |
35 |
Correct |
14 ms |
1664 KB |
Output is correct |
36 |
Correct |
14 ms |
1536 KB |
Output is correct |
37 |
Correct |
13 ms |
1536 KB |
Output is correct |
38 |
Correct |
15 ms |
1536 KB |
Output is correct |
39 |
Correct |
14 ms |
1536 KB |
Output is correct |
40 |
Correct |
12 ms |
1536 KB |
Output is correct |
41 |
Correct |
13 ms |
1536 KB |
Output is correct |
42 |
Correct |
13 ms |
1408 KB |
Output is correct |
43 |
Correct |
12 ms |
1536 KB |
Output is correct |
44 |
Correct |
14 ms |
1664 KB |
Output is correct |
45 |
Correct |
14 ms |
1664 KB |
Output is correct |
46 |
Correct |
14 ms |
1664 KB |
Output is correct |
47 |
Correct |
14 ms |
1664 KB |
Output is correct |
48 |
Correct |
16 ms |
1664 KB |
Output is correct |
49 |
Correct |
10 ms |
1280 KB |
Output is correct |
50 |
Correct |
12 ms |
1280 KB |
Output is correct |
51 |
Correct |
14 ms |
1408 KB |
Output is correct |
52 |
Correct |
13 ms |
1408 KB |
Output is correct |
53 |
Correct |
14 ms |
1408 KB |
Output is correct |
54 |
Correct |
16 ms |
1536 KB |
Output is correct |
55 |
Correct |
17 ms |
1408 KB |
Output is correct |
56 |
Correct |
15 ms |
1408 KB |
Output is correct |
57 |
Correct |
13 ms |
1408 KB |
Output is correct |
58 |
Correct |
12 ms |
1408 KB |
Output is correct |
59 |
Correct |
13 ms |
1408 KB |
Output is correct |
60 |
Correct |
13 ms |
1408 KB |
Output is correct |
61 |
Correct |
13 ms |
1408 KB |
Output is correct |
62 |
Correct |
14 ms |
1408 KB |
Output is correct |
63 |
Correct |
14 ms |
1408 KB |
Output is correct |
64 |
Correct |
14 ms |
1408 KB |
Output is correct |
65 |
Correct |
14 ms |
1408 KB |
Output is correct |
66 |
Correct |
15 ms |
1408 KB |
Output is correct |
67 |
Correct |
14 ms |
1408 KB |
Output is correct |
68 |
Correct |
5 ms |
640 KB |
Output is correct |
69 |
Correct |
9 ms |
1152 KB |
Output is correct |
70 |
Correct |
11 ms |
1152 KB |
Output is correct |
71 |
Correct |
12 ms |
1152 KB |
Output is correct |
72 |
Correct |
12 ms |
1152 KB |
Output is correct |
73 |
Correct |
7 ms |
768 KB |
Output is correct |
74 |
Correct |
15 ms |
1024 KB |
Output is correct |
75 |
Correct |
1922 ms |
1656 KB |
Output is correct |
76 |
Correct |
1235 ms |
1784 KB |
Output is correct |
77 |
Correct |
1664 ms |
1784 KB |
Output is correct |
78 |
Correct |
1645 ms |
1784 KB |
Output is correct |
79 |
Correct |
1654 ms |
1736 KB |
Output is correct |
80 |
Correct |
15 ms |
1664 KB |
Output is correct |
81 |
Correct |
14 ms |
1664 KB |
Output is correct |
82 |
Correct |
16 ms |
1664 KB |
Output is correct |
83 |
Correct |
16 ms |
1664 KB |
Output is correct |
84 |
Correct |
16 ms |
1664 KB |
Output is correct |
85 |
Correct |
16 ms |
1664 KB |
Output is correct |
86 |
Correct |
17 ms |
1664 KB |
Output is correct |
87 |
Correct |
14 ms |
1664 KB |
Output is correct |
88 |
Correct |
19 ms |
1664 KB |
Output is correct |
89 |
Correct |
14 ms |
1664 KB |
Output is correct |
90 |
Correct |
24 ms |
1664 KB |
Output is correct |
91 |
Correct |
22 ms |
1664 KB |
Output is correct |
92 |
Correct |
32 ms |
1664 KB |
Output is correct |
93 |
Correct |
44 ms |
1664 KB |
Output is correct |
94 |
Correct |
38 ms |
1792 KB |
Output is correct |
95 |
Correct |
37 ms |
1664 KB |
Output is correct |
96 |
Correct |
164 ms |
1536 KB |
Output is correct |
97 |
Correct |
239 ms |
1664 KB |
Output is correct |
98 |
Correct |
419 ms |
1684 KB |
Output is correct |
99 |
Correct |
258 ms |
1656 KB |
Output is correct |
100 |
Correct |
10 ms |
1408 KB |
Output is correct |