結果
問題 | No.2277 Honest or Dishonest ? |
ユーザー | tenten |
提出日時 | 2023-04-28 08:58:15 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 422 ms / 2,000 ms |
コード長 | 3,229 bytes |
コンパイル時間 | 2,966 ms |
コンパイル使用メモリ | 92,468 KB |
実行使用メモリ | 55,004 KB |
最終ジャッジ日時 | 2024-11-17 10:32:20 |
合計ジャッジ時間 | 21,181 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 55 ms
37,124 KB |
testcase_01 | AC | 55 ms
36,736 KB |
testcase_02 | AC | 54 ms
37,296 KB |
testcase_03 | AC | 354 ms
47,192 KB |
testcase_04 | AC | 397 ms
47,672 KB |
testcase_05 | AC | 297 ms
51,960 KB |
testcase_06 | AC | 316 ms
47,444 KB |
testcase_07 | AC | 290 ms
47,160 KB |
testcase_08 | AC | 272 ms
51,936 KB |
testcase_09 | AC | 308 ms
48,536 KB |
testcase_10 | AC | 251 ms
45,684 KB |
testcase_11 | AC | 184 ms
43,512 KB |
testcase_12 | AC | 234 ms
43,900 KB |
testcase_13 | AC | 337 ms
47,536 KB |
testcase_14 | AC | 252 ms
44,964 KB |
testcase_15 | AC | 257 ms
51,468 KB |
testcase_16 | AC | 290 ms
47,580 KB |
testcase_17 | AC | 210 ms
43,840 KB |
testcase_18 | AC | 358 ms
48,592 KB |
testcase_19 | AC | 375 ms
48,224 KB |
testcase_20 | AC | 323 ms
48,136 KB |
testcase_21 | AC | 276 ms
46,184 KB |
testcase_22 | AC | 292 ms
48,096 KB |
testcase_23 | AC | 304 ms
46,836 KB |
testcase_24 | AC | 281 ms
47,464 KB |
testcase_25 | AC | 355 ms
51,052 KB |
testcase_26 | AC | 245 ms
55,004 KB |
testcase_27 | AC | 222 ms
44,576 KB |
testcase_28 | AC | 268 ms
51,084 KB |
testcase_29 | AC | 319 ms
51,704 KB |
testcase_30 | AC | 207 ms
43,868 KB |
testcase_31 | AC | 284 ms
46,828 KB |
testcase_32 | AC | 290 ms
49,448 KB |
testcase_33 | AC | 379 ms
48,072 KB |
testcase_34 | AC | 328 ms
47,316 KB |
testcase_35 | AC | 193 ms
45,784 KB |
testcase_36 | AC | 244 ms
45,792 KB |
testcase_37 | AC | 313 ms
47,196 KB |
testcase_38 | AC | 176 ms
43,360 KB |
testcase_39 | AC | 200 ms
43,720 KB |
testcase_40 | AC | 189 ms
45,156 KB |
testcase_41 | AC | 336 ms
47,272 KB |
testcase_42 | AC | 286 ms
46,688 KB |
testcase_43 | AC | 340 ms
47,340 KB |
testcase_44 | AC | 412 ms
47,636 KB |
testcase_45 | AC | 410 ms
47,892 KB |
testcase_46 | AC | 408 ms
47,680 KB |
testcase_47 | AC | 408 ms
47,760 KB |
testcase_48 | AC | 422 ms
48,588 KB |
testcase_49 | AC | 345 ms
47,440 KB |
testcase_50 | AC | 347 ms
47,228 KB |
testcase_51 | AC | 355 ms
47,484 KB |
testcase_52 | AC | 349 ms
47,604 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(); } }