General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
73134886 Practice:
nayanava.de01
1324F - 9 Java 8 Time limit exceeded on test 42 2000 ms 29180 KB 2020-03-13 14:12:28 2020-03-13 14:12:28
→ Source
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MaximumWhiteSubtree {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        Info[] adj = new Info[n];
        int[] dp = new int[n];
        for(int i = 0; i < n; i++) {
            adj[i] = new Info(s.nextInt());
            if(adj[i].val == 0) {
                adj[i].val = -1;
            }
        }
        for(int i = 0; i < n-1; i++) {
            int u = s.nextInt()-1;
            int v = s.nextInt()-1;
            adj[u].list.add(v);
            adj[v].list.add(u);
        }
        postorderDfs(adj, 0, -1, dp);
        preorderDfs(adj, 0, -1, dp);
        for(int i = 0; i < n; i++) {
            System.out.print(dp[i] + " ");
        }
    }

    private static void preorderDfs(Info[] adj, int index, int parent, int[] dp) {
        for(int next : adj[index].list) {
            if(next == parent) {
                continue;
            }
            dp[next] += Math.max(dp[index] - Math.max(dp[next], 0), 0);
            preorderDfs(adj, next, index, dp);
        }
    }

    private static void postorderDfs(Info[] adj, int index, int parent, int[] in) {
        int max = adj[index].val;
        for(int next : adj[index].list) {
            if(next == parent) {
                continue;
            }
            postorderDfs(adj, next, index, in);
            max = Math.max(max, max + Math.max(in[next], 0));
        }
        in[index] = max;
    }


    static class Info {
        int val;
        List<Integer> list;
        Info(int val) {
            this.val = val;
            this.list = new ArrayList<>();
        }
    }
}
?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details