#include "train.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define bit(a , b) (((a)>>(b))&1)
const int maxn = 5e3 + 20;
map<pair<pair<int , int> , int> , bool> mp;
int n , who[maxn];
bool is[maxn];
vector<int> adj[maxn] , out[maxn] , in[maxn];
int get_dp(int mask1 , int mask2 , int v)
{
pair<pair<int , int> , int> tmp = {{mask1 , mask2} , v};
if(mp.count(tmp))
return mp[tmp];
for(auto u : adj[v])
{
int w;
if(bit(mask1 , u))
w = 1;
else if(bit(mask2 , u))
w = 0;
else
{
if(is[u])
w = get_dp(mask1 | mask2 | (1 << u) , 0 , u);
else
w = get_dp(mask1 , mask2 | (1 << u) , u);
}
if(w == who[v])
{
mp[tmp] = who[v];
return who[v];
}
}
mp[tmp] = 1 - who[v];
return 1 - who[v];
}
int DFS(int v)
{
for(auto u : adj[v])
{
int w;
if(u == v)
w = is[v];
else
w = DFS(u);
if(w == who[v])
return who[v];
}
return !who[v];
}
bool visited[maxn] , has[maxn] , wtf[maxn];
vector<int> topol , ver[maxn];
int C , color[maxn];
void dfs(int v)
{
visited[v] = 1;
for(auto u : out[v])
if(!visited[u])
dfs(u);
topol.pb(v);
}
void sfd(int v)
{
visited[v] = 1;
color[v] = C;
ver[C].pb(v);
has[color[v]] |= is[v];
for(auto u : in[v])
if(!visited[u])
sfd(u);
}
vector<int> who_wins(vector<int> a, vector<int> r, vector<int> u, vector<int> v)
{
n = a.size();
bool all = 1;
for(int i = 0; i < n; i++)
{
is[i] = r[i] , who[i] = a[i];
all &= who[i];
}
bool f = 1;
for(int i = 0; i < (int)u.size(); i++)
{
f &= (v[i] - u[i] <= 1) && (v[i] >= u[i]);
adj[u[i]].pb(v[i]);
out[u[i]].pb(v[i]);
in[v[i]].pb(u[i]);
if(v[i] == u[i])
wtf[v[i]] = 1;
}
vector<int> ans;
if(n <= 15)
{
for(int i = 0; i < n; i++)
{
if(is[i])
ans.pb(get_dp(1 << i , 0 , i));
else
ans.pb(get_dp(0 , (1 << i) , i));
}
}
else if(f)
{
for(int i = 0; i < n; i++)
ans.pb(DFS(i));
}
else if(all)
{
for(int i = 0; i < n; i++)
if(!visited[i])
dfs(i);
reverse(topol.begin() , topol.end());
memset(visited , 0 , sizeof visited);
for(auto v : topol)
if(!visited[v])
{
sfd(v);
if((int)ver[C].size() == 1)
has[C] &= wtf[ver[C][0]];
C++;
}
for(int i = C - 1; i >= 0; i--)
for(auto v : ver[i])
for(auto u : out[v])
has[i] |= has[color[u]];
for(int i = 0; i < n; i++)
ans.pb(has[color[i]]);
}
else
{
for(int i = 0; i < n; i++)
if(!visited[i] && !is[i])
dfs(i);
reverse(topol.begin() , topol.end());
memset(visited , 0 , sizeof visited);
for(int i = 0; i < n; i++)
if(is[i])
visited[i] = 1;
for(auto v : topol)
if(!visited[v])
{
sfd(v);
has[C] = (ver[C].size() > 1) || (wtf[ver[C][0]]);
C++;
}
for(int i = C - 1; i >= 0; i--)
for(auto v : ver[i])
for(auto u : out[v])
has[i] |= has[color[u]];
for(int i = 0; i < n; i++)
{
if(!is[i])
is[i] = has[color[i]];
else
is[i] = 0;
}
for(int i = 0; i < C; i++)
ver[i].clear();
memset(has , 0 , sizeof has);
memset(visited , 0 , sizeof visited);
memset(color , 0 , sizeof color);
topol.clear();
C = 0;
for(int i = 0; i < n; i++)
if(!visited[i])
dfs(i);
reverse(topol.begin() , topol.end());
memset(visited , 0 , sizeof visited);
for(auto v : topol)
if(!visited[v])
{
sfd(v);
C++;
}
for(int i = C - 1; i >= 0; i--)
for(auto v : ver[i])
for(auto u : out[v])
has[i] |= has[color[u]];
for(int i = 0; i < n; i++)
ans.pb(has[color[i]]);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1532 KB |
Output is correct |
2 |
Correct |
8 ms |
1528 KB |
Output is correct |
3 |
Correct |
8 ms |
1528 KB |
Output is correct |
4 |
Correct |
8 ms |
1528 KB |
Output is correct |
5 |
Correct |
8 ms |
1528 KB |
Output is correct |
6 |
Correct |
8 ms |
1528 KB |
Output is correct |
7 |
Correct |
8 ms |
1528 KB |
Output is correct |
8 |
Correct |
8 ms |
1528 KB |
Output is correct |
9 |
Correct |
8 ms |
1528 KB |
Output is correct |
10 |
Correct |
7 ms |
1528 KB |
Output is correct |
11 |
Correct |
7 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
760 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
760 KB |
Output is correct |
6 |
Correct |
3 ms |
888 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
888 KB |
Output is correct |
10 |
Correct |
3 ms |
888 KB |
Output is correct |
11 |
Correct |
3 ms |
888 KB |
Output is correct |
12 |
Correct |
5 ms |
1272 KB |
Output is correct |
13 |
Correct |
2 ms |
888 KB |
Output is correct |
14 |
Correct |
3 ms |
1016 KB |
Output is correct |
15 |
Correct |
2 ms |
888 KB |
Output is correct |
16 |
Correct |
2 ms |
760 KB |
Output is correct |
17 |
Correct |
2 ms |
888 KB |
Output is correct |
18 |
Correct |
2 ms |
888 KB |
Output is correct |
19 |
Correct |
2 ms |
760 KB |
Output is correct |
20 |
Correct |
2 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
2296 KB |
Output is correct |
2 |
Correct |
12 ms |
2296 KB |
Output is correct |
3 |
Correct |
12 ms |
2168 KB |
Output is correct |
4 |
Correct |
14 ms |
2040 KB |
Output is correct |
5 |
Correct |
15 ms |
2168 KB |
Output is correct |
6 |
Correct |
15 ms |
2040 KB |
Output is correct |
7 |
Correct |
14 ms |
1912 KB |
Output is correct |
8 |
Correct |
14 ms |
1912 KB |
Output is correct |
9 |
Correct |
13 ms |
2040 KB |
Output is correct |
10 |
Correct |
13 ms |
1912 KB |
Output is correct |
11 |
Correct |
13 ms |
1884 KB |
Output is correct |
12 |
Correct |
13 ms |
2040 KB |
Output is correct |
13 |
Correct |
12 ms |
2168 KB |
Output is correct |
14 |
Correct |
15 ms |
2168 KB |
Output is correct |
15 |
Correct |
15 ms |
2168 KB |
Output is correct |
16 |
Correct |
15 ms |
2168 KB |
Output is correct |
17 |
Correct |
14 ms |
2168 KB |
Output is correct |
18 |
Correct |
10 ms |
1912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
1912 KB |
3rd lines differ - on the 696th token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
2168 KB |
3rd lines differ - on the 2nd token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1532 KB |
Output is correct |
2 |
Correct |
8 ms |
1528 KB |
Output is correct |
3 |
Correct |
8 ms |
1528 KB |
Output is correct |
4 |
Correct |
8 ms |
1528 KB |
Output is correct |
5 |
Correct |
8 ms |
1528 KB |
Output is correct |
6 |
Correct |
8 ms |
1528 KB |
Output is correct |
7 |
Correct |
8 ms |
1528 KB |
Output is correct |
8 |
Correct |
8 ms |
1528 KB |
Output is correct |
9 |
Correct |
8 ms |
1528 KB |
Output is correct |
10 |
Correct |
7 ms |
1528 KB |
Output is correct |
11 |
Correct |
7 ms |
1528 KB |
Output is correct |
12 |
Correct |
3 ms |
888 KB |
Output is correct |
13 |
Correct |
3 ms |
888 KB |
Output is correct |
14 |
Correct |
3 ms |
760 KB |
Output is correct |
15 |
Correct |
3 ms |
760 KB |
Output is correct |
16 |
Correct |
2 ms |
760 KB |
Output is correct |
17 |
Correct |
3 ms |
888 KB |
Output is correct |
18 |
Correct |
3 ms |
888 KB |
Output is correct |
19 |
Correct |
3 ms |
888 KB |
Output is correct |
20 |
Correct |
3 ms |
888 KB |
Output is correct |
21 |
Correct |
3 ms |
888 KB |
Output is correct |
22 |
Correct |
3 ms |
888 KB |
Output is correct |
23 |
Correct |
5 ms |
1272 KB |
Output is correct |
24 |
Correct |
2 ms |
888 KB |
Output is correct |
25 |
Correct |
3 ms |
1016 KB |
Output is correct |
26 |
Correct |
2 ms |
888 KB |
Output is correct |
27 |
Correct |
2 ms |
760 KB |
Output is correct |
28 |
Correct |
2 ms |
888 KB |
Output is correct |
29 |
Correct |
2 ms |
888 KB |
Output is correct |
30 |
Correct |
2 ms |
760 KB |
Output is correct |
31 |
Correct |
2 ms |
888 KB |
Output is correct |
32 |
Correct |
13 ms |
2296 KB |
Output is correct |
33 |
Correct |
12 ms |
2296 KB |
Output is correct |
34 |
Correct |
12 ms |
2168 KB |
Output is correct |
35 |
Correct |
14 ms |
2040 KB |
Output is correct |
36 |
Correct |
15 ms |
2168 KB |
Output is correct |
37 |
Correct |
15 ms |
2040 KB |
Output is correct |
38 |
Correct |
14 ms |
1912 KB |
Output is correct |
39 |
Correct |
14 ms |
1912 KB |
Output is correct |
40 |
Correct |
13 ms |
2040 KB |
Output is correct |
41 |
Correct |
13 ms |
1912 KB |
Output is correct |
42 |
Correct |
13 ms |
1884 KB |
Output is correct |
43 |
Correct |
13 ms |
2040 KB |
Output is correct |
44 |
Correct |
12 ms |
2168 KB |
Output is correct |
45 |
Correct |
15 ms |
2168 KB |
Output is correct |
46 |
Correct |
15 ms |
2168 KB |
Output is correct |
47 |
Correct |
15 ms |
2168 KB |
Output is correct |
48 |
Correct |
14 ms |
2168 KB |
Output is correct |
49 |
Correct |
10 ms |
1912 KB |
Output is correct |
50 |
Incorrect |
12 ms |
1912 KB |
3rd lines differ - on the 696th token, expected: '0', found: '1' |
51 |
Halted |
0 ms |
0 KB |
- |