結果

問題 No.2277 Honest or Dishonest ?
ユーザー lloyzlloyz
提出日時 2023-04-21 23:27:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 485 ms / 2,000 ms
コード長 2,363 bytes
コンパイル時間 429 ms
コンパイル使用メモリ 82,376 KB
実行使用メモリ 101,800 KB
最終ジャッジ日時 2024-04-25 19:10:04
合計ジャッジ時間 19,228 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,936 KB
testcase_01 AC 42 ms
54,476 KB
testcase_02 AC 42 ms
54,596 KB
testcase_03 AC 397 ms
80,372 KB
testcase_04 AC 441 ms
84,484 KB
testcase_05 AC 187 ms
94,776 KB
testcase_06 AC 358 ms
80,112 KB
testcase_07 AC 365 ms
80,348 KB
testcase_08 AC 167 ms
101,800 KB
testcase_09 AC 344 ms
86,972 KB
testcase_10 AC 314 ms
79,704 KB
testcase_11 AC 243 ms
79,444 KB
testcase_12 AC 182 ms
78,336 KB
testcase_13 AC 385 ms
80,564 KB
testcase_14 AC 152 ms
77,600 KB
testcase_15 AC 182 ms
91,116 KB
testcase_16 AC 345 ms
79,932 KB
testcase_17 AC 278 ms
79,484 KB
testcase_18 AC 439 ms
87,044 KB
testcase_19 AC 445 ms
84,892 KB
testcase_20 AC 419 ms
81,976 KB
testcase_21 AC 293 ms
79,488 KB
testcase_22 AC 381 ms
81,432 KB
testcase_23 AC 309 ms
79,552 KB
testcase_24 AC 393 ms
83,484 KB
testcase_25 AC 352 ms
89,104 KB
testcase_26 AC 137 ms
101,048 KB
testcase_27 AC 256 ms
78,596 KB
testcase_28 AC 191 ms
89,732 KB
testcase_29 AC 256 ms
91,032 KB
testcase_30 AC 299 ms
79,920 KB
testcase_31 AC 366 ms
80,640 KB
testcase_32 AC 247 ms
90,636 KB
testcase_33 AC 437 ms
83,940 KB
testcase_34 AC 413 ms
80,928 KB
testcase_35 AC 121 ms
91,984 KB
testcase_36 AC 358 ms
80,680 KB
testcase_37 AC 345 ms
79,548 KB
testcase_38 AC 151 ms
77,532 KB
testcase_39 AC 220 ms
78,624 KB
testcase_40 AC 126 ms
86,528 KB
testcase_41 AC 420 ms
80,260 KB
testcase_42 AC 361 ms
80,540 KB
testcase_43 AC 444 ms
82,256 KB
testcase_44 AC 485 ms
85,188 KB
testcase_45 AC 454 ms
85,124 KB
testcase_46 AC 483 ms
84,992 KB
testcase_47 AC 482 ms
85,648 KB
testcase_48 AC 462 ms
85,044 KB
testcase_49 AC 462 ms
82,260 KB
testcase_50 AC 440 ms
81,020 KB
testcase_51 AC 452 ms
81,688 KB
testcase_52 AC 449 ms
81,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素xを含む集合の親を見つける関数。
        '''
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        要素xを含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素yを含む集合に統合される。
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        '''
        要素xを含む集合の要素数を出す関数。
        '''
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

n, q = map(int, input().split())
mod = 998244353
UF = UnionFind(2 * n)
for _ in range(q):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if c == 0:
        UF.union(a, b)
        UF.union(a + n, b + n)
    else:
        UF.union(a, b + n)
        UF.union(a + n, b)

for i in range(n):
    if UF.same(i, i + n):
        print(0)
        exit()
ans = 1
seen = set()
for i in range(n):
    r0, r1 = UF.find(i), UF.find(i + n)
    if r0 in seen or r1 in seen:
        continue
    seen.add(r0)
    seen.add(r1)
    ans *= 2
    ans %= mod
print(ans)
0