結果
問題 | No.2277 Honest or Dishonest ? |
ユーザー |
|
提出日時 | 2023-04-21 22:13:49 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,014 ms / 2,000 ms |
コード長 | 3,023 bytes |
コンパイル時間 | 369 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 123,648 KB |
最終ジャッジ日時 | 2024-11-08 06:34:54 |
合計ジャッジ時間 | 34,372 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 50 |
ソースコード
from collections import defaultdictclass UnionFind():def __init__(self, n):self.n = nself.parents = [-1] * ndef find(self, x):# 要素xが属するグループの根if self.parents[x] < 0:return xelse:self.parents[x] = self.find(self.parents[x])return self.parents[x]def union(self, x, y):# 要素xが属するグループと要素yが属するグループを併合x = self.find(x)y = self.find(y)if x == y:returnif self.parents[x] > self.parents[y]:x, y = y, xself.parents[x] += self.parents[y]self.parents[y] = xdef size(self, x):# 要素xが属するグループの要素数return -self.parents[self.find(x)]def same(self, x, y):# 要素x,yが同じグループに属するかどうかreturn self.find(x) == self.find(y)def members(self, x):# 要素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_membersdef __str__(self):return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())import syssys.setrecursionlimit(500000)#import pypyjit#pypyjit.set_param('max_unroll_recursion=-1')def I(): return int(sys.stdin.readline().rstrip())def MI(): return map(int,sys.stdin.readline().rstrip().split())def TI(): return tuple(map(int,sys.stdin.readline().rstrip().split()))def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))def SI(): return sys.stdin.readline().rstrip()def LS(): return list(sys.stdin.readline().rstrip())#for i, pi in enumerate(p):from collections import defaultdict,dequeimport bisectimport itertoolsdic = defaultdict(int)d = deque()YN = ['No','Yes']N,Q = MI()dp = [-1]*(N)S = []for _ in range(Q):a,b,c = MI()S.append((c,a-1,b-1))S.sort()uf = UnionFind(N*2)count = Qfor c,a,b in S:if not c:if uf.same(a+N,b) or uf.same(a,b+N):print(0)exit()if uf.same(a,b) or uf.same(a+N,b+N):count -= 1uf.union(a,b)uf.union(a+N,b+N)else:if uf.same(a,b) or uf.same(a+N,b+N):print(0)exit()if uf.same(a+N,b) or uf.same(a,b+N):count -= 1uf.union(a+N,b)uf.union(a,b+N)mod = 998244353x = uf.all_group_members()S = []for i in x:s = []for j in x[i]:if 0 <= j <= N-1:s.append(j)if s:S.append(s)print(pow(2,N-count,mod))