結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー tentententen
提出日時 2023-02-13 18:29:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 725 ms / 2,000 ms
コード長 2,479 bytes
コンパイル時間 4,508 ms
コンパイル使用メモリ 85,004 KB
実行使用メモリ 126,536 KB
最終ジャッジ日時 2023-09-23 12:14:14
合計ジャッジ時間 25,772 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
49,356 KB
testcase_01 AC 41 ms
49,532 KB
testcase_02 AC 44 ms
49,364 KB
testcase_03 AC 40 ms
49,300 KB
testcase_04 AC 41 ms
49,332 KB
testcase_05 AC 42 ms
49,416 KB
testcase_06 AC 41 ms
49,488 KB
testcase_07 AC 42 ms
49,276 KB
testcase_08 AC 41 ms
49,316 KB
testcase_09 AC 41 ms
50,012 KB
testcase_10 AC 42 ms
49,344 KB
testcase_11 AC 548 ms
95,284 KB
testcase_12 AC 599 ms
95,124 KB
testcase_13 AC 571 ms
95,476 KB
testcase_14 AC 589 ms
92,032 KB
testcase_15 AC 680 ms
126,536 KB
testcase_16 AC 397 ms
77,492 KB
testcase_17 AC 413 ms
76,276 KB
testcase_18 AC 42 ms
49,436 KB
testcase_19 AC 543 ms
92,520 KB
testcase_20 AC 552 ms
92,948 KB
testcase_21 AC 666 ms
95,112 KB
testcase_22 AC 673 ms
94,912 KB
testcase_23 AC 620 ms
92,936 KB
testcase_24 AC 640 ms
95,012 KB
testcase_25 AC 554 ms
91,296 KB
testcase_26 AC 631 ms
90,248 KB
testcase_27 AC 628 ms
92,300 KB
testcase_28 AC 506 ms
92,544 KB
testcase_29 AC 507 ms
92,416 KB
testcase_30 AC 546 ms
90,268 KB
testcase_31 AC 630 ms
98,736 KB
testcase_32 AC 551 ms
92,128 KB
testcase_33 AC 560 ms
91,972 KB
testcase_34 AC 625 ms
94,896 KB
testcase_35 AC 706 ms
94,588 KB
testcase_36 AC 678 ms
90,256 KB
testcase_37 AC 543 ms
92,532 KB
testcase_38 AC 725 ms
94,936 KB
testcase_39 AC 537 ms
92,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static boolean[] isBlack;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        isBlack = new boolean[n];
        for (int i = 0; i < n; i++) {
            isBlack[i] = (sc.nextInt() == 1);
        }
        int ans = getCount(0, 0);
        if (isBlack[0]) {
            System.out.println(ans);
        } else {
            System.out.println(-1);
        }
    }
    
    
    static int getCount(int idx, int p) {
        int count = 0;
        for (int x : graph.get(idx)) {
            if (x == p) {
                continue;
            }
            count += getCount(x, idx);
            if (!isBlack[x]) {
                count++;
                isBlack[idx] ^= true;
            }
        }
        return count;
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0