結果
問題 | No.2277 Honest or Dishonest ? |
ユーザー | lloyz |
提出日時 | 2023-04-21 23:20:38 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,741 bytes |
コンパイル時間 | 1,066 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 117,912 KB |
最終ジャッジ日時 | 2024-11-08 06:45:51 |
合計ジャッジ時間 | 23,202 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
54,528 KB |
testcase_01 | AC | 50 ms
54,784 KB |
testcase_02 | AC | 51 ms
55,168 KB |
testcase_03 | AC | 567 ms
114,248 KB |
testcase_04 | AC | 509 ms
107,744 KB |
testcase_05 | AC | 269 ms
98,064 KB |
testcase_06 | AC | 471 ms
103,108 KB |
testcase_07 | AC | 463 ms
99,624 KB |
testcase_08 | AC | 222 ms
91,360 KB |
testcase_09 | AC | 381 ms
93,664 KB |
testcase_10 | AC | 391 ms
92,800 KB |
testcase_11 | AC | 324 ms
84,464 KB |
testcase_12 | AC | 254 ms
88,736 KB |
testcase_13 | AC | 538 ms
107,764 KB |
testcase_14 | AC | 235 ms
91,564 KB |
testcase_15 | AC | 247 ms
91,088 KB |
testcase_16 | AC | 478 ms
98,552 KB |
testcase_17 | AC | 345 ms
87,456 KB |
testcase_18 | AC | 467 ms
101,636 KB |
testcase_19 | AC | 507 ms
101,804 KB |
testcase_20 | AC | 458 ms
99,260 KB |
testcase_21 | AC | 404 ms
94,796 KB |
testcase_22 | WA | - |
testcase_23 | AC | 452 ms
102,712 KB |
testcase_24 | AC | 397 ms
92,004 KB |
testcase_25 | AC | 390 ms
95,856 KB |
testcase_26 | AC | 204 ms
93,400 KB |
testcase_27 | AC | 347 ms
89,472 KB |
testcase_28 | AC | 255 ms
94,060 KB |
testcase_29 | AC | 290 ms
92,028 KB |
testcase_30 | WA | - |
testcase_31 | AC | 506 ms
104,348 KB |
testcase_32 | AC | 295 ms
96,860 KB |
testcase_33 | AC | 487 ms
103,764 KB |
testcase_34 | AC | 572 ms
115,536 KB |
testcase_35 | AC | 165 ms
85,904 KB |
testcase_36 | WA | - |
testcase_37 | AC | 526 ms
106,348 KB |
testcase_38 | AC | 221 ms
83,200 KB |
testcase_39 | AC | 304 ms
84,480 KB |
testcase_40 | AC | 181 ms
86,692 KB |
testcase_41 | AC | 600 ms
115,228 KB |
testcase_42 | AC | 485 ms
104,200 KB |
testcase_43 | WA | - |
testcase_44 | AC | 567 ms
116,140 KB |
testcase_45 | AC | 566 ms
116,916 KB |
testcase_46 | AC | 565 ms
116,540 KB |
testcase_47 | AC | 574 ms
116,808 KB |
testcase_48 | AC | 560 ms
114,568 KB |
testcase_49 | AC | 564 ms
114,344 KB |
testcase_50 | WA | - |
testcase_51 | AC | 589 ms
117,912 KB |
testcase_52 | WA | - |
ソースコード
def scc(N,edges): M=len(edges) start=[0]*(N+1) elist=[0]*M for e in edges: start[e[0]+1]+=1 for i in range(1,N+1): start[i]+=start[i-1] counter=start[:] for e in edges: elist[counter[e[0]]]=e[1] counter[e[0]]+=1 visited=[] low=[0]*N Ord=[-1]*N ids=[0]*N NG=[0,0] def dfs(v): stack=[(v,-1,0),(v,-1,1)] while stack: v,bef,t=stack.pop() if t: if bef!=-1 and Ord[v]!=-1: low[bef]=min(low[bef],Ord[v]) stack.pop() continue low[v]=NG[0] Ord[v]=NG[0] NG[0]+=1 visited.append(v) for i in range(start[v],start[v+1]): to=elist[i] if Ord[to]==-1: stack.append((to,v,0)) stack.append((to,v,1)) else: low[v]=min(low[v],Ord[to]) else: if low[v]==Ord[v]: while(True): u=visited.pop() Ord[u]=N ids[u]=NG[1] if u==v: break NG[1]+=1 low[bef]=min(low[bef],low[v]) for i in range(N): if Ord[i]==-1: dfs(i) for i in range(N): ids[i]=NG[1]-1-ids[i] group_num=NG[1] counts=[0]*group_num for x in ids: counts[x]+=1 groups=[[] for i in range(group_num)] for i in range(N): groups[ids[i]].append(i) return groups from collections import defaultdict class UnionFind(): def __init__(self, n): ''' UnionFindクラス。nは要素数を表す。 ''' self.n = n self.parents = [-1] * n def find(self, x): ''' 要素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を含む集合と要素yを含む集合を合体する関数。 基本的には、要素数が多い集合に統合される。 要素数が同じときは要素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): ''' 要素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()) from collections import deque, defaultdict n, q = map(int, input().split()) OUTs = defaultdict(list) UF = UnionFind(n) E = [] for _ in range(q): a, b, c = map(int, input().split()) a -= 1 b -= 1 OUTs[a].append((b, c)) E.append((a, b)) UF.union(a, b) mod = 998244353 ans = 1 seen = set() G = scc(n, E) g_num = 0 for GG in G: if len(GG) > 1: flag = True for i in GG: if i in seen: flag = False break if not flag: continue g_num += 1 i = GG[0] seen.add(i) P = defaultdict(int) P[i] = 0 Que = deque([i]) flag = True while Que: ci = Que.popleft() pi = P[ci] for j, pj in OUTs[ci]: if j not in seen: P[j] = pj ^ pi seen.add(j) Que.append(j) else: if P[j] != pj ^ pi: flag = False break if flag: ans *= 2 ans %= mod else: print(0) exit() total_g_num = UF.group_count() ans *= pow(2, total_g_num - g_num, mod) ans %= mod print(ans)