From add370870fce668e33a8d6ae5eb6d9a111c5f5ef Mon Sep 17 00:00:00 2001 From: oncsr Date: Fri, 14 Mar 2025 09:51:46 +0900 Subject: [PATCH] =?UTF-8?q?[20250314]=20BOJ=20/=20P5=20/=20=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=EC=99=80=20=EA=B2=BD=EB=A1=9C=20=EA=B0=9C=EC=88=98=20?= =?UTF-8?q?=EC=BF=BC=EB=A6=AC=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\210\230 \354\277\274\353\246\254.md" | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 "khj20006/202503/14 BOJ P5 \355\212\270\353\246\254\354\231\200 \352\262\275\353\241\234 \352\260\234\354\210\230 \354\277\274\353\246\254.md" diff --git "a/khj20006/202503/14 BOJ P5 \355\212\270\353\246\254\354\231\200 \352\262\275\353\241\234 \352\260\234\354\210\230 \354\277\274\353\246\254.md" "b/khj20006/202503/14 BOJ P5 \355\212\270\353\246\254\354\231\200 \352\262\275\353\241\234 \352\260\234\354\210\230 \354\277\274\353\246\254.md" new file mode 100644 index 00000000..74c0c1a6 --- /dev/null +++ "b/khj20006/202503/14 BOJ P5 \355\212\270\353\246\254\354\231\200 \352\262\275\353\241\234 \352\260\234\354\210\230 \354\277\274\353\246\254.md" @@ -0,0 +1,93 @@ +```java + +import java.util.*; +import java.io.*; + + +class Main { + + // IO field + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st = new StringTokenizer(""); + + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() throws Exception { + if(!st.hasMoreTokens()) nextLine(); + return Integer.parseInt(st.nextToken()); + } + static long nextLong() throws Exception { + if(!st.hasMoreTokens()) nextLine(); + return Long.parseLong(st.nextToken()); + } + static void bwEnd() throws Exception {bw.flush();bw.close();} + + // Additional field + + static int N; + static int[] A; + static long[] R, B, ans; + static List[] V; + static long blue = 0, red = 0; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + + } + + static void ready() throws Exception{ + + N = nextInt(); + A = new int[N+1]; + for(int i=1;i<=N;i++) { + A[i] = nextInt(); + red += A[i] == 1 ? 1 : 0; + blue += A[i] == 0 ? 1 : 0; + } + V = new List[N+1]; + for(int i=1;i<=N;i++) V[i] = new ArrayList<>(); + + for(int i=1;i0;) { + int x = nextInt(); + bw.write(ans[x] + "\n"); + } + + } + + static void dfs(int n, int p) { + + for(int i:V[n]) if(i != p) { + dfs(i, n); + R[n] += R[i] + (A[i] == 1 ? 1 : 0); + B[n] += B[i] + (A[i] == 0 ? 1 : 0); + } + ans[n] += R[n] * (blue - (B[n] + (A[n] == 0 ? 1 : 0))); + ans[n] += B[n] * (red - (R[n] + (A[n] == 1 ? 1 : 0))); + for(int i:V[n]) if(i != p) { + ans[n] += (R[i] + (A[i] == 1 ? 1 : 0)) * (B[n] - (B[i] + (A[i] == 0 ? 1 : 0))); + } + + } + +} + +```