結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー tentententen
提出日時 2023-03-30 11:05:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 825 ms / 2,000 ms
コード長 2,371 bytes
コンパイル時間 3,042 ms
コンパイル使用メモリ 91,912 KB
実行使用メモリ 120,752 KB
最終ジャッジ日時 2023-10-21 20:41:05
合計ジャッジ時間 26,087 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
36,676 KB
testcase_01 AC 46 ms
36,664 KB
testcase_02 AC 47 ms
36,764 KB
testcase_03 AC 48 ms
36,676 KB
testcase_04 AC 49 ms
36,708 KB
testcase_05 AC 49 ms
36,692 KB
testcase_06 AC 47 ms
36,704 KB
testcase_07 AC 46 ms
36,688 KB
testcase_08 AC 48 ms
36,700 KB
testcase_09 AC 48 ms
36,676 KB
testcase_10 AC 45 ms
36,756 KB
testcase_11 AC 645 ms
82,628 KB
testcase_12 AC 665 ms
84,160 KB
testcase_13 AC 585 ms
84,508 KB
testcase_14 AC 742 ms
87,856 KB
testcase_15 AC 825 ms
120,752 KB
testcase_16 AC 488 ms
65,596 KB
testcase_17 AC 407 ms
77,424 KB
testcase_18 AC 50 ms
53,468 KB
testcase_19 AC 622 ms
95,412 KB
testcase_20 AC 639 ms
94,344 KB
testcase_21 AC 583 ms
95,276 KB
testcase_22 AC 621 ms
94,864 KB
testcase_23 AC 632 ms
94,352 KB
testcase_24 AC 631 ms
94,400 KB
testcase_25 AC 569 ms
92,780 KB
testcase_26 AC 578 ms
92,536 KB
testcase_27 AC 568 ms
95,276 KB
testcase_28 AC 552 ms
94,428 KB
testcase_29 AC 602 ms
95,264 KB
testcase_30 AC 724 ms
95,296 KB
testcase_31 AC 561 ms
96,296 KB
testcase_32 AC 574 ms
93,992 KB
testcase_33 AC 670 ms
93,232 KB
testcase_34 AC 697 ms
94,480 KB
testcase_35 AC 728 ms
95,644 KB
testcase_36 AC 666 ms
93,056 KB
testcase_37 AC 623 ms
94,416 KB
testcase_38 AC 597 ms
93,856 KB
testcase_39 AC 721 ms
97,648 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;
    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 (getLight(0, 0)) {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }
    
    static boolean getLight(int idx, int p) {
        for (int x : graph.get(idx)) {
            if (x != p) {
                isOn[idx] ^= getLight(x, idx);
            }
        }
        if (!isOn[idx]) {
            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