結果
問題 | No.2277 Honest or Dishonest ? |
ユーザー |
|
提出日時 | 2023-04-21 22:56:13 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 739 ms / 2,000 ms |
コード長 | 4,476 bytes |
コンパイル時間 | 598 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 152,640 KB |
最終ジャッジ日時 | 2024-11-08 06:42:18 |
合計ジャッジ時間 | 25,494 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 50 |
ソースコード
import sysinput = sys.stdin.readlineclass SCC:def __init__(self,n):self.n = nself.edges = []def csr(self):self.start = [0]*(self.n+1)self.elist = [0]*len(self.edges)for e in self.edges:self.start[e[0]+1] += 1for i in range(1,self.n+1):self.start[i] += self.start[i-1]counter = self.start[:]for e in self.edges:self.elist[counter[e[0]]] = e[1]counter[e[0]] += 1def add_edge(self,u,v):self.edges.append((u,v))def scc_ids(self):self.csr()n = self.nnow_ord = group_num = 0visited = []low = [0]*norder = [-1]*nids = [0]*nparent = [-1]*nstack = []for i in range(n):if order[i] == -1:stack.append(i)stack.append(i)while stack:v = stack.pop()if order[v] == -1:low[v] = order[v] = now_ordnow_ord += 1visited.append(v)for i in range(self.start[v],self.start[v+1]):to = self.elist[i]if order[to] == -1:stack.append(to)stack.append(to)parent[to] = velse:low[v] = min(low[v],order[to])else:if low[v] == order[v]:while True:u = visited.pop()order[u] = nids[u] = group_numif u == v:breakgroup_num += 1if parent[v] != -1:low[parent[v]] = min(low[parent[v]],low[v])for i,x in enumerate(ids):ids[i] = group_num-1-xreturn group_num,idsdef scc(self):group_num,ids = self.scc_ids()groups = [[] for i in range(group_num)]for i,x in enumerate(ids):groups[x].append(i)return groupsclass Unionfind:def __init__(self,n):self.uf = [-1]*ndef find(self,x):if self.uf[x] < 0:return xelse:self.uf[x] = self.find(self.uf[x])return self.uf[x]def same(self,x,y):return self.find(x) == self.find(y)def union(self,x,y):x = self.find(x)y = self.find(y)if x == y:return Falseif self.uf[x] > self.uf[y]:x,y = y,xself.uf[x] += self.uf[y]self.uf[y] = xreturn Truedef size(self,x):x = self.find(x)return -self.uf[x]n,q = map(int,input().split())e = [[] for i in range(n)]re = [[] for i in range(n)]mod = 998244353ans = 1scc = SCC(n)for i in range(q):a,b,c = map(int,input().split())a,b = a-1,b-1scc.add_edge(a,b)e[a].append([b,c])re[b].append([a,c])vis = [0]*nid = 0def search(u,c,id):dic = {}dic[u] = cvis[u] = idq = [u]ok = 1# print(u,c,dic)while q:now = q.pop()c = dic[now]# print(now,c)for nex,nc in e[now]:if c == 0:judge = ncelse:judge = nc^1# print(now,nex,nc,judge,dic)if nex in dic and dic[nex] != judge:ok = 0if vis[nex] == id:continuedic[nex] = judgevis[nex] = idq.append(nex)for nex,nc in re[now]:if nc == c:judge = 0else:judge = 1if nex in dic and dic[nex] != judge:ok = 0if vis[nex] == id:continuedic[nex] = judgevis[nex] = idq.append(nex)# print(ok)return okgr = scc.scc()for g in gr:# print(g)if vis[g[0]]:continueu = g[0]num = search(u,0,id+1) + search(u,1,id+2)ans *= numans %= modid += 2print(ans)