求助!jzoj 1487最大利润

???然后就运行时错误了,第一个数据点输入时一直输入。

#include<iostream>
#include<cstdio>
using namespace std;
struct ooo
{
	int to,next;
}c[100005];
int n,x,y,t,a[100005],f[100005][2],head[100005];
void lil(int yy,int s)
{
	f[s][1]=a[s];
	for(int i=head[s];i;i=c[i].next)
	{
		if(c[i].to==yy) continue;
		if(f[c[i].to][1]==0) lil(s,c[i].to);
		f[s][1]+=f[c[i].to][0];
		f[s][0]+=max(f[c[i].to][1],f[c[i].to][0]);
	}
}
int main()
{
	freopen("profit.in","r",stdin);
	freopen("porfit.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		t++; c[t].to=y; c[t].next=head[x]; head[x]=t;
		t++; c[t].to=x; c[t].next=head[y]; head[y]=t;
		c[++t]=(ooo){y,head[x]}; head[x]=t;
	    c[++t]=(ooo){x,head[y]}; head[y]=t;
	}
	lil(0,1);
	printf("%d",max(f[1][0],f[1][1]));
	fclose(stdin);
	fclose(stdout);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43010386/article/details/86618392