Submission #1314529

#TimeUsernameProblemLanguageResultExecution timeMemory
1314529germanmanThe Xana coup (BOI21_xanadu)Java
Compilation error
0 ms0 KiB
import java.io.*;
import java.util.*;

public class Main {

	static int[] head;
	static int[] to;
	static int[] next;
	static int[] black;
	static int INF; 

	public static void main(String[] args) throws IOException {
		PrintWriter out = new PrintWriter(System.out);
		FastScanner fs = new FastScanner();
		int N = fs.nextInt();
		head = new int[N + 1];
		to = new int[N* 2];
		black = new int[N + 1];
		next = new int[N * 2];
		Arrays.fill(head, -1);
		INF = N + 1;
		for(int i = 0; i < N - 1; i++) {
			int u = fs.nextInt(), v = fs.nextInt();
			addEdge(i, u, v);
			addEdge(N + i, v, u);
		}
		for(int i = 1; i <= N; i++) {
			black[i] = fs.nextInt();
		}
		boolean[] visited = new boolean[N + 1];
		visited[1] = true;
		int[][][] dp = new int[N + 1][2][2];
		dp(1, visited, dp);
		int min = Math.min(dp[1][0][0], dp[1][0][1]);
		if(min >= INF) {
			out.print("impossible");
		}else {
			out.print(min);
		}
		out.flush();
	}

	

	private static void dp(int curr, boolean[] visited, int[][][] dp) {
		dp[curr][0][0] = black[curr] == 1 ? INF : 0;
		dp[curr][0][1] = black[curr] == 1 ? 1 : INF;
		dp[curr][1][0] = black[curr] == 1 ? 0 : INF;
		dp[curr][1][1] = black[curr] == 1 ? INF : 1;
		for(int e = head[curr]; e != -1; e = next[e]) {
			int node = to[e];
			if(!visited[node]) {
				visited[node] = true;
				dp(node, visited, dp);
				int[][] temp = new int[2][2];
				temp[0][0] = Math.min(dp[curr][0][0] + dp[node][0][0], dp[curr][1][0] + dp[node][0][1]);
				temp[0][1] = Math.min(dp[curr][0][1] + dp[node][1][0], dp[curr][1][1] + dp[node][1][1]);
				temp[1][0] = Math.min(dp[curr][1][0] + dp[node][0][0], dp[curr][0][0] + dp[node][0][1]);
				temp[1][1] = Math.min(dp[curr][1][1] + dp[node][1][0], dp[curr][0][1] + dp[node][1][1]);
				for(int i = 0; i < 2; i++) {
					for(int j = 0; j < 2; j++) {
						dp[curr][i][j] = temp[i][j];
					}
				}
			}
		}
	}



	private static void addEdge(int e, int u, int v) {
		next[e] = head[u];
		head[u] = e;
		to[e] = v;
	}

	static class FastScanner {
		InputStream is = System.in;
		byte[] buffer = new byte[1 << 17];
		int pointer = 0;
		int bufferLength = 0;

		private byte readByte() {
			if (pointer == bufferLength) {
				try {
					bufferLength = is.read(buffer);
					pointer = 0;
					if (bufferLength == -1)
						bufferLength = 0;
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return bufferLength == 0 ? -1 : buffer[pointer++];
		}

		private void skip() {
			int b;
			while ((b = readByte()) != -1 && b <= 32)
				;
			if (b != -1)
				pointer--;
		}

		public int nextInt() {
			int n = 0;
			skip();
			int b = readByte();
			while (b >= '0' && b <= '9') {
				n = n * 10 + (b - '0');
				b = readByte();
			}
			return n;
		}
	}
}

Compilation message (stderr)

xanadu.java:4: error: class Main is public, should be declared in a file named Main.java
public class Main {
       ^
1 error

=======