Submission #1985956


Source Code Expand

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<list>
#include<algorithm>
using namespace std;
#define si scanf
#define so printf
#define N 200100
#define M 
#define INF  
#define mod %P
#define nxt t[j]
template<typename TP>inline bool rd(TP& r)
{
	r=0;
	char tmp=getchar();
	while(tmp<'0'||tmp>'9')
	{
		if(tmp==EOF)
			return 0;
		tmp=getchar();
	}
	while('0'<=tmp&&tmp<='9')
	{
		r=(r<<3)+(r<<1)+tmp-'0';
		tmp=getchar();
	}
	return 1;
	
} 
long long P=1000000007,ans;
int n;
int h[N],fa[N][30],x[N*2],t[N*2],tot=1;
void ade(int u,int v)
{
	++tot;
	x[tot]=h[u];
	h[u]=tot;
	t[tot]=v;
}
/*int hd[N],xd[N*2],td[N*2],lst[N];
void ins(int u,int v)
{
	++tot;
	xd[lst]=tot;
	td[tot]=v;
}*/
void fathers()
{
	for(int i=1;(1<<(i-1))<=n;++i)
	for(int j=1;j<=n;++j)
		fa[j][i]=fa[fa[j][i-1]][i-1];
}
long long f[N][2];
long long pl[N],pr[N];
int sons[N];
long long power(long long ori,long long t)
{
	long long re=1;
	while(t)
	{
		if(t&1)
			re=re*ori mod;
		ori=ori*ori mod;
		t>>=1;
	}
	return re;
}
int tr[N*2];
void add(int po,int pl)
{
	for(;po<=n+100;po+=(-po)&po)
		tr[po]+=pl;
}
int ask(int po)
{
	int re=0;
	for(;po;po-=(-po)&po)
		re+=tr[po];
	return re;
}
int l[N],r[N],cnt[N];
int d[N];
list<int> ln[N];
void init(int now)
{
	//ins(d[now],now);
	ln[d[now]].push_back(now);
	l[now]=++tot;
	for(int j=h[now];j;j=x[j])
		d[nxt]=d[now]+1,init(nxt); 
	r[now]=tot;
}
long long g[N];
queue<int> Q;
int getlca(int now)
{
	int u=now;
	for(int i=20;i>=0;--i)
		if(ask(r[fa[u][i]])-ask(l[fa[u][i]]-1)<=0)
			u=fa[u][i];
	u=fa[u][0];
	return u;
}
bool vis[N];
struct sta
{
	int po,dp;
	sta(int pos=0,int dep=0)
	{
		po=pos,dp=dep;
	}
	friend bool operator < (sta a,sta b)
	{
		return a.dp<b.dp;
	}
};
priority_queue<sta> q;
void work(int dp)
{
	int now;
	add(l[0],1);
	while(!q.empty())	q.pop();
	list<int>::iterator it;
	for(it=ln[dp].begin();it!=ln[dp].end();it++)
	{
		add(l[*it],1);
		f[*it][1]=1;
		cnt[*it]=1;
		q.push(sta(*it,d[*it]));
	}
	while(!q.empty())
	{
		now=q.top().po;
		q.pop();
		f[now][0]=(power(2,cnt[now])-f[now][1]+P)mod;
		add(l[now],-(ask(l[now])-ask(l[now]-1)));
		
		//更新lca 
		int lca=getlca(now);
		if(lca!=now)
		{
			f[lca][1]=(f[lca][1]*f[now][0]mod+g[lca]*f[now][1]mod)mod;
			g[lca]=g[lca]*f[now][0]mod;
			cnt[lca]+=cnt[now];
			add(l[lca],1);
			if(!vis[lca])
				q.push(sta(lca,d[lca])),vis[lca]=1;
		}
		
		//清空自己 
		g[now]=1;
		vis[now]=0;
		if(now)
		{
			f[now][0]=0,f[now][1]=0;
			cnt[now]=0;
		}
	}
	tot=0;
	ans=(ans+f[0][1]*power(2,n+1-cnt[0])mod)mod;
	f[0][0]=0,f[0][1]=0;
	cnt[0]=0;
}

int main()
{
	rd(n);
	for(int i=1;i<=n;++i)
	{
		rd(fa[i][0]);
		ade(fa[i][0],i);
	}
	for(int i=0;i<=n;++i)
		g[i]=1;
	fathers();
	tot=0;
	init(0);
	for(int i=0;i<=n;++i)
		if(!ln[i].empty())
			work(i);
		else
			break;
	so("%lld\n",ans);
	return 0;
} 

Submission Info

Submission Time
Task E - Smuggling Marbles
User cdyp
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 3011 Byte
Status AC
Exec Time 409 ms
Memory 54656 KB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 400 / 400 600 / 600
Status
AC × 3
AC × 20
AC × 52
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 4 ms 12800 KB
00_example_02.txt AC 4 ms 12800 KB
00_example_03.txt AC 4 ms 12800 KB
s1_01.txt AC 5 ms 12928 KB
s1_02.txt AC 5 ms 12800 KB
s1_03.txt AC 5 ms 12928 KB
s1_04.txt AC 4 ms 12800 KB
s1_05.txt AC 4 ms 12800 KB
s1_06.txt AC 5 ms 12928 KB
s1_07.txt AC 4 ms 12928 KB
s1_08.txt AC 6 ms 12928 KB
s1_09.txt AC 5 ms 12928 KB
s1_10.txt AC 6 ms 12928 KB
s1_11.txt AC 5 ms 12800 KB
s1_12.txt AC 5 ms 12928 KB
s1_13.txt AC 4 ms 12800 KB
s1_14.txt AC 6 ms 12928 KB
s1_15.txt AC 6 ms 12928 KB
s1_16.txt AC 6 ms 12928 KB
s1_17.txt AC 6 ms 12928 KB
s2_01.txt AC 22 ms 15616 KB
s2_02.txt AC 7 ms 12928 KB
s2_03.txt AC 171 ms 37248 KB
s2_04.txt AC 111 ms 30720 KB
s2_05.txt AC 205 ms 39296 KB
s2_06.txt AC 271 ms 48124 KB
s2_07.txt AC 168 ms 36480 KB
s2_08.txt AC 91 ms 25600 KB
s2_09.txt AC 263 ms 48000 KB
s2_10.txt AC 219 ms 39808 KB
s2_11.txt AC 159 ms 49656 KB
s2_12.txt AC 6 ms 13056 KB
s2_13.txt AC 312 ms 54656 KB
s2_14.txt AC 349 ms 54656 KB
s2_15.txt AC 310 ms 50944 KB
s2_16.txt AC 24 ms 15744 KB
s2_17.txt AC 8 ms 13056 KB
s2_18.txt AC 210 ms 37248 KB
s2_19.txt AC 121 ms 30720 KB
s2_20.txt AC 291 ms 48768 KB
s2_21.txt AC 325 ms 48640 KB
s2_22.txt AC 253 ms 49024 KB
s2_23.txt AC 296 ms 48768 KB
s2_24.txt AC 384 ms 48512 KB
s2_25.txt AC 409 ms 48512 KB
s2_26.txt AC 386 ms 48896 KB
s2_27.txt AC 335 ms 52352 KB
s2_28.txt AC 344 ms 51968 KB
s2_29.txt AC 310 ms 54400 KB
s2_30.txt AC 309 ms 54400 KB
s2_31.txt AC 259 ms 51328 KB
s2_32.txt AC 266 ms 51328 KB