結果

問題 No.1641 Tree Xor Query
ユーザー tentententen
提出日時 2021-08-10 09:15:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 600 ms / 5,000 ms
コード長 2,189 bytes
コンパイル時間 2,829 ms
コンパイル使用メモリ 78,680 KB
実行使用メモリ 97,124 KB
最終ジャッジ日時 2023-10-23 16:09:12
合計ジャッジ時間 7,772 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
52,420 KB
testcase_01 AC 54 ms
52,448 KB
testcase_02 AC 53 ms
52,432 KB
testcase_03 AC 56 ms
53,536 KB
testcase_04 AC 57 ms
53,540 KB
testcase_05 AC 58 ms
52,792 KB
testcase_06 AC 61 ms
53,536 KB
testcase_07 AC 58 ms
53,532 KB
testcase_08 AC 54 ms
52,428 KB
testcase_09 AC 56 ms
53,532 KB
testcase_10 AC 61 ms
52,460 KB
testcase_11 AC 56 ms
53,520 KB
testcase_12 AC 56 ms
52,424 KB
testcase_13 AC 600 ms
96,712 KB
testcase_14 AC 576 ms
97,124 KB
testcase_15 AC 132 ms
55,872 KB
testcase_16 AC 183 ms
58,504 KB
testcase_17 AC 159 ms
57,996 KB
testcase_18 AC 163 ms
55,924 KB
testcase_19 AC 139 ms
55,944 KB
testcase_20 AC 541 ms
77,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static int[] costs;
    static int[] parents;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int q = sc.nextInt();
        costs = new int[n];
        parents = new int[n];
        for (int i = 0; i < n; i++) {
            costs[i] = sc.nextInt();
            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);
        }
        getCost(0, 0);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            int type = sc.nextInt();
            int x = sc.nextInt() - 1;
            int y = sc.nextInt();
            if (type == 1) {
                setCost(x, y);
            } else {
                sb.append(costs[x]).append("\n");
            }
        }
        System.out.print(sb);
    }
    
    static int getCost(int idx, int p) {
        parents[idx] = p;
        for (int x : graph.get(idx)) {
            if (p == x) {
                continue;
            }
            costs[idx] ^= getCost(x, idx);
        }
        return costs[idx];
    }
    
    static void setCost(int idx, int value) {
        while (true) {
            costs[idx] ^= value;
            if (idx == 0) {
                break;
            }
            idx = parents[idx];
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0