标签:hit pac process int white ace ever cto error
https://codeforces.com/contest/1324/problem/F
you are given a tree, in which the vertices are all printed either black or white. , find out the maximum difference between the number of black vertices and the number of white vertices you can obtain if you choose some subtree contain vertex v. you need to print that maximum of every vertices in the tree.
something we need to notice.
to sum up, what we need is DP with rerooting technique.
the process are as follows
#include <bits/stdc++.h>
#pragma GCC diagnostic error "-std=c++11"
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
//#define endl '\n'
#define all(A) (A).begin(),(A).end()
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define PER(I, A, B) for (int I = (A); I >= (B); --I)
#define lson k*2
#define rson k*2+1
#define fi first
#define se second
#define DB(A) cout<<(A)<<endl
#define DB1(A,B,C) cout<<(A)<<" "<<(B)<<" "<<(C)<<"!"<<endl
#define PB push_back
#define Pair pair<int,int>
#define MP make_pair
#define LL long long
using namespace std;
const int maxn=3e5+10;
const int MAX=1000;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
//head
int dp[maxn];
int note[maxn];
int ans[maxn];
vector<int>a[maxn];
void dfs(int now,int fa=0)
{
dp[now]=note[now];
for (auto to:a[now])
{
if (to==fa) continue;
dfs(to,now);
dp[now]+=max(0,dp[to]);
}
}
void dfs2(int now,int fa=0)
{
ans[now]=dp[now];//
for (auto to:a[now])
{
if (to==fa) continue;
dp[now]-=max(0,dp[to]);
dp[to]+=max(0,dp[now]);
dfs2(to,now);
dp[to]-=max(0,dp[now]);
dp[now]+=max(0,dp[to]);
}
}
int main()
{
SIS;
int n;
cin>>n;
FOR(i,1,n)
{
int x;
cin>>x;
if (x==0) x=-1;
note[i]=x;
}
FOR(i,1,n-1)
{
int x,y;
cin>>x>>y;
a[x].PB(y);
a[y].PB(x);
}
dfs(1);
dfs2(1);
FOR(i,1,n) cout<<ans[i]<<" ";cout<<endl;
}
标签:hit pac process int white ace ever cto error
原文地址:https://www.cnblogs.com/reshuffle/p/12514448.html