Codeforces660 C. Uncle Bogdan and Country Happiness(dfs模拟)

Uncle Bogdan and Country Happiness

Uncle Bogdan is in captain Flint’s crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.

There are n cities and n−1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.

There are m citizens living in the country. A pi people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.

Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won’t improve it.

Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index hi as the number of people in good mood minus the number of people in bad mood. Let’s say for the simplicity that mood of a person doesn’t change inside the city.

Happiness detector is still in development, so there is a probability of a mistake in judging a person’s happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.

Uncle Bogdan successfully solved the problem. Can you do the same?

More formally, You need to check: “Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to hi”.

Input
The first line contains a single integer t (1≤t≤10000) — the number of test cases.

The first line of each test case contains two integers n and m (1≤n≤105; 0≤m≤109) — the number of cities and citizens.

The second line of each test case contains n integers p1,p2,…,pn (0≤pi≤m; p1+p2+…+pn=m), where pi is the number of people living in the i-th city.

The third line contains n integers h1,h2,…,hn (−109≤hi≤109), where hi is the calculated happiness index of the i-th city.

Next n−1 lines contain description of the roads, one per line. Each line contains two integers xi and yi (1≤xi,yi≤n; xi≠yi), where xi and yi are cities connected by the i-th road.

It’s guaranteed that the sum of n from all test cases doesn’t exceed 2⋅105.

Output
For each test case, print YES, if the collected data is correct, or NO — otherwise. You can print characters in YES or NO in any case.

思路:
以1为根,dfs时记录每个城市的子城市的开心人数、难过人数之和(该城市的必经人数)。再根据当前城市的h值与p值计算出当前城市的开心、难过人数。在计算人数时判断是否合理。(心情只能从开心转为难过,即当前城市的开心人数必须大于子城市开心人数之和)

代码

#include<bits/stdc++.h>
#define ll long long
#define LL long long
#define PB push_back
#define MP make_pair
using namespace std;
const int maxn=2e5+100;
const ll inf=1e18+10;
vector<int>g[maxn];
ll n,m,a[maxn],h[maxn],ok=1;
ll num[maxn][5];
void dfs1(int pre,int now){
	if(g[now].size()==1&&pre){
		if(a[now]<abs(h[now])||(h[now]+a[now])%2==1)ok=0;
		num[now][1]=(h[now]+a[now])/2;
		num[now][0]=a[now]-num[now][1];
		return;
	}
	for(int i=0;i<g[now].size();i++){
		int x=g[now][i];
		if(x!=pre){
			dfs1(now,x);
			num[now][1]+=num[x][1];
			num[now][0]+=num[x][0];
		}
	}
	int sum=num[now][0]+num[now][1]+a[now];
	int ha=(sum+h[now])/2;
	int uh=sum-ha;
	if(sum<abs(h[now])||(h[now]+sum)%2==1)ok=0;
	if(ha<num[now][1])ok=0;
	num[now][1]=ha,num[now][0]=uh;
}

void slove(){
	cin>>n>>m;
	ok=1;
	for(int i=1;i<=n;i++){
		g[i].clear();
		num[i][0]=num[i][1]=0;
	}
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)cin>>h[i];
	for(int i=1;i<n;i++){
		int x,y;
		cin>>x>>y;
		g[x].PB(y);
		g[y].PB(x);
	}
	dfs1(0,1);
	if(ok)puts("YES");
	else puts("NO");
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		slove();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值