# Maximum White Subtree

0
10

https://blog.csdn.net/starlet_kiss/article/details/104844691

https://www.cnblogs.com/cjtcalc/p/12485536.html

dfs解法

```//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXX=200100;
int color[MAXX],sum[MAXX],ans[MAXX],fu[MAXX];
int tot=0,n;

/*
int x=0,f=1;
char s=getchar();
while(s<0||s>9){if(s=='-'){f=-f;s=getchar();}}
while(s>=0&&s<=9){x=x*10+s-'0';s=getchar();}
return x*f;
*/
char x=getchar();
while(x==' '||x=='\n')x=getchar();//
if(x=='0')      return -1;
else if(x=='1') return 1;
else            return 0;
}

}

void dfs(int u,int f){
fu[u]=f;
sum[u]=color[u];
int to=edge[i];
if(to==f)continue;
dfs(to,u);
if(sum[to]>0)sum[u]+=sum[to];
}
}

void DFS(int u,int f){
if(sum[u]>=0)ans[u]=max(sum[u],ans[f]);
else if(ans[f]>0)ans[u]=ans[f]+sum[u];
else ans[u]=sum[u];
int to=edge[i];
if(to==f)continue;
DFS(to,u);
}
}

int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
}
int a,b;
for(int i=1;i<n;i++){
scanf("%d%d",&a,&b);
}
ans[0]=sum[0]=-1*MAXX;
dfs(1,0);
ans[1]=sum[1];
DFS(1,0);
for(int i=1;i<=n;i++){printf("%d ",ans[i]);}
printf("\n");
return 0;
}```

<