結果

問題 No.2277 Honest or Dishonest ?
ユーザー 👑 H20H20
提出日時 2023-04-21 22:50:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 472 ms / 2,000 ms
コード長 1,813 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 82,324 KB
実行使用メモリ 99,960 KB
最終ジャッジ日時 2024-04-25 19:04:26
合計ジャッジ時間 19,016 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
54,500 KB
testcase_01 AC 38 ms
56,372 KB
testcase_02 AC 39 ms
54,936 KB
testcase_03 AC 406 ms
96,288 KB
testcase_04 AC 435 ms
96,536 KB
testcase_05 AC 166 ms
89,784 KB
testcase_06 AC 356 ms
93,584 KB
testcase_07 AC 359 ms
90,484 KB
testcase_08 AC 141 ms
91,504 KB
testcase_09 AC 331 ms
90,780 KB
testcase_10 AC 302 ms
87,912 KB
testcase_11 AC 238 ms
82,188 KB
testcase_12 AC 186 ms
85,916 KB
testcase_13 AC 390 ms
95,164 KB
testcase_14 AC 167 ms
88,720 KB
testcase_15 AC 163 ms
87,348 KB
testcase_16 AC 358 ms
90,660 KB
testcase_17 AC 266 ms
83,928 KB
testcase_18 AC 427 ms
96,184 KB
testcase_19 AC 415 ms
95,624 KB
testcase_20 AC 397 ms
92,620 KB
testcase_21 AC 318 ms
91,720 KB
testcase_22 AC 383 ms
90,300 KB
testcase_23 AC 325 ms
95,872 KB
testcase_24 AC 359 ms
88,724 KB
testcase_25 AC 325 ms
93,016 KB
testcase_26 AC 113 ms
89,828 KB
testcase_27 AC 259 ms
87,212 KB
testcase_28 AC 171 ms
86,936 KB
testcase_29 AC 236 ms
89,884 KB
testcase_30 AC 308 ms
86,204 KB
testcase_31 AC 361 ms
91,372 KB
testcase_32 AC 224 ms
88,888 KB
testcase_33 AC 453 ms
95,824 KB
testcase_34 AC 404 ms
97,552 KB
testcase_35 AC 102 ms
84,888 KB
testcase_36 AC 344 ms
90,768 KB
testcase_37 AC 356 ms
96,860 KB
testcase_38 AC 163 ms
81,148 KB
testcase_39 AC 225 ms
83,788 KB
testcase_40 AC 106 ms
82,156 KB
testcase_41 AC 437 ms
98,500 KB
testcase_42 AC 361 ms
91,952 KB
testcase_43 AC 438 ms
98,856 KB
testcase_44 AC 472 ms
99,960 KB
testcase_45 AC 460 ms
99,428 KB
testcase_46 AC 457 ms
99,656 KB
testcase_47 AC 469 ms
99,692 KB
testcase_48 AC 471 ms
99,416 KB
testcase_49 AC 459 ms
99,172 KB
testcase_50 AC 449 ms
98,692 KB
testcase_51 AC 440 ms
98,520 KB
testcase_52 AC 442 ms
99,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, 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 = 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):
        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())
ABC = [list(map(int, input().split())) for _ in range(Q)]
uf = UnionFind(2*N)
edges=[]
for a,b,c in ABC:
    edges.append((a-1,b-1))
    if c==0:
        uf.union(a-1,b-1)
        uf.union(a-1+N,b-1+N)
    if c==1:
        uf.union(a-1,b-1+N)
        uf.union(a-1+N,b-1)
for i in range(N):
    if uf.same(i,i+N):
        print(0)
        exit()
ans=1
mod=998244353 
print(pow(2,uf.group_count()//2,mod))
0