Submission #3227158


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define P 1000000007LL
const ll P2 = P*P;
#define MAXN 200010

ll mod(ll &a){
	return ((a >= P) ? (a % P) : (a));
}

struct state {
	ll c0, c1, c2;
	state(){
		c0 = 1, c1 = 1, c2 = 0;
	}

	struct state& operator += (const state &a){
		c2 = (a.c1 + a.c2) *(c1 + c2) + a.c0*c2 + c0*a.c2;
		c1 = a.c1*c0 + a.c0*c1;
		c0 = a.c0*c0;

		c0 = mod(c0);
		c1 = mod(c1);
		c2 = mod(c2);
	}
};

vector<state> ans[MAXN];
int id[MAXN];

int level[MAXN];
int cnt[MAXN];

int p[MAXN];
vector<int> gr[MAXN];

void dfs(int u){
	if(gr[u].size() == 0){
		id[u] = u;
		ans[u].push_back(state());
		return;
	}

	int ma = -1;
	int i_ma = 0;
	for(auto &v : gr[u]){
		level[v] = level[u]+1;
		cnt[level[v]]++;
		dfs(v);
		if((int)ans[id[v]].size() > ma){
			ma = ans[id[v]].size();
			i_ma = v;
		}
	}

	id[u] = id[i_ma];
	int t = ans[id[u]].size();
	for(auto &v : gr[u]){
		if(v == i_ma) continue;
		int t1 = ans[id[v]].size();
		for(int i = 0; i < t1; i++){
			ans[id[u]][t-1- i ] += ans[id[v]][t1-1 - i];
		}
	}
	for(int i = 0; i < ma; i++){
		ans[id[u]][t - 1 - i].c0 += ans[id[u]][t - 1 - i].c2;
		if(ans[id[u]][t - 1 - i].c0 >= P) ans[id[u]][t - 1 - i].c0 -= P;
		ans[id[u]][t - 1 - i].c2 = 0;
	}

	//cout << u << "\n";

	ans[id[u]].push_back(state());
	//for(auto &x : ans[id[u]]) cout << "(" << x.c0 << ", " << x.c1 << ", " << x.c2 << ") ";
	//cout << "\n";
}

ll pot2[MAXN];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	pot2[0] = 1;
	for(int i = 1; i < MAXN; i++){
		pot2[i] = pot2[i-1] << 1;
		if(pot2[i] >= P ) pot2[i] -= P;
	}
	int N;
	cin >> N;

	p[0] = -1;
	cnt[0] = 1;
	
	for(int i = 1; i <= N; i++) {
		cin >> p[i];
		gr[p[i]].push_back(i);
	}

	dfs(0);
	ll sol = 0;
	int t = ans[id[0]].size();
	for(int l = 0; l < t; l++){
		sol += ans[id[0]][t-1-l].c1 * pot2[N + 1 - cnt[l]];
		if(sol >= P2) sol -= P2;
		
	}
	sol %= P;
	cout << sol << "\n";


	return 0;
}

Submission Info

Submission Time
Task E - Smuggling Marbles
User misael
Language C++14 (GCC 5.4.1)
Score 400
Code Size 2047 Byte
Status TLE
Exec Time 3158 ms
Memory 50424 KB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 400 / 400 0 / 600
Status
AC × 3
AC × 20
AC × 43
TLE × 9
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 AC 7 ms 14336 KB
00_example_02.txt AC 7 ms 14336 KB
00_example_03.txt AC 6 ms 14336 KB
s1_01.txt AC 7 ms 14336 KB
s1_02.txt AC 6 ms 14336 KB
s1_03.txt AC 6 ms 14336 KB
s1_04.txt AC 6 ms 14336 KB
s1_05.txt AC 6 ms 14336 KB
s1_06.txt AC 6 ms 14464 KB
s1_07.txt AC 6 ms 14336 KB
s1_08.txt AC 8 ms 14720 KB
s1_09.txt AC 6 ms 14464 KB
s1_10.txt AC 6 ms 14464 KB
s1_11.txt AC 6 ms 14336 KB
s1_12.txt AC 6 ms 14336 KB
s1_13.txt AC 6 ms 14336 KB
s1_14.txt AC 6 ms 14464 KB
s1_15.txt AC 6 ms 14464 KB
s1_16.txt AC 6 ms 14464 KB
s1_17.txt AC 7 ms 14464 KB
s2_01.txt AC 11 ms 15104 KB
s2_02.txt AC 7 ms 14464 KB
s2_03.txt AC 39 ms 19968 KB
s2_04.txt AC 28 ms 18048 KB
s2_05.txt AC 53 ms 21120 KB
s2_06.txt AC 57 ms 22528 KB
s2_07.txt AC 36 ms 19840 KB
s2_08.txt AC 22 ms 17152 KB
s2_09.txt AC 55 ms 22400 KB
s2_10.txt AC 45 ms 21120 KB
s2_11.txt AC 34 ms 21364 KB
s2_12.txt AC 6 ms 14464 KB
s2_13.txt TLE 3158 ms 50424 KB
s2_14.txt TLE 3158 ms 50424 KB
s2_15.txt TLE 3158 ms 46840 KB
s2_16.txt AC 10 ms 15104 KB
s2_17.txt AC 7 ms 14464 KB
s2_18.txt AC 37 ms 20480 KB
s2_19.txt AC 22 ms 18048 KB
s2_20.txt AC 64 ms 24064 KB
s2_21.txt AC 65 ms 24704 KB
s2_22.txt AC 61 ms 23168 KB
s2_23.txt AC 63 ms 24064 KB
s2_24.txt AC 61 ms 26368 KB
s2_25.txt AC 68 ms 27392 KB
s2_26.txt AC 188 ms 28288 KB
s2_27.txt TLE 3158 ms 41876 KB
s2_28.txt TLE 3157 ms 42148 KB
s2_29.txt TLE 3158 ms 50300 KB
s2_30.txt TLE 3158 ms 49144 KB
s2_31.txt TLE 3157 ms 38260 KB
s2_32.txt TLE 3157 ms 37876 KB