[树]|[树] cf 1388 C. Uncle Bogdan and Country Happiness

题目 [树]|[树] cf 1388 C. Uncle Bogdan and Country Happiness
文章图片

题目链接:https://codeforces.com/contest/1388/problem/C
思路 【[树]|[树] cf 1388 C. Uncle Bogdan and Country Happiness】设x为好心情 y为坏心情 x+y=p x-y=h =>y=(p-h)/2 p-h 一定为偶数
代码

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if __cplusplus >= 201103L #include #include #endif #define ll long long using namespace std; const int INF = 0x3f3f3f3f; int num[100010],h[100010],peo[100010],bad[100010],sum[100010]; vector v[100010]; bool flag=0; int dfs(int u,int fa){ peo[u]=num[u]; for(int i=0; i>t; while(t--){ int n,m; cin>>n>>m; flag=0; for(int i=1; i<=n; i++) cin>>num[i],v[i].clear(); for(int i=1; i<=n; i++) cin>>h[i]; for(int i=1; i<=n-1; i++){ int x,y; cin>>x>>y; v[x].push_back(y); v[y].push_back(x); } dfs(1,0); bool flag=1; for(int i=1; i<=n; i++){ if((peo[i]-h[i])%2==0) bad[i]=(peo[i]-h[i])/2; else{ flag=0; break; } } if(!flag){ cout<<"NO"<num[i]+sum[i]||bad[i]<0) flag=0; } if(flag) cout<<"YES"<

    推荐阅读