#include "train.h"
#include <bits/stdc++.h>
#define pb push_back
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define ll long long
#define ff first
#define ss second
#define pii pair<int,int>
#define mk make_pair
#define lp(i,a,b) for(int i = a ; i< b ; i++)
const int MAXN = 5010 ;
using namespace std;
int N , M ;
bool is_a[MAXN] , is_charger[MAXN] ;
vector<int> good_for_a , good_for_b ;
bool marc[MAXN] ;
vector<int> adj[MAXN] , revAdj[MAXN] ;
vector<int> List ;
vector< vector<int> > components ;
int deg[MAXN] ;
void dfs1(int x)
{
marc[x] = true ;
for(auto e : adj[x] )
if(!marc[e] ) dfs1(e) ;
List.push_back(x) ;
}
void dfs2(int x)
{
marc[x] = true ;
components.back().pb(x) ;
for(auto e : revAdj[x])
if(!marc[e]) dfs2(e) ;
}
void goUpwards(vector<int> v)
{
int ini = 0 ;
while(ini < sz(v) )
{
int x = v[ini++] ;
for(auto e : revAdj[x])
{
bool k = good_for_a[e]|good_for_b[e] ;
if(!k && is_a[e] == good_for_a[x] )
{
if(is_a[e]) good_for_a[e] = true ;
else good_for_b[e] = true ;
v.pb(e) ;
}
else if(!k && sz(adj[e]) == 1 )
{
if(is_a[e]) good_for_b[e] = true ;
else good_for_a[e] = true ;
v.pb(e) ;
}
for(int i = 0 ; i < sz(adj[e]) ; i++ )
{
if( adj[e][i] != x ) continue ;
swap(adj[e][i] , adj[e].back() ) ;
adj[e].pop_back() ;
break ;
}
}
revAdj[x].clear() ;
for(auto e : adj[x] )
for(int i= 0 ; i < sz(revAdj[e]) ; i++ )
{
if( revAdj[e][i] != x ) continue ;
swap(revAdj[e][i] , revAdj[e].back() ) ;
revAdj[e].pop_back() ;
break ;
}
adj[x].clear() ;
}
// for(auto e : v ) cout << e <<" " << good_for_a[e] << endl ;
}
bool solve()
{
components.clear() ;
List.clear() ;
for(int i = 0 ; i < N ; i++ ) marc[i] = good_for_a[i]|good_for_b[i] ;
for(int i = 0 ; i < N ; i++ )
{
if(marc[i]) continue ;
deg[i] = 0 ;
for(int j : adj[i] )
if( !marc[j] ) deg[i]++ ;
if(is_a[i]) deg[i] = 1 ;
}
for(int i = 0 ; i < N ; i++ )
if(!marc[i]) dfs1(i) ;
if( List.empty() ) return false ;
for(int i : List ) marc[i] = false ;
for(int i = sz(List)-1 ; i >= 0 ; i-- )
{
if(marc[List[i]]) continue ;
components.push_back(vector<int>()) ;
dfs2(List[i]) ;
}
for(int i : List ) marc[i] = false ;
for(int i = 0 ; i < sz(components)-1 ; i++ )
for(auto e : components[i] ) marc[e] = true ;
for(auto e : components.back() )
{
if( !is_charger[e] ) continue ;
vector<int> fila = {e} ;
marc[e] = true ;
int ini = 0 ;
while( ini < sz(fila) )
{
int x = fila[ini++] ;
for(auto e : revAdj[x] )
{
if( marc[e] || is_charger[e] ) continue ;
if( (--deg[e]) ) continue ;
fila.pb( e ) ;
marc[e] = true ;
}
}
}
vector<int> aux ;
for(int i = 0 ; i < N ; i++ )
if( !marc[i] ) aux.pb(i) ;
if(aux.empty())
{
for(auto e : components.back()) good_for_a[e] = true ;
goUpwards(components.back()) ;
return true ;
}
//for(auto e : aux ) cout << e <<" " ;
//cout << endl ;
List.clear() ;
components.clear() ;
for(auto e : aux )
if(!marc[e]) dfs1(e) ;
for(int i : aux ) marc[i] = false ;
for(int i = sz(List)-1 ; i >= 0 ; i-- )
{
if( marc[ List[i] ] ) continue ;
components.pb(vector<int>()) ;
dfs2(List[i]) ;
}
for(auto e : aux ) marc[e] = false ;
for(int i = 0 ; i < sz(components) ; i++ )
{
bool cool = false ;
for(auto e : components[i] ) marc[e] = true ;
for(auto e : components[i] )
for( auto ee : adj[e] )
cool |= !marc[ee] ;
for(auto e : components[i] ) marc[e] = true ;
if(cool) continue ;
if( sz(components[i]) == 1 )
{
for(auto e : adj[components[i][0]]) cool |= (e == components[i][0] ) ;
cool = !cool ;
}
if(cool) continue ;
//I'm a leaf and a cycle
for(auto e : components[i]) good_for_b[e] = true ;
goUpwards(components[i]) ;
return true ;
}
return true ;
}
vector<int> who_wins( vector<int> a, vector<int> r, vector<int> u, vector<int> v)
{
N = sz(a) ;
M = sz(u) ;
good_for_a.resize( N , 0 ) ;
good_for_b.resize( N , 0 ) ;
lp(i,0,N) is_a[i] = a[i] ;
lp(i,0,N) is_charger[i] = r[i] ;
lp(i,0,M)
{
adj[ u[i] ].pb( v[i] ) ;
revAdj[ v[i] ].pb(u[i]) ;
}
while(solve()) {}
return good_for_a ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
233 ms |
1740 KB |
Output is correct |
2 |
Correct |
247 ms |
1616 KB |
Output is correct |
3 |
Correct |
274 ms |
1612 KB |
Output is correct |
4 |
Correct |
241 ms |
1584 KB |
Output is correct |
5 |
Correct |
268 ms |
1484 KB |
Output is correct |
6 |
Correct |
316 ms |
1488 KB |
Output is correct |
7 |
Correct |
152 ms |
1484 KB |
Output is correct |
8 |
Correct |
132 ms |
1552 KB |
Output is correct |
9 |
Correct |
448 ms |
1528 KB |
Output is correct |
10 |
Correct |
669 ms |
1516 KB |
Output is correct |
11 |
Correct |
1193 ms |
1500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
540 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
246 ms |
2124 KB |
Output is correct |
2 |
Correct |
417 ms |
1992 KB |
Output is correct |
3 |
Correct |
501 ms |
1964 KB |
Output is correct |
4 |
Correct |
12 ms |
1836 KB |
Output is correct |
5 |
Correct |
18 ms |
1940 KB |
Output is correct |
6 |
Correct |
20 ms |
1704 KB |
Output is correct |
7 |
Correct |
11 ms |
1616 KB |
Output is correct |
8 |
Correct |
13 ms |
1612 KB |
Output is correct |
9 |
Correct |
24 ms |
1580 KB |
Output is correct |
10 |
Correct |
21 ms |
1688 KB |
Output is correct |
11 |
Correct |
78 ms |
1552 KB |
Output is correct |
12 |
Correct |
1525 ms |
1868 KB |
Output is correct |
13 |
Correct |
11 ms |
1868 KB |
Output is correct |
14 |
Correct |
11 ms |
1908 KB |
Output is correct |
15 |
Correct |
12 ms |
1836 KB |
Output is correct |
16 |
Correct |
11 ms |
1832 KB |
Output is correct |
17 |
Correct |
11 ms |
1868 KB |
Output is correct |
18 |
Correct |
856 ms |
1680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1612 KB |
Output is correct |
2 |
Correct |
10 ms |
1832 KB |
Output is correct |
3 |
Correct |
15 ms |
1772 KB |
Output is correct |
4 |
Correct |
12 ms |
1740 KB |
Output is correct |
5 |
Correct |
14 ms |
1656 KB |
Output is correct |
6 |
Correct |
19 ms |
1676 KB |
Output is correct |
7 |
Correct |
28 ms |
1652 KB |
Output is correct |
8 |
Correct |
11 ms |
1612 KB |
Output is correct |
9 |
Correct |
1079 ms |
1884 KB |
Output is correct |
10 |
Correct |
12 ms |
1904 KB |
Output is correct |
11 |
Correct |
12 ms |
1868 KB |
Output is correct |
12 |
Correct |
11 ms |
1868 KB |
Output is correct |
13 |
Correct |
12 ms |
1856 KB |
Output is correct |
14 |
Correct |
11 ms |
1716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
1752 KB |
Output is correct |
2 |
Correct |
12 ms |
1868 KB |
Output is correct |
3 |
Correct |
13 ms |
1764 KB |
Output is correct |
4 |
Correct |
14 ms |
1740 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
6 ms |
1356 KB |
Output is correct |
7 |
Correct |
7 ms |
1192 KB |
Output is correct |
8 |
Correct |
7 ms |
1228 KB |
Output is correct |
9 |
Correct |
7 ms |
1312 KB |
Output is correct |
10 |
Correct |
3 ms |
716 KB |
Output is correct |
11 |
Correct |
7 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
233 ms |
1740 KB |
Output is correct |
2 |
Correct |
247 ms |
1616 KB |
Output is correct |
3 |
Correct |
274 ms |
1612 KB |
Output is correct |
4 |
Correct |
241 ms |
1584 KB |
Output is correct |
5 |
Correct |
268 ms |
1484 KB |
Output is correct |
6 |
Correct |
316 ms |
1488 KB |
Output is correct |
7 |
Correct |
152 ms |
1484 KB |
Output is correct |
8 |
Correct |
132 ms |
1552 KB |
Output is correct |
9 |
Correct |
448 ms |
1528 KB |
Output is correct |
10 |
Correct |
669 ms |
1516 KB |
Output is correct |
11 |
Correct |
1193 ms |
1500 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
540 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
246 ms |
2124 KB |
Output is correct |
33 |
Correct |
417 ms |
1992 KB |
Output is correct |
34 |
Correct |
501 ms |
1964 KB |
Output is correct |
35 |
Correct |
12 ms |
1836 KB |
Output is correct |
36 |
Correct |
18 ms |
1940 KB |
Output is correct |
37 |
Correct |
20 ms |
1704 KB |
Output is correct |
38 |
Correct |
11 ms |
1616 KB |
Output is correct |
39 |
Correct |
13 ms |
1612 KB |
Output is correct |
40 |
Correct |
24 ms |
1580 KB |
Output is correct |
41 |
Correct |
21 ms |
1688 KB |
Output is correct |
42 |
Correct |
78 ms |
1552 KB |
Output is correct |
43 |
Correct |
1525 ms |
1868 KB |
Output is correct |
44 |
Correct |
11 ms |
1868 KB |
Output is correct |
45 |
Correct |
11 ms |
1908 KB |
Output is correct |
46 |
Correct |
12 ms |
1836 KB |
Output is correct |
47 |
Correct |
11 ms |
1832 KB |
Output is correct |
48 |
Correct |
11 ms |
1868 KB |
Output is correct |
49 |
Correct |
856 ms |
1680 KB |
Output is correct |
50 |
Correct |
10 ms |
1612 KB |
Output is correct |
51 |
Correct |
10 ms |
1832 KB |
Output is correct |
52 |
Correct |
15 ms |
1772 KB |
Output is correct |
53 |
Correct |
12 ms |
1740 KB |
Output is correct |
54 |
Correct |
14 ms |
1656 KB |
Output is correct |
55 |
Correct |
19 ms |
1676 KB |
Output is correct |
56 |
Correct |
28 ms |
1652 KB |
Output is correct |
57 |
Correct |
11 ms |
1612 KB |
Output is correct |
58 |
Correct |
1079 ms |
1884 KB |
Output is correct |
59 |
Correct |
12 ms |
1904 KB |
Output is correct |
60 |
Correct |
12 ms |
1868 KB |
Output is correct |
61 |
Correct |
11 ms |
1868 KB |
Output is correct |
62 |
Correct |
12 ms |
1856 KB |
Output is correct |
63 |
Correct |
11 ms |
1716 KB |
Output is correct |
64 |
Correct |
16 ms |
1752 KB |
Output is correct |
65 |
Correct |
12 ms |
1868 KB |
Output is correct |
66 |
Correct |
13 ms |
1764 KB |
Output is correct |
67 |
Correct |
14 ms |
1740 KB |
Output is correct |
68 |
Correct |
2 ms |
588 KB |
Output is correct |
69 |
Correct |
6 ms |
1356 KB |
Output is correct |
70 |
Correct |
7 ms |
1192 KB |
Output is correct |
71 |
Correct |
7 ms |
1228 KB |
Output is correct |
72 |
Correct |
7 ms |
1312 KB |
Output is correct |
73 |
Correct |
3 ms |
716 KB |
Output is correct |
74 |
Correct |
7 ms |
1228 KB |
Output is correct |
75 |
Correct |
358 ms |
2048 KB |
Output is correct |
76 |
Correct |
416 ms |
2000 KB |
Output is correct |
77 |
Correct |
504 ms |
1956 KB |
Output is correct |
78 |
Correct |
513 ms |
1956 KB |
Output is correct |
79 |
Correct |
498 ms |
1952 KB |
Output is correct |
80 |
Correct |
12 ms |
1868 KB |
Output is correct |
81 |
Correct |
13 ms |
1740 KB |
Output is correct |
82 |
Correct |
15 ms |
1820 KB |
Output is correct |
83 |
Correct |
15 ms |
1708 KB |
Output is correct |
84 |
Correct |
17 ms |
1732 KB |
Output is correct |
85 |
Correct |
12 ms |
1708 KB |
Output is correct |
86 |
Correct |
18 ms |
1708 KB |
Output is correct |
87 |
Correct |
13 ms |
1668 KB |
Output is correct |
88 |
Correct |
23 ms |
1840 KB |
Output is correct |
89 |
Correct |
14 ms |
1740 KB |
Output is correct |
90 |
Correct |
39 ms |
1744 KB |
Output is correct |
91 |
Correct |
34 ms |
1796 KB |
Output is correct |
92 |
Correct |
101 ms |
1828 KB |
Output is correct |
93 |
Correct |
45 ms |
1740 KB |
Output is correct |
94 |
Correct |
65 ms |
1840 KB |
Output is correct |
95 |
Correct |
73 ms |
1844 KB |
Output is correct |
96 |
Correct |
164 ms |
1700 KB |
Output is correct |
97 |
Correct |
704 ms |
1824 KB |
Output is correct |
98 |
Correct |
779 ms |
1860 KB |
Output is correct |
99 |
Correct |
833 ms |
1860 KB |
Output is correct |
100 |
Correct |
833 ms |
1680 KB |
Output is correct |