Submission #1871752


Source Code Expand

int n,x[5010],y[5010];
int tote,FIR[5010],TO[5010],NEXT[5010];
bool vis[5010];

void addedge(int u,int v)
{
	TO[++tote]=v;
	NEXT[tote]=FIR[u];
	FIR[u]=tote;
}

bool dfs(int u)
{
	for (int p=FIR[u];p;p=NEXT[p])
	{
		int v=TO[p];
		if (!dfs(v)) return 0;
	}
	memset(vis,0,sizeof(vis));
	vis[0]=1;
	int i,S0=0,S1=0;
	for (int p=FIR[u];p;p=NEXT[p])
	{
		int v=TO[p];
		S0+=min(x[v],y[v]);
		S1+=max(x[v],y[v]);
		int tmp=abs(x[v]-y[v]);
		for (i=x[u]-tmp;i>=0;i--)
			vis[i+tmp]|=vis[i];
	}
	if (S0>x[u]) return 0;
	for (i=x[u]-S0;i>=0;i--)
		if (vis[i]) {y[u]=S1-i; break;}
	return 1;
}

int main()
{
#ifdef h10
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	int i,f;
	scanf("%d",&n);
	for (i=2;i<=n;i++)
	{
		scanf("%d",&f);
		addedge(f,i);
	}
	for (i=1;i<=n;i++)
		scanf("%d",&x[i]);
	puts(dfs(1)?"POSSIBLE":"IMPOSSIBLE");
}

Submission Info

Submission Time
Task E - Bichrome Tree
User h10
Language C++14 (GCC 5.4.1)
Score 0
Code Size 896 Byte
Status CE

Compile Error

./Main.cpp: In function ‘bool dfs(int)’:
./Main.cpp:19:26: error: ‘memset’ was not declared in this scope
  memset(vis,0,sizeof(vis));
                          ^
./Main.cpp:25:20: error: ‘min’ was not declared in this scope
   S0+=min(x[v],y[v]);
                    ^
./Main.cpp:26:20: error: ‘max’ was not declared in this scope
   S1+=max(x[v],y[v]);
                    ^
./Main.cpp:27:24: error: ‘abs’ was not declared in this scope
   int tmp=abs(x[v]-y[v]);
                        ^
./Main.cpp: In function ‘int main()’:
./Main.cpp:44:15: error: ‘scanf’ was not declared in this scope
  scanf("%d",&n);
               ^
./Main.cpp:52:37: error: ‘puts’ was not declared in this scope
  puts(dfs(1)?"POSSIBLE":"IMPOSSIBLE");
                                     ^