贪心52pts求调
查看原帖
贪心52pts求调
1176197
ljcnoi楼主2024/10/19 12:54

请问该题能否使用贪心的思路,正确的思路是什么? 这道题,我只能想到用贪心骗了52pts……

#include<bits/stdc++.h>
using namespace std;
int c,t,n,sr,jl;
long long int w[100005];
vector<int> son[100005];
char xx;
inline long long int read()
{
	long long int tot=0;
	int k=1;
	while(xx<'0'||xx>'9')
	{
		if(xx=='-')
		{
			k=-1;
		}
		xx=getchar();
	}
	while(xx>='0'&&xx<='9')
	{
		tot=tot*10+xx-'0';
		xx=getchar();
	}
	return tot*k;
}
void change(int x,long long int y)
{
	long long int z=0;
	w[x]=w[x]+y;
	if(w[x]<0)
	{
		z=-w[x];
		w[x]=0;
	}
	for(int i=0;i<son[x].size();i++)
	{
		change(son[x][i],y+z);
	}
} 
void dfss(int x)
{
	long long int tot=0;
	for(int i=0;i<son[x].size();i++)
	{
		dfss(son[x][i]);
		tot=tot+w[son[x][i]];
	}
	if(tot>w[x])
	{
		jl=true;
	}
}
int main()
{
	c=read();
	t=read();
	for(int i=1;i<=t;i++)
	{
		n=read();
		for(int j=1;j<=n;j++)
		{
			son[j].clear();
		}
		for(int j=2;j<=n;j++)
		{
			sr=read();
			//cout<<sr<<" ";
			son[sr].push_back(j);
		}
		//cout<<endl;
		for(int j=1;j<=n;j++)
		{
			w[j]=read();
		}
		change(1,0);
		jl=false;
		dfss(1);
		if(jl)
		{
			printf("Shuiniao\n");
		}
		else
		{
			printf("Huoyu\n");
		}
	}
	return 0;
 } 
2024/10/19 12:54
加载中...