#include "train.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a.size())
constexpr int maxn = 5010;
int a[maxn], ligado[maxn], comp[maxn], brabo[maxn], grau[maxn], atv[maxn], cor[maxn];
int n, m, t;
bool mark[maxn];
vector<int> g[maxn], g_rev[maxn], meus[maxn], g_comp[maxn], g_rev_comp[maxn], ord;
void dfs(int u) {
mark[u] = 1;
for(int v : g[u])
if(a[v] && !mark[v]) dfs(v);
ord.pb(u);
}
void go(int u, int& ok) {
ok |= ligado[u]<<1;
comp[u] = t;
meus[t].pb(u);
for(int v : g_rev[u]) {
if(!comp[v] && a[v]) go(v, ok);
else if(comp[v] == comp[u] && a[v]) ok |= 1;
}
}
void limpar(int u) {
brabo[u] = 1, mark[u] = 1;
for(int v : g_rev_comp[u])
if(!mark[v] && !cor[v]) limpar(v);
}
vector<int> who_wins(vector<int> A, vector<int> R, vector<int> U, vector<int> V) {
n = (int)(A.size()), m = (int)(U.size());
for(int i = 0; i < n; i++)
a[i] = A[i], ligado[i] = R[i];
for(int i = 0; i < m; i++)
g[U[i]].pb(V[i]), g_rev[V[i]].pb(U[i]);
for(int i = 0; i < n; i++)
if(a[i] && !mark[i]) dfs(i);
reverse(all(ord));
for(int i = 0; i < sz(ord); i++) {
if(comp[ord[i]]) continue;
int ok = 0; ++t;
go(ord[i], ok);
if(ok == 3) brabo[t] = 1;
if(ok&2) atv[t] = 1;
}
for(int i = 0; i < n; i++)
if(!a[i]) comp[i] = ++t, cor[t] = 1, atv[t] = R[i], meus[t] = {i};
memset(mark, 0, sizeof mark);
for(int i = 1; i <= t; i++) {
for(int u : meus[i])
for(int v : g[u])
if(!mark[comp[v]])
mark[comp[v]] = 1, g_comp[i].pb(comp[v]), g_rev_comp[comp[v]].pb(i);
for(int x : g_comp[i])
mark[x] = 0;
}
memset(mark, 0, sizeof mark);
for(int i = 1; i <= t; i++)
if(brabo[i] && !mark[i]) limpar(i);
for(int rep = 0; rep < n; rep++) {
vector<int> fontes;
memset(mark, 0, sizeof mark);
for(int i = 1; i <= t; i++)
if(atv[i]) mark[i] = 1, fontes.pb(i);
for(int i = 1; i <= t; i++)
grau[i] = sz(g_comp[i]);
while(fontes.size()) {
int u = fontes.back(); fontes.pop_back();
for(int v : g_rev_comp[u]) {
if(mark[v]) continue;
--grau[v];
if(!grau[v] || !cor[v]) fontes.pb(v), mark[v] = 1;
}
}
for(int i = 1; i <= t; i++) {
grau[i] = sz(g_comp[i]);
if(!mark[i] && !brabo[i]) fontes.pb(i);
}
memset(mark, 0, sizeof mark);
for(int x : fontes)
mark[x] = 1;
while(fontes.size()) {
int u = fontes.back(); fontes.pop_back();
for(int v : g_rev_comp[u]) {
if(mark[v]) continue;
--grau[v];
if((!grau[v] && !brabo[v]) || cor[v]) fontes.pb(v), mark[v] = 1;
}
}
for(int i = 1; i <= t; i++)
atv[i] &= !mark[i];
}
vector<int> ans(n);
for(int i = 0; i < n; i++)
ans[i] = !mark[comp[i]];
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
649 ms |
2092 KB |
Output is correct |
2 |
Correct |
694 ms |
2096 KB |
Output is correct |
3 |
Correct |
684 ms |
2092 KB |
Output is correct |
4 |
Correct |
640 ms |
2088 KB |
Output is correct |
5 |
Correct |
651 ms |
2088 KB |
Output is correct |
6 |
Correct |
653 ms |
2080 KB |
Output is correct |
7 |
Correct |
564 ms |
1996 KB |
Output is correct |
8 |
Correct |
617 ms |
2124 KB |
Output is correct |
9 |
Correct |
685 ms |
2056 KB |
Output is correct |
10 |
Correct |
659 ms |
2116 KB |
Output is correct |
11 |
Correct |
472 ms |
2068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
844 KB |
Output is correct |
2 |
Correct |
1 ms |
844 KB |
Output is correct |
3 |
Correct |
1 ms |
932 KB |
Output is correct |
4 |
Correct |
1 ms |
844 KB |
Output is correct |
5 |
Correct |
1 ms |
844 KB |
Output is correct |
6 |
Correct |
1 ms |
844 KB |
Output is correct |
7 |
Correct |
1 ms |
844 KB |
Output is correct |
8 |
Correct |
1 ms |
844 KB |
Output is correct |
9 |
Correct |
1 ms |
844 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
844 KB |
Output is correct |
12 |
Correct |
1 ms |
844 KB |
Output is correct |
13 |
Correct |
1 ms |
844 KB |
Output is correct |
14 |
Correct |
1 ms |
844 KB |
Output is correct |
15 |
Correct |
1 ms |
844 KB |
Output is correct |
16 |
Correct |
1 ms |
844 KB |
Output is correct |
17 |
Correct |
1 ms |
844 KB |
Output is correct |
18 |
Correct |
2 ms |
844 KB |
Output is correct |
19 |
Correct |
1 ms |
844 KB |
Output is correct |
20 |
Correct |
1 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
439 ms |
2884 KB |
Output is correct |
2 |
Correct |
394 ms |
2764 KB |
Output is correct |
3 |
Correct |
388 ms |
2712 KB |
Output is correct |
4 |
Correct |
42 ms |
2124 KB |
Output is correct |
5 |
Correct |
133 ms |
2204 KB |
Output is correct |
6 |
Correct |
145 ms |
2148 KB |
Output is correct |
7 |
Correct |
82 ms |
2104 KB |
Output is correct |
8 |
Correct |
20 ms |
2060 KB |
Output is correct |
9 |
Correct |
24 ms |
2032 KB |
Output is correct |
10 |
Correct |
62 ms |
2008 KB |
Output is correct |
11 |
Correct |
171 ms |
2060 KB |
Output is correct |
12 |
Correct |
641 ms |
2508 KB |
Output is correct |
13 |
Correct |
21 ms |
2124 KB |
Output is correct |
14 |
Correct |
25 ms |
2252 KB |
Output is correct |
15 |
Correct |
26 ms |
2272 KB |
Output is correct |
16 |
Correct |
28 ms |
2176 KB |
Output is correct |
17 |
Correct |
26 ms |
2256 KB |
Output is correct |
18 |
Correct |
439 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
779 ms |
2332 KB |
Output is correct |
2 |
Correct |
1241 ms |
2380 KB |
Output is correct |
3 |
Correct |
727 ms |
2560 KB |
Output is correct |
4 |
Correct |
699 ms |
2584 KB |
Output is correct |
5 |
Correct |
1122 ms |
2612 KB |
Output is correct |
6 |
Correct |
1276 ms |
2568 KB |
Output is correct |
7 |
Correct |
1287 ms |
2572 KB |
Output is correct |
8 |
Correct |
729 ms |
2560 KB |
Output is correct |
9 |
Correct |
1070 ms |
2480 KB |
Output is correct |
10 |
Correct |
1203 ms |
2636 KB |
Output is correct |
11 |
Correct |
918 ms |
2572 KB |
Output is correct |
12 |
Correct |
1250 ms |
2520 KB |
Output is correct |
13 |
Correct |
1182 ms |
2536 KB |
Output is correct |
14 |
Correct |
1296 ms |
2420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1400 ms |
2532 KB |
Output is correct |
2 |
Correct |
181 ms |
2252 KB |
Output is correct |
3 |
Correct |
961 ms |
2420 KB |
Output is correct |
4 |
Correct |
708 ms |
2276 KB |
Output is correct |
5 |
Correct |
3 ms |
972 KB |
Output is correct |
6 |
Correct |
530 ms |
1916 KB |
Output is correct |
7 |
Correct |
14 ms |
1612 KB |
Output is correct |
8 |
Correct |
13 ms |
1648 KB |
Output is correct |
9 |
Correct |
14 ms |
1668 KB |
Output is correct |
10 |
Correct |
3 ms |
1100 KB |
Output is correct |
11 |
Correct |
27 ms |
1612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
649 ms |
2092 KB |
Output is correct |
2 |
Correct |
694 ms |
2096 KB |
Output is correct |
3 |
Correct |
684 ms |
2092 KB |
Output is correct |
4 |
Correct |
640 ms |
2088 KB |
Output is correct |
5 |
Correct |
651 ms |
2088 KB |
Output is correct |
6 |
Correct |
653 ms |
2080 KB |
Output is correct |
7 |
Correct |
564 ms |
1996 KB |
Output is correct |
8 |
Correct |
617 ms |
2124 KB |
Output is correct |
9 |
Correct |
685 ms |
2056 KB |
Output is correct |
10 |
Correct |
659 ms |
2116 KB |
Output is correct |
11 |
Correct |
472 ms |
2068 KB |
Output is correct |
12 |
Correct |
2 ms |
844 KB |
Output is correct |
13 |
Correct |
1 ms |
844 KB |
Output is correct |
14 |
Correct |
1 ms |
932 KB |
Output is correct |
15 |
Correct |
1 ms |
844 KB |
Output is correct |
16 |
Correct |
1 ms |
844 KB |
Output is correct |
17 |
Correct |
1 ms |
844 KB |
Output is correct |
18 |
Correct |
1 ms |
844 KB |
Output is correct |
19 |
Correct |
1 ms |
844 KB |
Output is correct |
20 |
Correct |
1 ms |
844 KB |
Output is correct |
21 |
Correct |
1 ms |
896 KB |
Output is correct |
22 |
Correct |
1 ms |
844 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
844 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
844 KB |
Output is correct |
27 |
Correct |
1 ms |
844 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
2 ms |
844 KB |
Output is correct |
30 |
Correct |
1 ms |
844 KB |
Output is correct |
31 |
Correct |
1 ms |
844 KB |
Output is correct |
32 |
Correct |
439 ms |
2884 KB |
Output is correct |
33 |
Correct |
394 ms |
2764 KB |
Output is correct |
34 |
Correct |
388 ms |
2712 KB |
Output is correct |
35 |
Correct |
42 ms |
2124 KB |
Output is correct |
36 |
Correct |
133 ms |
2204 KB |
Output is correct |
37 |
Correct |
145 ms |
2148 KB |
Output is correct |
38 |
Correct |
82 ms |
2104 KB |
Output is correct |
39 |
Correct |
20 ms |
2060 KB |
Output is correct |
40 |
Correct |
24 ms |
2032 KB |
Output is correct |
41 |
Correct |
62 ms |
2008 KB |
Output is correct |
42 |
Correct |
171 ms |
2060 KB |
Output is correct |
43 |
Correct |
641 ms |
2508 KB |
Output is correct |
44 |
Correct |
21 ms |
2124 KB |
Output is correct |
45 |
Correct |
25 ms |
2252 KB |
Output is correct |
46 |
Correct |
26 ms |
2272 KB |
Output is correct |
47 |
Correct |
28 ms |
2176 KB |
Output is correct |
48 |
Correct |
26 ms |
2256 KB |
Output is correct |
49 |
Correct |
439 ms |
2392 KB |
Output is correct |
50 |
Correct |
779 ms |
2332 KB |
Output is correct |
51 |
Correct |
1241 ms |
2380 KB |
Output is correct |
52 |
Correct |
727 ms |
2560 KB |
Output is correct |
53 |
Correct |
699 ms |
2584 KB |
Output is correct |
54 |
Correct |
1122 ms |
2612 KB |
Output is correct |
55 |
Correct |
1276 ms |
2568 KB |
Output is correct |
56 |
Correct |
1287 ms |
2572 KB |
Output is correct |
57 |
Correct |
729 ms |
2560 KB |
Output is correct |
58 |
Correct |
1070 ms |
2480 KB |
Output is correct |
59 |
Correct |
1203 ms |
2636 KB |
Output is correct |
60 |
Correct |
918 ms |
2572 KB |
Output is correct |
61 |
Correct |
1250 ms |
2520 KB |
Output is correct |
62 |
Correct |
1182 ms |
2536 KB |
Output is correct |
63 |
Correct |
1296 ms |
2420 KB |
Output is correct |
64 |
Correct |
1400 ms |
2532 KB |
Output is correct |
65 |
Correct |
181 ms |
2252 KB |
Output is correct |
66 |
Correct |
961 ms |
2420 KB |
Output is correct |
67 |
Correct |
708 ms |
2276 KB |
Output is correct |
68 |
Correct |
3 ms |
972 KB |
Output is correct |
69 |
Correct |
530 ms |
1916 KB |
Output is correct |
70 |
Correct |
14 ms |
1612 KB |
Output is correct |
71 |
Correct |
13 ms |
1648 KB |
Output is correct |
72 |
Correct |
14 ms |
1668 KB |
Output is correct |
73 |
Correct |
3 ms |
1100 KB |
Output is correct |
74 |
Correct |
27 ms |
1612 KB |
Output is correct |
75 |
Correct |
425 ms |
2584 KB |
Output is correct |
76 |
Correct |
512 ms |
2616 KB |
Output is correct |
77 |
Correct |
472 ms |
2508 KB |
Output is correct |
78 |
Correct |
443 ms |
2528 KB |
Output is correct |
79 |
Correct |
454 ms |
2628 KB |
Output is correct |
80 |
Correct |
921 ms |
2396 KB |
Output is correct |
81 |
Correct |
817 ms |
2464 KB |
Output is correct |
82 |
Correct |
1321 ms |
2540 KB |
Output is correct |
83 |
Correct |
198 ms |
2232 KB |
Output is correct |
84 |
Correct |
218 ms |
2312 KB |
Output is correct |
85 |
Correct |
695 ms |
2556 KB |
Output is correct |
86 |
Correct |
185 ms |
2124 KB |
Output is correct |
87 |
Correct |
679 ms |
2552 KB |
Output is correct |
88 |
Correct |
782 ms |
2512 KB |
Output is correct |
89 |
Correct |
1179 ms |
2548 KB |
Output is correct |
90 |
Correct |
647 ms |
2540 KB |
Output is correct |
91 |
Correct |
366 ms |
2252 KB |
Output is correct |
92 |
Correct |
451 ms |
2324 KB |
Output is correct |
93 |
Correct |
976 ms |
2548 KB |
Output is correct |
94 |
Correct |
772 ms |
2504 KB |
Output is correct |
95 |
Correct |
947 ms |
2488 KB |
Output is correct |
96 |
Correct |
1349 ms |
2424 KB |
Output is correct |
97 |
Correct |
671 ms |
2588 KB |
Output is correct |
98 |
Correct |
869 ms |
2472 KB |
Output is correct |
99 |
Correct |
850 ms |
2524 KB |
Output is correct |
100 |
Correct |
429 ms |
2252 KB |
Output is correct |