結果

問題 No.1420 国勢調査 (Easy)
ユーザー tentententen
提出日時 2021-03-06 15:11:11
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,955 bytes
コンパイル時間 2,683 ms
コンパイル使用メモリ 78,248 KB
実行使用メモリ 52,228 KB
最終ジャッジ日時 2024-04-17 04:01:19
合計ジャッジ時間 23,047 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
41,032 KB
testcase_01 AC 128 ms
41,304 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 297 ms
47,536 KB
testcase_08 AC 285 ms
47,388 KB
testcase_09 AC 284 ms
46,216 KB
testcase_10 AC 298 ms
47,588 KB
testcase_11 AC 302 ms
47,644 KB
testcase_12 AC 320 ms
48,120 KB
testcase_13 WA -
testcase_14 AC 344 ms
47,360 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 735 ms
50,848 KB
testcase_28 AC 690 ms
50,988 KB
testcase_29 AC 611 ms
48,656 KB
testcase_30 AC 847 ms
52,228 KB
testcase_31 AC 706 ms
49,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        UnionFindTree uft = new UnionFindTree(n);
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            if (uft.same(a, b)) {
                if (uft.get(a, b) != c) {
                    System.out.println(-1);
                    return;
                }
            } else {
                uft.unite(a, b, c);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(uft.getCost(i)).append("\n");
        }
        System.out.print(sb);
    }
    
    static class UnionFindTree {
        int[] parents;
        int[] costs;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            costs = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                int tmp = find(parents[x]);
                costs[x] ^= costs[parents[x]];
                return parents[x] = tmp;
            }
        }
        
        public int get(int x, int y) {
            find(x);
            find(y);
            return costs[x] ^ costs[y];
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public int getCost(int x) {
            find(x);
            return costs[x];
        }
        
        public void unite(int x, int y, int c) {
            int xx = find(x);
            int yy = find(y);
            parents[xx] = yy;
            costs[xx] = costs[x] ^ c;
        }
    }
}
0