Submission #2704375


Source Code Expand

#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 fbo find_by_order
#define ook order_of_key
#define f first
#define s second
#define pb push_back
#define reset(a,b) memset(a,b,sizeof a);
#define MOD 1000000007
#define MID (l+r)/2
#define ALL(x) x.begin(),x.end()
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define mx 200003
#define pc(x) putchar_unlocked(x);
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> pbds;
struct aa{
	long long x,y,z;
};
vector<aa>dp[mx];
long long level[mx],p[mx],n,pake[mx],idx,depth[mx];
vector<int>ve,g[mx];
void dfs(int now,int lev){
	level[now]=depth[now]=lev;
	//debug(now);
	ve.pb(now);
	int cnt=0;
	for(int i:g[now]){
		dfs(i,lev+1);
		depth[now]=max(depth[now],depth[i]);
		cnt=1;
	}
	if(cnt==0){
		pake[now]=++idx;
	}
}

bool cmp(int x,int y){
	return level[x]>level[y];
}

bool cmp2(int x,int y){
	return depth[x]<depth[y];
}

const long long ini=500000004;

int main(){
	cin>>n;
	long long pang=1;
	for(int i=0;i<=n;i++)pang=(pang*2)%MOD;
//	debug(pang);
	for(int i=1;i<=n;i++){
		cin>>p[i];
		g[p[i]].pb(i);
	}
	long long jaw=0;
	dfs(0,1);
	for(int i=0;i<=n;i++)sort(ALL(g[i]),cmp2);
	sort(ALL(ve),cmp);
	for(int i:ve){
		if(pake[i]){
			dp[pake[i]].pb({ini,ini,0});
			continue;
		}
		int cnt=0;
		for(int j:g[i]){
			if(cnt==0 && g[i].size()>1){
				pake[i]=pake[j];
				for(auto &k:dp[pake[i]]){
					k.x=(k.x+k.z)%MOD;
					k.z=0;
				}
				cnt++;
				continue;
			}
			else if(cnt==0){
				pake[i]=pake[j];
				break;
			}
			int x=dp[pake[i]].size()-1,y=dp[pake[j]].size()-1;
			assert(x<=y);
			for(;x>=0 && y>=0;x--,y--){
				auto tmp=dp[pake[j]][y];
				auto &now=dp[pake[j]][y];
				auto sem=dp[pake[i]][x];
				//0 -> 0 dan 2
				now.x=(sem.x*((tmp.x+tmp.z)%MOD))%MOD;
				//1 -> 0 dan 1, 1 dan 0
				now.y=(sem.x*tmp.y)%MOD;
				now.y=(now.y+(sem.y*((tmp.x+tmp.z)%MOD))%MOD)%MOD;
				//2 ->1 dan 1, 2 dan 0, 2 dan 1
				now.z=(sem.y*tmp.y)%MOD;
				now.z=(now.z+(sem.z*((tmp.x+tmp.z)%MOD))%MOD)%MOD;
				now.z=(now.z+(sem.z*tmp.y)%MOD)%MOD;
			}
			if(i==0){
				debug(j);
				debug(depth[j]);
			}
			pake[i]=pake[j];
		}
	//	debug(i);
		dp[pake[i]].pb({ini,ini,0});
	}
	for(auto i:dp[pake[0]]){
		long long sem=(i.y*pang)%MOD;
		jaw=(jaw+sem)%MOD;
	}
	cout<<jaw<<endl;
}


Submission Info

Submission Time
Task E - Smuggling Marbles
User yogahmad
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2504 Byte
Status WA
Exec Time 713 ms
Memory 43376 KB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 0 / 400 0 / 600
Status
WA × 3
AC × 2
WA × 18
AC × 9
WA × 43
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
Subtask1 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt, s1_09.txt, s1_10.txt, s1_11.txt, s1_12.txt, s1_13.txt, s1_14.txt, s1_15.txt, s1_16.txt, s1_17.txt
All 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt, s1_09.txt, s1_10.txt, s1_11.txt, s1_12.txt, s1_13.txt, s1_14.txt, s1_15.txt, s1_16.txt, s1_17.txt, s2_01.txt, s2_02.txt, s2_03.txt, s2_04.txt, s2_05.txt, s2_06.txt, s2_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt, s2_15.txt, s2_16.txt, s2_17.txt, s2_18.txt, s2_19.txt, s2_20.txt, s2_21.txt, s2_22.txt, s2_23.txt, s2_24.txt, s2_25.txt, s2_26.txt, s2_27.txt, s2_28.txt, s2_29.txt, s2_30.txt, s2_31.txt, s2_32.txt
Case Name Status Exec Time Memory
00_example_01.txt WA 6 ms 15872 KB
00_example_02.txt WA 6 ms 15872 KB
00_example_03.txt WA 6 ms 15872 KB
s1_01.txt WA 6 ms 15872 KB
s1_02.txt WA 7 ms 15872 KB
s1_03.txt WA 6 ms 15872 KB
s1_04.txt WA 6 ms 15872 KB
s1_05.txt WA 6 ms 15872 KB
s1_06.txt WA 13 ms 16000 KB
s1_07.txt WA 8 ms 15872 KB
s1_08.txt AC 7 ms 16128 KB
s1_09.txt AC 6 ms 16000 KB
s1_10.txt WA 7 ms 16000 KB
s1_11.txt WA 6 ms 15872 KB
s1_12.txt WA 6 ms 16000 KB
s1_13.txt WA 6 ms 15872 KB
s1_14.txt WA 7 ms 16000 KB
s1_15.txt WA 7 ms 16000 KB
s1_16.txt WA 7 ms 16000 KB
s1_17.txt WA 7 ms 16000 KB
s2_01.txt WA 15 ms 16768 KB
s2_02.txt WA 8 ms 16000 KB
s2_03.txt WA 84 ms 22008 KB
s2_04.txt WA 55 ms 19964 KB
s2_05.txt WA 96 ms 23156 KB
s2_06.txt WA 128 ms 24824 KB
s2_07.txt WA 85 ms 21880 KB
s2_08.txt WA 41 ms 18940 KB
s2_09.txt WA 129 ms 24692 KB
s2_10.txt WA 114 ms 23156 KB
s2_11.txt WA 713 ms 28276 KB
s2_12.txt WA 17 ms 16128 KB
s2_13.txt AC 101 ms 43376 KB
s2_14.txt AC 102 ms 43120 KB
s2_15.txt AC 91 ms 42096 KB
s2_16.txt WA 14 ms 17024 KB
s2_17.txt WA 7 ms 16128 KB
s2_18.txt WA 75 ms 24060 KB
s2_19.txt WA 48 ms 20604 KB
s2_20.txt WA 149 ms 26484 KB
s2_21.txt WA 166 ms 27000 KB
s2_22.txt WA 135 ms 25588 KB
s2_23.txt WA 163 ms 26356 KB
s2_24.txt WA 137 ms 28660 KB
s2_25.txt AC 120 ms 29556 KB
s2_26.txt AC 113 ms 29688 KB
s2_27.txt AC 115 ms 36212 KB
s2_28.txt AC 112 ms 35444 KB
s2_29.txt WA 105 ms 42352 KB
s2_30.txt WA 110 ms 42736 KB
s2_31.txt WA 129 ms 36464 KB
s2_32.txt WA 128 ms 35952 KB