結果
問題 | No.2277 Honest or Dishonest ? |
ユーザー | shotoyoo |
提出日時 | 2023-04-21 21:48:53 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 361 ms / 2,000 ms |
コード長 | 2,571 bytes |
コンパイル時間 | 278 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 110,848 KB |
最終ジャッジ日時 | 2024-11-08 06:29:01 |
合計ジャッジ時間 | 14,721 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 48 ms
54,656 KB |
testcase_01 | AC | 47 ms
54,400 KB |
testcase_02 | AC | 47 ms
54,912 KB |
testcase_03 | AC | 285 ms
86,272 KB |
testcase_04 | AC | 341 ms
98,560 KB |
testcase_05 | AC | 229 ms
97,240 KB |
testcase_06 | AC | 232 ms
83,968 KB |
testcase_07 | AC | 245 ms
83,712 KB |
testcase_08 | AC | 216 ms
110,848 KB |
testcase_09 | AC | 289 ms
97,664 KB |
testcase_10 | AC | 201 ms
81,280 KB |
testcase_11 | AC | 175 ms
79,616 KB |
testcase_12 | AC | 163 ms
81,024 KB |
testcase_13 | AC | 255 ms
84,736 KB |
testcase_14 | AC | 168 ms
82,048 KB |
testcase_15 | AC | 204 ms
98,176 KB |
testcase_16 | AC | 231 ms
83,456 KB |
testcase_17 | AC | 178 ms
79,616 KB |
testcase_18 | AC | 350 ms
100,352 KB |
testcase_19 | AC | 307 ms
97,920 KB |
testcase_20 | AC | 267 ms
88,808 KB |
testcase_21 | AC | 210 ms
82,560 KB |
testcase_22 | AC | 245 ms
87,004 KB |
testcase_23 | AC | 182 ms
81,664 KB |
testcase_24 | AC | 248 ms
88,960 KB |
testcase_25 | AC | 290 ms
101,632 KB |
testcase_26 | AC | 170 ms
109,696 KB |
testcase_27 | AC | 178 ms
80,512 KB |
testcase_28 | AC | 213 ms
95,744 KB |
testcase_29 | AC | 243 ms
95,400 KB |
testcase_30 | AC | 195 ms
80,512 KB |
testcase_31 | AC | 237 ms
83,968 KB |
testcase_32 | AC | 247 ms
98,688 KB |
testcase_33 | AC | 299 ms
93,696 KB |
testcase_34 | AC | 275 ms
87,040 KB |
testcase_35 | AC | 141 ms
101,120 KB |
testcase_36 | AC | 223 ms
82,688 KB |
testcase_37 | AC | 244 ms
85,120 KB |
testcase_38 | AC | 129 ms
78,336 KB |
testcase_39 | AC | 162 ms
78,720 KB |
testcase_40 | AC | 141 ms
89,856 KB |
testcase_41 | AC | 276 ms
87,424 KB |
testcase_42 | AC | 247 ms
85,120 KB |
testcase_43 | AC | 322 ms
99,456 KB |
testcase_44 | AC | 361 ms
99,200 KB |
testcase_45 | AC | 357 ms
99,584 KB |
testcase_46 | AC | 347 ms
99,456 KB |
testcase_47 | AC | 350 ms
99,456 KB |
testcase_48 | AC | 355 ms
99,456 KB |
testcase_49 | AC | 318 ms
99,328 KB |
testcase_50 | AC | 329 ms
99,584 KB |
testcase_51 | AC | 310 ms
99,584 KB |
testcase_52 | AC | 316 ms
99,584 KB |
ソースコード
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)