Submission #3010315


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define x first
#define y second

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;

const int N=2010;
const int mod=1000000007;
const int inv2=(mod+1)/2;

vi e[2010];

int dp[2010][2010][3],size[2010];
int fac[2010],pow2[2010];

template <class T>
void read(T &x)
{
	char ch;
	for (ch=getchar();(ch<'0'||ch>'9')&&ch!='-';) ch=getchar();
	x=0;int t=1;if (ch=='-') {ch=getchar();t=-1;}
	for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	x*=t;
}

void dfs(int k)
{
	dp[k][1][0]=1;size[k]=1;
	for (vi::iterator p=e[k].begin();p!=e[k].end();p++)
	{
		dfs(*p);
		for (int j=size[k];j;j--)
			for (int x=2;x>=0;x--)
			{
				for (int l=1;l<=size[*p];l++)
				{
					if (x<2)
						for (int y=0;y<2;y++)
							dp[k][j+l-1][x+1]=(ll(dp[k][j][x])*dp[*p][l][y]+dp[k][j+l-1][x+1])%mod;
					for (int y=0;y<=2;y++)
					{
							dp[k][j+l][x]=(ll(dp[k][j][x])*dp[*p][l][y]%mod*(y==0 ? inv2 : 1)+dp[k][j+l][x])%mod;
							//if (k==2) printf("%d %d %d %d %d\n",dp[2][2][0],j,x,l,y);
					}
				}
				dp[k][j][x]=0;
			}
		size[k]+=size[*p];
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
		//freopen("input.txt","r",stdin);
		//freopen("output.txt","w",stdout);
	#endif
	int n;scanf("%d",&n);
	for (int i=2;i<=n;i++)
	{
		int x;scanf("%d",&x);e[x].pb(i);
	}
	memset(dp,0,sizeof(dp));
	dfs(1);
	fac[0]=1;for (int i=1;i<=n;i++) fac[i]=ll(fac[i-1])*i%mod;
	pow2[0]=1;for (int i=1;i<=n;i++) pow2[i]=pow2[i-1]*2%mod;
	int ans=0;
	for (int i=1;i<=n;i++)
		for (int j=0;j<=2;j++)
		{
			int t=ll(fac[i])*pow2[i]%mod*dp[1][i][j]%mod*(j==0 ? inv2 : 1)%mod;
			if ((n-i)&1) ans=(ans-t+mod)%mod; else ans=(ans+t)%mod;
		}
	printf("%d\n",ans);
	return 0;
}

Submission Info

Submission Time
Task A - Awkward
User fxt
Language C++ (GCC 5.4.1)
Score 1000
Code Size 1851 Byte
Status AC
Exec Time 145 ms
Memory 48000 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:64:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  int n;scanf("%d",&n);
                      ^
./Main.cpp:67:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   int x;scanf("%d",&x);e[x].pb(i);
                       ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 4
AC × 55
Set Name Test Cases
Sample a01, a02, a03, a04
All a01, a02, a03, a04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55
Case Name Status Exec Time Memory
a01 AC 15 ms 47616 KB
a02 AC 15 ms 47616 KB
a03 AC 15 ms 47616 KB
a04 AC 15 ms 47616 KB
b05 AC 15 ms 47616 KB
b06 AC 145 ms 47616 KB
b07 AC 136 ms 48000 KB
b08 AC 136 ms 47744 KB
b09 AC 136 ms 47616 KB
b10 AC 136 ms 47616 KB
b11 AC 136 ms 47616 KB
b12 AC 137 ms 47616 KB
b13 AC 138 ms 47616 KB
b14 AC 145 ms 47616 KB
b15 AC 136 ms 47744 KB
b16 AC 46 ms 47616 KB
b17 AC 16 ms 47616 KB
b18 AC 15 ms 47616 KB
b19 AC 136 ms 47744 KB
b20 AC 136 ms 47744 KB
b21 AC 136 ms 47616 KB
b22 AC 136 ms 47744 KB
b23 AC 138 ms 47616 KB
b24 AC 136 ms 47616 KB
b25 AC 136 ms 47744 KB
b26 AC 144 ms 47616 KB
b27 AC 136 ms 47744 KB
b28 AC 144 ms 47616 KB
b29 AC 136 ms 47872 KB
b30 AC 136 ms 47872 KB
b31 AC 136 ms 47744 KB
b32 AC 136 ms 47872 KB
b33 AC 136 ms 47744 KB
b34 AC 136 ms 47744 KB
b35 AC 136 ms 47744 KB
b36 AC 136 ms 47744 KB
b37 AC 136 ms 47744 KB
b38 AC 136 ms 47744 KB
b39 AC 136 ms 47744 KB
b40 AC 136 ms 47744 KB
b41 AC 136 ms 47744 KB
b42 AC 137 ms 47744 KB
b43 AC 136 ms 47744 KB
b44 AC 136 ms 47744 KB
b45 AC 136 ms 47872 KB
b46 AC 136 ms 48000 KB
b47 AC 136 ms 48000 KB
b48 AC 136 ms 48000 KB
b49 AC 136 ms 48000 KB
b50 AC 144 ms 47616 KB
b51 AC 144 ms 47616 KB
b52 AC 144 ms 47616 KB
b53 AC 144 ms 47616 KB
b54 AC 144 ms 47616 KB
b55 AC 136 ms 47872 KB