結果

問題 No.2277 Honest or Dishonest ?
ユーザー tassei903tassei903
提出日時 2023-04-21 21:58:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 2,055 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 82,468 KB
実行使用メモリ 101,752 KB
最終ジャッジ日時 2024-04-25 18:47:50
合計ジャッジ時間 16,622 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
55,268 KB
testcase_01 AC 39 ms
54,816 KB
testcase_02 AC 37 ms
55,000 KB
testcase_03 AC 333 ms
85,760 KB
testcase_04 AC 383 ms
92,112 KB
testcase_05 AC 163 ms
94,996 KB
testcase_06 AC 290 ms
82,500 KB
testcase_07 AC 316 ms
83,528 KB
testcase_08 AC 166 ms
101,752 KB
testcase_09 AC 327 ms
91,372 KB
testcase_10 AC 259 ms
79,820 KB
testcase_11 AC 218 ms
78,796 KB
testcase_12 AC 144 ms
77,416 KB
testcase_13 AC 323 ms
83,976 KB
testcase_14 AC 121 ms
77,200 KB
testcase_15 AC 166 ms
91,212 KB
testcase_16 AC 314 ms
84,580 KB
testcase_17 AC 241 ms
79,748 KB
testcase_18 AC 359 ms
93,168 KB
testcase_19 AC 358 ms
91,916 KB
testcase_20 AC 367 ms
88,552 KB
testcase_21 AC 242 ms
79,472 KB
testcase_22 AC 329 ms
85,084 KB
testcase_23 AC 246 ms
80,264 KB
testcase_24 AC 319 ms
86,092 KB
testcase_25 AC 312 ms
97,716 KB
testcase_26 AC 134 ms
100,748 KB
testcase_27 AC 231 ms
79,436 KB
testcase_28 AC 173 ms
89,852 KB
testcase_29 AC 214 ms
91,212 KB
testcase_30 AC 251 ms
81,188 KB
testcase_31 AC 317 ms
85,380 KB
testcase_32 AC 210 ms
91,328 KB
testcase_33 AC 378 ms
90,928 KB
testcase_34 AC 344 ms
88,340 KB
testcase_35 AC 108 ms
92,192 KB
testcase_36 AC 321 ms
84,492 KB
testcase_37 AC 297 ms
82,608 KB
testcase_38 AC 131 ms
77,740 KB
testcase_39 AC 203 ms
78,852 KB
testcase_40 AC 116 ms
93,116 KB
testcase_41 AC 391 ms
88,856 KB
testcase_42 AC 357 ms
86,336 KB
testcase_43 AC 417 ms
96,600 KB
testcase_44 AC 401 ms
97,852 KB
testcase_45 AC 391 ms
95,656 KB
testcase_46 AC 405 ms
95,016 KB
testcase_47 AC 415 ms
94,016 KB
testcase_48 AC 427 ms
94,888 KB
testcase_49 AC 418 ms
95,872 KB
testcase_50 AC 411 ms
96,172 KB
testcase_51 AC 417 ms
95,628 KB
testcase_52 AC 413 ms
95,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES")
no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO")
#######################################################################

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())
mod = 998244353
n, q = na()
uf = UnionFind(n*2)

for _ in range(q):
    a,b,c = na()
    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)
d = uf.all_group_members()
cnt = 0
for i in d:
    i %= n
    if uf.same(i, i+n):
        print(0)
        exit()
    else:
        cnt += 1

print(pow(2, cnt//2, mod))
0