This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bulb.h"
using namespace std;
 
int N, dlr[303030][2], dcn, par[303030], sw[303030], qsc, cha, ch[2], valid=-1, lose[303030];
vector<int> L, R;
 
int lca(int a, int b){
	if(a==-1) return b;
	if(a==-2) return a;
	while(a>=0){
		if(dlr[a][0] <= dlr[b][0] && dlr[b][0] <= dlr[a][1]) break;
		a=par[a];
	}
	return a;
}
 
void pre(int ix){
	if(ix<0) return;
	dlr[ix][0]=dcn++;
	pre(L[ix]), pre(R[ix]);
	dlr[ix][1]=dcn-1;
}
 
void dfs(int pv, int ix){
	if(ix==-1) return;
	if(ix==-2){
		if(cha==0) valid=-2;
		if(cha==1 && qsc) valid=lca(valid,pv), lose[ch[0]]=1;
		if(cha==2) lose[ch[0]]=lose[ch[1]]=1;
		return;
	}
	par[ix]=pv;
	sw[ix]=0, qsc--;
	dfs(ix, L[ix]);
	if(cha<2) ch[cha]=ix;
	sw[ix]=1, cha++;
	dfs(ix, R[ix]);
	sw[ix]=2, cha--, qsc++;
}
 
int FindWinner(int T, vector<int> L_, vector<int> R_){
	N=L_.size(), L=L_, R=R_;
	for(int i=0; i<N; i++) sw[i]=2; qsc=N;
	pre(0);
	dfs(-1, 0);
	if(valid==-2) return 0;
	if(valid==-1){
		for(int i=0; i<N; i++){
			if(!lose[i]) return 1;
		}
		return 0;
	}
	for(int i=valid; i>=0; i=par[i]){
		if(!lose[i]) return 1;
	}
	return 0;
}
Compilation message (stderr)
bulb.cpp: In function 'int FindWinner(int, std::vector<int>, std::vector<int>)':
bulb.cpp:43:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for(int i=0; i<N; i++) sw[i]=2; qsc=N;
  ^~~
bulb.cpp:43:34: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for(int i=0; i<N; i++) sw[i]=2; qsc=N;
                                  ^~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |