結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー tentententen
提出日時 2023-07-26 09:52:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 770 ms / 2,000 ms
コード長 2,430 bytes
コンパイル時間 3,295 ms
コンパイル使用メモリ 92,776 KB
実行使用メモリ 125,596 KB
最終ジャッジ日時 2024-04-12 11:00:37
合計ジャッジ時間 25,668 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
50,452 KB
testcase_01 AC 56 ms
50,264 KB
testcase_02 AC 57 ms
50,164 KB
testcase_03 AC 58 ms
50,052 KB
testcase_04 AC 59 ms
50,528 KB
testcase_05 AC 57 ms
50,444 KB
testcase_06 AC 57 ms
50,372 KB
testcase_07 AC 57 ms
50,508 KB
testcase_08 AC 57 ms
50,280 KB
testcase_09 AC 57 ms
50,336 KB
testcase_10 AC 56 ms
50,348 KB
testcase_11 AC 745 ms
95,316 KB
testcase_12 AC 647 ms
94,044 KB
testcase_13 AC 722 ms
94,740 KB
testcase_14 AC 605 ms
90,576 KB
testcase_15 AC 665 ms
125,596 KB
testcase_16 AC 450 ms
76,208 KB
testcase_17 AC 457 ms
75,896 KB
testcase_18 AC 58 ms
50,412 KB
testcase_19 AC 637 ms
92,584 KB
testcase_20 AC 648 ms
89,936 KB
testcase_21 AC 642 ms
90,196 KB
testcase_22 AC 616 ms
92,660 KB
testcase_23 AC 639 ms
91,764 KB
testcase_24 AC 721 ms
94,280 KB
testcase_25 AC 638 ms
89,820 KB
testcase_26 AC 741 ms
92,000 KB
testcase_27 AC 704 ms
92,736 KB
testcase_28 AC 602 ms
92,252 KB
testcase_29 AC 694 ms
92,512 KB
testcase_30 AC 655 ms
91,548 KB
testcase_31 AC 631 ms
94,716 KB
testcase_32 AC 770 ms
94,672 KB
testcase_33 AC 661 ms
90,092 KB
testcase_34 AC 707 ms
92,152 KB
testcase_35 AC 646 ms
90,080 KB
testcase_36 AC 740 ms
94,312 KB
testcase_37 AC 709 ms
92,096 KB
testcase_38 AC 669 ms
91,540 KB
testcase_39 AC 634 ms
93,452 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;
    static long ans = 0;
    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);
        }
        check(0, 0);
        if (isBlack[0]) {
            System.out.println(ans);
        } else {
            System.out.println(-1);
        }
    }
    
    static void check(int idx, int p) {
        for (int x : graph.get(idx)) {
            if (p == x) {
                continue;
            }
            check(x, idx);
            if (!isBlack[x]) {
                isBlack[idx] ^= true;
                ans++;
            }
        }
    }
    
}
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