結果

問題 No.2277 Honest or Dishonest ?
ユーザー shotoyooshotoyoo
提出日時 2023-04-21 21:48:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 289 ms / 2,000 ms
コード長 2,571 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 82,448 KB
実行使用メモリ 110,748 KB
最終ジャッジ日時 2024-04-25 18:54:33
合計ジャッジ時間 12,332 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,524 KB
testcase_01 AC 42 ms
55,624 KB
testcase_02 AC 38 ms
55,768 KB
testcase_03 AC 223 ms
86,436 KB
testcase_04 AC 270 ms
99,248 KB
testcase_05 AC 191 ms
97,496 KB
testcase_06 AC 199 ms
84,236 KB
testcase_07 AC 194 ms
84,224 KB
testcase_08 AC 176 ms
110,748 KB
testcase_09 AC 222 ms
98,192 KB
testcase_10 AC 156 ms
81,288 KB
testcase_11 AC 146 ms
79,408 KB
testcase_12 AC 131 ms
80,952 KB
testcase_13 AC 203 ms
84,888 KB
testcase_14 AC 134 ms
82,292 KB
testcase_15 AC 163 ms
98,248 KB
testcase_16 AC 189 ms
83,564 KB
testcase_17 AC 151 ms
79,888 KB
testcase_18 AC 266 ms
100,808 KB
testcase_19 AC 252 ms
97,896 KB
testcase_20 AC 235 ms
89,444 KB
testcase_21 AC 176 ms
82,664 KB
testcase_22 AC 211 ms
87,452 KB
testcase_23 AC 143 ms
81,900 KB
testcase_24 AC 195 ms
88,856 KB
testcase_25 AC 236 ms
102,084 KB
testcase_26 AC 152 ms
109,980 KB
testcase_27 AC 151 ms
80,648 KB
testcase_28 AC 171 ms
96,048 KB
testcase_29 AC 204 ms
95,392 KB
testcase_30 AC 157 ms
80,672 KB
testcase_31 AC 196 ms
84,264 KB
testcase_32 AC 207 ms
98,632 KB
testcase_33 AC 240 ms
93,780 KB
testcase_34 AC 232 ms
87,180 KB
testcase_35 AC 118 ms
100,900 KB
testcase_36 AC 179 ms
82,924 KB
testcase_37 AC 185 ms
85,328 KB
testcase_38 AC 99 ms
78,484 KB
testcase_39 AC 125 ms
78,792 KB
testcase_40 AC 111 ms
90,008 KB
testcase_41 AC 214 ms
87,952 KB
testcase_42 AC 206 ms
85,188 KB
testcase_43 AC 262 ms
99,920 KB
testcase_44 AC 283 ms
99,440 KB
testcase_45 AC 288 ms
99,692 KB
testcase_46 AC 289 ms
99,668 KB
testcase_47 AC 277 ms
99,484 KB
testcase_48 AC 285 ms
99,600 KB
testcase_49 AC 266 ms
99,376 KB
testcase_50 AC 267 ms
99,624 KB
testcase_51 AC 258 ms
99,664 KB
testcase_52 AC 260 ms
99,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, random
input = lambda : sys.stdin.readline().rstrip()


write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x))
debug = lambda x: sys.stderr.write(x+"\n")
YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO); INF=10**18
LI = lambda : list(map(int, input().split())); II=lambda : int(input()); SI=lambda : [ord(c)-ord("a") for c in input()]
def debug(_l_):
    for s in _l_.split():
        print(f"{s}={eval(s)}", end=" ")
    print()
def dlist(*l, fill=0):
    if len(l)==1:
        return [fill]*l[0]
    ll = l[1:]
    return [dlist(*ll, fill=fill) for _ in range(l[0])]


class UF:
    # unionfind
    def __init__(self, n):
        self.n = n
        self.parent = list(range(n))
        self.size = [1] * n
#         self.es = [0] * n
    def check(self):
        return [self.root(i) for i in range(self.n)]
    def root(self, i):
        inter = set()
        while self.parent[i]!=i:
            inter.add(i)
            i = self.parent[i]
        r = i
        for i in inter:
            self.parent[i] = r
        return r
    def merge(self, i, j):
        # 繋いだかどうかを返す
        ri = self.root(i)
        rj = self.root(j)
        if ri==rj:
#             self.es[ri] += 1
            return False
        if self.size[ri]>self.size[rj]:
            ri,rj = rj,ri
        self.parent[ri] = rj
        self.size[rj] += self.size[ri]
#         self.es[rj] += self.es[ri] + 1
        return True
    def rs(self):
        return sorted(set([self.root(i) for i in range(self.n)]))
    
n,q = list(map(int, input().split()))
uf = UF(n)
ab = []
for i in range(q):
    a,b,c = LI()
    a -= 1
    b -= 1
    if c==0:
        uf.merge(a,b)
    else:
        ab.append((a,b))
rs = uf.rs()
ns = {r: [] for r in rs}
M = 998244353
for a,b in ab:
    ra = uf.root(a)
    rb = uf.root(b)
    if ra==rb:
        ans = 0
        break
    else:
        ns[ra].append(rb)
        ns[rb].append(ra)
else:
    num = 0
    done = {}
    for r in rs:
        if r in done:
            continue
        q = [r]
        done[r] = 0
        num += 1
        ng = 0
        while q:
            u = q.pop()
            for v in ns[u]:
                if v not in done:
                    done[v] = done[u]^1
                    q.append(v)
                else:
                    if done[v]==done[u]:
                        ng = 1
                        break
            if ng:
                break
        if ng:
            ans = 0
            break
    else:
        ans = pow(2, num, M)
print(ans%M)
0