#include "train.h"
#include <queue>
#include <algorithm>
#include <iostream>
#include <cassert>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
enum Player {
Arezou = 1, Borzou = 0
};
enum State {
UNKNOWN = -1, LOSING = 0, POSSIBLY_WINNING = 1
};
int N, M;
VVI G;
VI in_deg;
VI owner;
void bfs(int player, const VI& sources, vector<int>& states) {
queue<int> q;
for (int i : sources)
q.push(i);
VI num_neighbors_required(G.size());
for (int i = 0; i < N; i++) {
if (states[i] == UNKNOWN) {
if (owner[i] == player)
num_neighbors_required[i] = 1;
else
num_neighbors_required[i] = in_deg[i];
}
}
while (!q.empty()) {
int cur = q.front(); q.pop();
for (int nxt : G[cur]) {
if (states[nxt] != UNKNOWN)
continue;
num_neighbors_required[nxt]--;
if (num_neighbors_required[nxt] == 0) {
q.push(nxt);
states[nxt] = player == Arezou ? POSSIBLY_WINNING : LOSING;
}
}
}
}
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r,
std::vector<int> u, std::vector<int> v)
{
owner = a;
N = a.size();
M = u.size();
assert( (int) v.size() == M );
// Build transpose graph G (that is, reverse each edge)
G = VVI(N);
in_deg = VI(N);
for (int j = 0; j < M; ++j) {
G[ v[j] ].push_back( u[j] );
in_deg[ u[j] ]++;
}
vector<int> states(N, UNKNOWN);
for (int ronda = 1; ronda <= 10000; ronda++) {
VI sources;
for (int i = 0; i < N; i++)
if (r[i] == 1 and states[i] != LOSING) {
sources.push_back(i);
states[i] = POSSIBLY_WINNING;
}
bfs(Arezou, sources, states);
/*
cerr << "Round #" << ronda << endl;
for (int i = 0; i < N; i++)
cerr << states[i] << ' ';
cerr << endl;
*/
if (count(states.begin(), states.end(), UNKNOWN) == 0)
break;
sources.clear();
for (int i = 0; i < N; i++) {
switch (states[i]) {
case UNKNOWN:
states[i] = LOSING;
case LOSING:
sources.push_back(i);
break;
case POSSIBLY_WINNING:
states[i] = UNKNOWN;
break;
}
}
bfs(Borzou, sources, states);
/*
for (int i = 0; i < N; i++)
cerr << states[i] << ' ';
cerr << endl;
*/
if (count(states.begin(), states.end(), UNKNOWN) == 0)
break;
}
VI res(N);
for (int i = 0; i < N; i++) {
assert( states[i] != UNKNOWN );
if (states[i] == LOSING)
res[i] = 0;
else
res[i] = 1;
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
980 KB |
Output is correct |
2 |
Correct |
6 ms |
952 KB |
Output is correct |
3 |
Correct |
7 ms |
960 KB |
Output is correct |
4 |
Correct |
6 ms |
856 KB |
Output is correct |
5 |
Correct |
6 ms |
952 KB |
Output is correct |
6 |
Correct |
5 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
8 ms |
852 KB |
Output is correct |
9 |
Correct |
4 ms |
852 KB |
Output is correct |
10 |
Correct |
5 ms |
824 KB |
Output is correct |
11 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
304 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
1344 KB |
Output is correct |
2 |
Correct |
211 ms |
1352 KB |
Output is correct |
3 |
Correct |
304 ms |
1340 KB |
Output is correct |
4 |
Correct |
10 ms |
1316 KB |
Output is correct |
5 |
Correct |
12 ms |
1340 KB |
Output is correct |
6 |
Correct |
10 ms |
1364 KB |
Output is correct |
7 |
Correct |
9 ms |
1344 KB |
Output is correct |
8 |
Correct |
8 ms |
1336 KB |
Output is correct |
9 |
Correct |
8 ms |
1240 KB |
Output is correct |
10 |
Correct |
6 ms |
1236 KB |
Output is correct |
11 |
Correct |
7 ms |
1212 KB |
Output is correct |
12 |
Correct |
9 ms |
1168 KB |
Output is correct |
13 |
Correct |
9 ms |
1236 KB |
Output is correct |
14 |
Correct |
7 ms |
1216 KB |
Output is correct |
15 |
Correct |
8 ms |
1220 KB |
Output is correct |
16 |
Correct |
9 ms |
1216 KB |
Output is correct |
17 |
Correct |
10 ms |
1244 KB |
Output is correct |
18 |
Correct |
458 ms |
1032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1208 KB |
Output is correct |
2 |
Correct |
9 ms |
1116 KB |
Output is correct |
3 |
Correct |
9 ms |
1332 KB |
Output is correct |
4 |
Correct |
8 ms |
1216 KB |
Output is correct |
5 |
Correct |
7 ms |
1340 KB |
Output is correct |
6 |
Correct |
9 ms |
1252 KB |
Output is correct |
7 |
Correct |
10 ms |
1212 KB |
Output is correct |
8 |
Correct |
7 ms |
1300 KB |
Output is correct |
9 |
Correct |
8 ms |
1160 KB |
Output is correct |
10 |
Correct |
8 ms |
1304 KB |
Output is correct |
11 |
Correct |
9 ms |
1352 KB |
Output is correct |
12 |
Correct |
10 ms |
1352 KB |
Output is correct |
13 |
Correct |
9 ms |
1304 KB |
Output is correct |
14 |
Correct |
7 ms |
1188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1352 KB |
Output is correct |
2 |
Correct |
8 ms |
1220 KB |
Output is correct |
3 |
Correct |
8 ms |
1248 KB |
Output is correct |
4 |
Correct |
8 ms |
1232 KB |
Output is correct |
5 |
Correct |
1 ms |
352 KB |
Output is correct |
6 |
Correct |
6 ms |
836 KB |
Output is correct |
7 |
Correct |
5 ms |
864 KB |
Output is correct |
8 |
Correct |
5 ms |
864 KB |
Output is correct |
9 |
Correct |
7 ms |
936 KB |
Output is correct |
10 |
Correct |
2 ms |
480 KB |
Output is correct |
11 |
Correct |
5 ms |
828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
980 KB |
Output is correct |
2 |
Correct |
6 ms |
952 KB |
Output is correct |
3 |
Correct |
7 ms |
960 KB |
Output is correct |
4 |
Correct |
6 ms |
856 KB |
Output is correct |
5 |
Correct |
6 ms |
952 KB |
Output is correct |
6 |
Correct |
5 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
8 ms |
852 KB |
Output is correct |
9 |
Correct |
4 ms |
852 KB |
Output is correct |
10 |
Correct |
5 ms |
824 KB |
Output is correct |
11 |
Correct |
4 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
304 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
304 KB |
Output is correct |
31 |
Correct |
1 ms |
308 KB |
Output is correct |
32 |
Correct |
118 ms |
1344 KB |
Output is correct |
33 |
Correct |
211 ms |
1352 KB |
Output is correct |
34 |
Correct |
304 ms |
1340 KB |
Output is correct |
35 |
Correct |
10 ms |
1316 KB |
Output is correct |
36 |
Correct |
12 ms |
1340 KB |
Output is correct |
37 |
Correct |
10 ms |
1364 KB |
Output is correct |
38 |
Correct |
9 ms |
1344 KB |
Output is correct |
39 |
Correct |
8 ms |
1336 KB |
Output is correct |
40 |
Correct |
8 ms |
1240 KB |
Output is correct |
41 |
Correct |
6 ms |
1236 KB |
Output is correct |
42 |
Correct |
7 ms |
1212 KB |
Output is correct |
43 |
Correct |
9 ms |
1168 KB |
Output is correct |
44 |
Correct |
9 ms |
1236 KB |
Output is correct |
45 |
Correct |
7 ms |
1216 KB |
Output is correct |
46 |
Correct |
8 ms |
1220 KB |
Output is correct |
47 |
Correct |
9 ms |
1216 KB |
Output is correct |
48 |
Correct |
10 ms |
1244 KB |
Output is correct |
49 |
Correct |
458 ms |
1032 KB |
Output is correct |
50 |
Correct |
10 ms |
1208 KB |
Output is correct |
51 |
Correct |
9 ms |
1116 KB |
Output is correct |
52 |
Correct |
9 ms |
1332 KB |
Output is correct |
53 |
Correct |
8 ms |
1216 KB |
Output is correct |
54 |
Correct |
7 ms |
1340 KB |
Output is correct |
55 |
Correct |
9 ms |
1252 KB |
Output is correct |
56 |
Correct |
10 ms |
1212 KB |
Output is correct |
57 |
Correct |
7 ms |
1300 KB |
Output is correct |
58 |
Correct |
8 ms |
1160 KB |
Output is correct |
59 |
Correct |
8 ms |
1304 KB |
Output is correct |
60 |
Correct |
9 ms |
1352 KB |
Output is correct |
61 |
Correct |
10 ms |
1352 KB |
Output is correct |
62 |
Correct |
9 ms |
1304 KB |
Output is correct |
63 |
Correct |
7 ms |
1188 KB |
Output is correct |
64 |
Correct |
9 ms |
1352 KB |
Output is correct |
65 |
Correct |
8 ms |
1220 KB |
Output is correct |
66 |
Correct |
8 ms |
1248 KB |
Output is correct |
67 |
Correct |
8 ms |
1232 KB |
Output is correct |
68 |
Correct |
1 ms |
352 KB |
Output is correct |
69 |
Correct |
6 ms |
836 KB |
Output is correct |
70 |
Correct |
5 ms |
864 KB |
Output is correct |
71 |
Correct |
5 ms |
864 KB |
Output is correct |
72 |
Correct |
7 ms |
936 KB |
Output is correct |
73 |
Correct |
2 ms |
480 KB |
Output is correct |
74 |
Correct |
5 ms |
828 KB |
Output is correct |
75 |
Correct |
167 ms |
1324 KB |
Output is correct |
76 |
Correct |
263 ms |
1340 KB |
Output is correct |
77 |
Correct |
360 ms |
1340 KB |
Output is correct |
78 |
Correct |
364 ms |
1372 KB |
Output is correct |
79 |
Correct |
381 ms |
1340 KB |
Output is correct |
80 |
Correct |
11 ms |
1312 KB |
Output is correct |
81 |
Correct |
8 ms |
1364 KB |
Output is correct |
82 |
Correct |
8 ms |
1236 KB |
Output is correct |
83 |
Correct |
12 ms |
1252 KB |
Output is correct |
84 |
Correct |
8 ms |
1332 KB |
Output is correct |
85 |
Correct |
12 ms |
1276 KB |
Output is correct |
86 |
Correct |
10 ms |
1348 KB |
Output is correct |
87 |
Correct |
10 ms |
1236 KB |
Output is correct |
88 |
Correct |
13 ms |
1328 KB |
Output is correct |
89 |
Correct |
10 ms |
1348 KB |
Output is correct |
90 |
Correct |
19 ms |
1368 KB |
Output is correct |
91 |
Correct |
17 ms |
1360 KB |
Output is correct |
92 |
Correct |
25 ms |
1340 KB |
Output is correct |
93 |
Correct |
28 ms |
1308 KB |
Output is correct |
94 |
Correct |
31 ms |
1456 KB |
Output is correct |
95 |
Correct |
32 ms |
1320 KB |
Output is correct |
96 |
Correct |
79 ms |
1232 KB |
Output is correct |
97 |
Correct |
471 ms |
1340 KB |
Output is correct |
98 |
Correct |
722 ms |
1300 KB |
Output is correct |
99 |
Correct |
743 ms |
1288 KB |
Output is correct |
100 |
Correct |
493 ms |
1056 KB |
Output is correct |