結果

問題 No.2277 Honest or Dishonest ?
ユーザー tentententen
提出日時 2023-07-26 19:05:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 3,462 bytes
コンパイル時間 3,147 ms
コンパイル使用メモリ 91,820 KB
実行使用メモリ 59,968 KB
最終ジャッジ日時 2024-10-03 05:41:43
合計ジャッジ時間 16,550 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
50,380 KB
testcase_01 AC 47 ms
50,476 KB
testcase_02 AC 47 ms
50,280 KB
testcase_03 AC 270 ms
59,000 KB
testcase_04 AC 283 ms
58,896 KB
testcase_05 AC 195 ms
56,480 KB
testcase_06 AC 252 ms
58,776 KB
testcase_07 AC 227 ms
58,004 KB
testcase_08 AC 179 ms
57,772 KB
testcase_09 AC 221 ms
57,912 KB
testcase_10 AC 208 ms
56,092 KB
testcase_11 AC 163 ms
55,280 KB
testcase_12 AC 198 ms
55,988 KB
testcase_13 AC 265 ms
58,940 KB
testcase_14 AC 222 ms
56,476 KB
testcase_15 AC 180 ms
57,556 KB
testcase_16 AC 223 ms
57,836 KB
testcase_17 AC 177 ms
55,352 KB
testcase_18 AC 259 ms
58,728 KB
testcase_19 AC 267 ms
58,808 KB
testcase_20 AC 238 ms
58,656 KB
testcase_21 AC 221 ms
57,668 KB
testcase_22 AC 219 ms
57,540 KB
testcase_23 AC 248 ms
58,632 KB
testcase_24 AC 217 ms
57,628 KB
testcase_25 AC 232 ms
58,048 KB
testcase_26 AC 161 ms
59,968 KB
testcase_27 AC 191 ms
55,296 KB
testcase_28 AC 171 ms
55,748 KB
testcase_29 AC 214 ms
57,040 KB
testcase_30 AC 176 ms
55,488 KB
testcase_31 AC 214 ms
57,676 KB
testcase_32 AC 209 ms
58,596 KB
testcase_33 AC 258 ms
58,768 KB
testcase_34 AC 255 ms
58,708 KB
testcase_35 AC 145 ms
55,528 KB
testcase_36 AC 209 ms
56,744 KB
testcase_37 AC 260 ms
58,748 KB
testcase_38 AC 150 ms
55,000 KB
testcase_39 AC 162 ms
55,324 KB
testcase_40 AC 140 ms
55,444 KB
testcase_41 AC 276 ms
58,608 KB
testcase_42 AC 212 ms
57,740 KB
testcase_43 AC 275 ms
58,816 KB
testcase_44 AC 291 ms
58,856 KB
testcase_45 AC 299 ms
58,724 KB
testcase_46 AC 302 ms
58,732 KB
testcase_47 AC 288 ms
58,856 KB
testcase_48 AC 295 ms
58,888 KB
testcase_49 AC 266 ms
58,836 KB
testcase_50 AC 262 ms
58,992 KB
testcase_51 AC 264 ms
58,844 KB
testcase_52 AC 262 ms
58,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static final int MOD = 998244353;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int q = sc.nextInt();
        UnionFindTree uft = new UnionFindTree(n);
        while (q-- > 0) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            if (uft.same(a, b)) {
                if ((uft.getCount(a) + uft.getCount(b)) % 2 != c) {
                    System.out.println(0);
                    return;
                }
            } else {
                uft.unite(a, b, c);
            }
        }
        HashSet<Integer> counts = new HashSet<>();
        for (int i = 0; i < n; i++) {
            counts.add(uft.find(i));
        }
        System.out.println(pow(2, counts.size()));
    }
    
    static long pow(long x, int p) {
        if (p == 0) {
            return 1;
        } else if (p % 2 == 0) {
            return pow(x * x % MOD, p / 2);
        } else {
            return pow(x, p - 1) * x % MOD;
        }
    }
    
    static class UnionFindTree {
        int[] parents;
        int[] counts;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            counts = 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 p = find(parents[x]);
                counts[x] += counts[parents[x]];
                return parents[x] = p;
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public int getCount(int x) {
            find(x);
            return counts[x];
        }
        
        public void unite(int x, int y, int v) {
            int xx = find(x);
            int yy = find(y);
            if ((getCount(x) + getCount(y)) % 2 != v) {
                counts[xx] = 1;
            }
            parents[xx] = yy;
        }
    }
    
}
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