-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumwhitesubtree.java
More file actions
47 lines (46 loc) · 1.32 KB
/
maximumwhitesubtree.java
File metadata and controls
47 lines (46 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.*;
import java.util.*;
public class maximumwhitesubtree {
static LinkedList<Integer>[] adj;
static boolean[] vis;
static int[] a,count,ans;
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
StringBuilder sb=new StringBuilder();
int n=sc.nextInt();
adj=new LinkedList[n];
for(int i=0;i<n;i++)adj[i]=new LinkedList<>();
a =new int[n];
count=new int[n];
ans=new int[n];
Arrays.fill(ans,Integer.MAX_VALUE);
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
if(a[i]==0)a[i]--;
}
for(int i=0;i<n-1;i++){
int u=sc.nextInt()-1,v=sc.nextInt()-1;
adj[u].add(v);
adj[v].add(u);
}
dfs1(0,0);
dfs2(0,0,0);
for(int i=0;i<n;i++)sb.append(ans[i]+" ");
System.out.println(sb);
}
static void dfs1(int i,int par){
for(int node:adj[i]){
if(node!=par){
dfs1(node,i);
count[i]+=Math.max(count[node],0);
}
}
count[i]+=a[i];
}
static void dfs2(int i,int par,int v){
ans[i]=count[i]+v;
for(int node:adj[i]){
if(node!=par)dfs2(node,i,Math.max(ans[i]-Math.max(count[node],0),0));
}
}
}