#include "bulb.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
int dp[555555]; //what color you get if you keep go left
int dp2[555555]; //what color you get if we go right from you and then spam left
int dp3[555555];
int sub[555555];
int a[555555];
int b[555555];
int dfs(int u)
{
if(dp[u]!=-1) return dp[u];
if(a[u]<0)
{
if(a[u]==-1) return (dp[u]=1);
else return (dp[u]=0);
}
return (dp[u]=dfs(a[u]));
}
int calcsiz(int u)
{
if(u<0) return 0;
if(sub[u]>=0) return sub[u];
return (sub[u]=calcsiz(a[u])+calcsiz(b[u])+1);
}
int dfs3(int u)
{
if(u<0) return 1;
if(dp3[u]>=0) return dp3[u];
if(dp2[u]==0) return (dp3[u]=0);
return (dp3[u]=dfs3(a[u]));
}
int FindWinner(int T, std::vector<int> L, std::vector<int> R)
{
int n = L.size();
for(int i=0;i<n;i++)
{
a[i]=L[i]; b[i]=R[i];
}
//T is useless
memset(sub,-1,sizeof(sub));
memset(dp,-1,sizeof(dp));
memset(dp3,-1,sizeof(dp3));
for(int i=0;i<n;i++)
{
dp[i] = dfs(i);
sub[i] = calcsiz(i);
}
if(!dp[0]) return 0;
//dp[0] = 1
for(int i=0;i<n;i++)
{
if(b[i]<0)
{
if(b[i]==-1) dp2[i]=1;
else dp2[i]=0;
}
else
{
dp2[i]=dp[b[i]];
}
}
for(int i=0;i<n;i++)
{
dp3[i]=dfs3(i);
}
int cur=0;
int cnt=0;
while(cur>=0)
{
//flip cur
int bad=0;
if(b[cur]<0)
{
if(b[cur]==-2) bad=1;
}
else
{
if(dp[b[cur]]==0)
{
if(cnt+sub[cur]<n)
{
bad=1;
}
}
}
if(bad) break;
if(dfs3(b[cur]))
{
return 1;
}
if(!dp2[cur]) break;
cur=a[cur]; cnt++;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
6904 KB |
Output is correct |
2 |
Incorrect |
8 ms |
6876 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
6904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |