結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー tentententen
提出日時 2023-12-11 13:01:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 781 ms / 2,000 ms
コード長 2,390 bytes
コンパイル時間 2,790 ms
コンパイル使用メモリ 88,112 KB
実行使用メモリ 124,032 KB
最終ジャッジ日時 2023-12-11 13:01:35
合計ジャッジ時間 27,305 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
51,932 KB
testcase_01 AC 50 ms
53,972 KB
testcase_02 AC 50 ms
53,992 KB
testcase_03 AC 50 ms
53,984 KB
testcase_04 AC 50 ms
53,984 KB
testcase_05 AC 53 ms
53,968 KB
testcase_06 AC 48 ms
53,996 KB
testcase_07 AC 50 ms
53,984 KB
testcase_08 AC 49 ms
53,988 KB
testcase_09 AC 49 ms
53,980 KB
testcase_10 AC 49 ms
53,996 KB
testcase_11 AC 661 ms
96,524 KB
testcase_12 AC 639 ms
98,728 KB
testcase_13 AC 703 ms
98,564 KB
testcase_14 AC 525 ms
94,172 KB
testcase_15 AC 781 ms
124,032 KB
testcase_16 AC 478 ms
80,244 KB
testcase_17 AC 451 ms
80,124 KB
testcase_18 AC 52 ms
53,996 KB
testcase_19 AC 639 ms
96,564 KB
testcase_20 AC 670 ms
95,740 KB
testcase_21 AC 748 ms
98,200 KB
testcase_22 AC 639 ms
95,792 KB
testcase_23 AC 568 ms
95,772 KB
testcase_24 AC 600 ms
95,840 KB
testcase_25 AC 704 ms
95,768 KB
testcase_26 AC 660 ms
93,728 KB
testcase_27 AC 658 ms
95,940 KB
testcase_28 AC 634 ms
95,848 KB
testcase_29 AC 628 ms
96,036 KB
testcase_30 AC 643 ms
93,776 KB
testcase_31 AC 682 ms
98,136 KB
testcase_32 AC 614 ms
95,728 KB
testcase_33 AC 675 ms
95,896 KB
testcase_34 AC 551 ms
95,592 KB
testcase_35 AC 646 ms
95,756 KB
testcase_36 AC 640 ms
95,856 KB
testcase_37 AC 679 ms
95,908 KB
testcase_38 AC 738 ms
95,752 KB
testcase_39 AC 686 ms
95,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static boolean[] isOn;
    static int 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);
        }
        isOn = new boolean[n];
        for (int i = 0; i < n; i++) {
            isOn[i] = (sc.nextInt() == 1);
        }
        if (change(0, 0)) {
            System.out.println(ans);
        } else {
            System.out.println(-1);
        }
    }
    
    static boolean change(int idx, int p) {
        for (int x : graph.get(idx)) {
            if (p != x) {
                if (!change(x, idx)) {
                    isOn[idx] ^= true;
                    ans++;
                }
            }
        }
        return isOn[idx];
    }
}
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