Submission #309916

#TimeUsernameProblemLanguageResultExecution timeMemory
309916ijxjdjdPower Plant (JOI20_power)Java
100 / 100
1314 ms68452 KiB
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class power { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); powersol solver = new powersol(); solver.solve(1, in, out); out.close(); } static class powersol { char[] s; int[][] adj; int ans = 0; public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int[] U = new int[N - 1]; int[] V = new int[N - 1]; for (int i = 0; i < N - 1; i++) { U[i] = in.nextInt() - 1; V[i] = in.nextInt() - 1; } adj = DefaultTree.packU(N, U, V); s = in.next().toCharArray(); dfs(0, 0); out.println(ans); } int dfs(int n, int p) { int mx = 0; int tot = 0; for (int e : adj[n]) { if (e != p) { int a = dfs(e, n); mx = Math.max(mx, a); tot += a; } } ans = Math.max(ans, tot - (s[n] == '1' ? 1 : 0)); if (s[n] == '1') { ans = Math.max(ans, mx + 1); } if (s[n] == '1') { return Math.max(1, tot - 1); } return tot; } } static class DefaultTree { DefaultTree.Node[] tree; int K; int[][] lift; int[] tin; int[] tout; public DefaultTree(int N) { tree = new DefaultTree.Node[N]; K = (int) Math.ceil(Math.log(N) / Math.log(2)) + 1; lift = new int[N][K]; tin = new int[N]; tout = new int[N]; for (int i = 0; i < N; i++) { tree[i] = new DefaultTree.Node(); } } public static int[][] packU(int N, int[] from, int[] to) { //undirected int[][] adj = new int[N][]; int[] cntFrom = new int[N]; for (int i = 0; i < from.length; i++) { cntFrom[from[i]]++; cntFrom[to[i]]++; } for (int i = 0; i < N; i++) { adj[i] = new int[cntFrom[i]]; } for (int i = 0; i < from.length; i++) { adj[from[i]][--cntFrom[from[i]]] = to[i]; adj[to[i]][--cntFrom[to[i]]] = from[i]; } return adj; } static class Node { } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...