結果

問題 No.2277 Honest or Dishonest ?
ユーザー shotoyooshotoyoo
提出日時 2023-04-21 21:46:10
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,482 bytes
コンパイル時間 395 ms
コンパイル使用メモリ 82,484 KB
実行使用メモリ 97,320 KB
最終ジャッジ日時 2024-04-25 18:53:41
合計ジャッジ時間 13,917 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
56,272 KB
testcase_01 AC 42 ms
54,940 KB
testcase_02 AC 43 ms
55,132 KB
testcase_03 AC 257 ms
86,536 KB
testcase_04 AC 295 ms
95,176 KB
testcase_05 AC 205 ms
93,308 KB
testcase_06 AC 220 ms
84,108 KB
testcase_07 AC 223 ms
83,548 KB
testcase_08 AC 178 ms
96,872 KB
testcase_09 AC 258 ms
92,740 KB
testcase_10 AC 182 ms
81,372 KB
testcase_11 AC 163 ms
79,172 KB
testcase_12 AC 149 ms
81,192 KB
testcase_13 AC 239 ms
84,684 KB
testcase_14 AC 156 ms
82,588 KB
testcase_15 AC 187 ms
91,060 KB
testcase_16 AC 219 ms
83,472 KB
testcase_17 AC 165 ms
79,840 KB
testcase_18 AC 297 ms
96,380 KB
testcase_19 AC 287 ms
94,444 KB
testcase_20 AC 253 ms
88,468 KB
testcase_21 AC 189 ms
82,788 KB
testcase_22 AC 238 ms
87,384 KB
testcase_23 WA -
testcase_24 AC 237 ms
87,620 KB
testcase_25 AC 263 ms
95,916 KB
testcase_26 AC 141 ms
95,368 KB
testcase_27 WA -
testcase_28 AC 194 ms
90,148 KB
testcase_29 AC 237 ms
90,904 KB
testcase_30 AC 177 ms
80,676 KB
testcase_31 WA -
testcase_32 AC 229 ms
92,632 KB
testcase_33 AC 279 ms
92,920 KB
testcase_34 AC 254 ms
87,084 KB
testcase_35 AC 116 ms
93,948 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 120 ms
85,628 KB
testcase_41 WA -
testcase_42 AC 227 ms
84,412 KB
testcase_43 AC 291 ms
91,244 KB
testcase_44 AC 331 ms
97,320 KB
testcase_45 AC 329 ms
95,556 KB
testcase_46 AC 321 ms
94,744 KB
testcase_47 AC 322 ms
95,304 KB
testcase_48 AC 326 ms
94,960 KB
testcase_49 AC 294 ms
90,936 KB
testcase_50 AC 296 ms
91,248 KB
testcase_51 AC 282 ms
91,324 KB
testcase_52 AC 291 ms
91,152 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 = {}
M = 998244353
for a,b in ab:
    ra = uf.root(a)
    rb = uf.root(b)
    if ra==rb:
        ans = 0
        break
    else:
        ns.setdefault(ra, []).append(rb)
        ns.setdefault(rb, []).append(ra)
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.get(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