結果
問題 | No.2277 Honest or Dishonest ? |
ユーザー | tenten |
提出日時 | 2023-04-28 08:58:15 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 396 ms / 2,000 ms |
コード長 | 3,229 bytes |
コンパイル時間 | 2,660 ms |
コンパイル使用メモリ | 95,496 KB |
実行使用メモリ | 55,140 KB |
最終ジャッジ日時 | 2024-04-28 16:49:33 |
合計ジャッジ時間 | 20,100 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
36,780 KB |
testcase_01 | AC | 52 ms
37,200 KB |
testcase_02 | AC | 51 ms
37,172 KB |
testcase_03 | AC | 318 ms
47,432 KB |
testcase_04 | AC | 333 ms
47,764 KB |
testcase_05 | AC | 261 ms
50,832 KB |
testcase_06 | AC | 283 ms
47,644 KB |
testcase_07 | AC | 267 ms
47,384 KB |
testcase_08 | AC | 246 ms
51,576 KB |
testcase_09 | AC | 281 ms
48,872 KB |
testcase_10 | AC | 243 ms
46,740 KB |
testcase_11 | AC | 184 ms
43,708 KB |
testcase_12 | AC | 218 ms
43,528 KB |
testcase_13 | AC | 317 ms
47,700 KB |
testcase_14 | AC | 245 ms
45,468 KB |
testcase_15 | AC | 242 ms
51,784 KB |
testcase_16 | AC | 273 ms
46,924 KB |
testcase_17 | AC | 208 ms
45,856 KB |
testcase_18 | AC | 353 ms
48,700 KB |
testcase_19 | AC | 353 ms
48,144 KB |
testcase_20 | AC | 293 ms
48,244 KB |
testcase_21 | AC | 265 ms
46,208 KB |
testcase_22 | AC | 277 ms
48,224 KB |
testcase_23 | AC | 294 ms
47,236 KB |
testcase_24 | AC | 274 ms
47,108 KB |
testcase_25 | AC | 307 ms
51,260 KB |
testcase_26 | AC | 224 ms
55,140 KB |
testcase_27 | AC | 214 ms
43,620 KB |
testcase_28 | AC | 240 ms
51,032 KB |
testcase_29 | AC | 292 ms
51,984 KB |
testcase_30 | AC | 195 ms
43,796 KB |
testcase_31 | AC | 263 ms
47,328 KB |
testcase_32 | AC | 265 ms
48,536 KB |
testcase_33 | AC | 331 ms
48,076 KB |
testcase_34 | AC | 310 ms
47,376 KB |
testcase_35 | AC | 182 ms
46,020 KB |
testcase_36 | AC | 245 ms
45,840 KB |
testcase_37 | AC | 295 ms
47,604 KB |
testcase_38 | AC | 165 ms
43,480 KB |
testcase_39 | AC | 185 ms
43,724 KB |
testcase_40 | AC | 175 ms
45,716 KB |
testcase_41 | AC | 302 ms
47,140 KB |
testcase_42 | AC | 271 ms
47,468 KB |
testcase_43 | AC | 310 ms
47,352 KB |
testcase_44 | AC | 396 ms
47,960 KB |
testcase_45 | AC | 379 ms
47,940 KB |
testcase_46 | AC | 392 ms
47,592 KB |
testcase_47 | AC | 394 ms
47,780 KB |
testcase_48 | AC | 388 ms
47,796 KB |
testcase_49 | AC | 335 ms
47,412 KB |
testcase_50 | AC | 318 ms
47,752 KB |
testcase_51 | AC | 330 ms
47,300 KB |
testcase_52 | AC | 335 ms
47,348 KB |
ソースコード
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 * 2); for (int i = 0; i < q; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; int c = sc.nextInt(); if (c == 0) { uft.unite(a, b); uft.unite(a + n, b + n); } else { uft.unite(a, b + n); uft.unite(a + n, b); } } HashSet<Integer> leader = new HashSet<>(); for (int i = 0; i < n; i++) { if (uft.same(i, i + n)) { System.out.println(0); return; } leader.add(uft.find(i)); leader.add(uft.find(i + n)); } System.out.println(pow(2, leader.size() / 2)); } 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; public UnionFindTree(int size) { parents = 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 { return parents[x] = find(parents[x]); } } public boolean same(int x, int y) { return find(x) == find(y); } public void unite(int x, int y) { if (!same(x, y)) { parents[find(x)] = find(y); } } } } 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(); } }