結果

問題 No.2277 Honest or Dishonest ?
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,864 KB
testcase_01 AC 43 ms
52,864 KB
testcase_02 AC 46 ms
53,760 KB
testcase_03 AC 556 ms
124,248 KB
testcase_04 AC 610 ms
135,652 KB
testcase_05 AC 329 ms
104,148 KB
testcase_06 AC 454 ms
109,264 KB
testcase_07 AC 416 ms
111,268 KB
testcase_08 AC 304 ms
103,704 KB
testcase_09 AC 408 ms
118,060 KB
testcase_10 AC 322 ms
97,896 KB
testcase_11 AC 242 ms
88,772 KB
testcase_12 AC 226 ms
93,184 KB
testcase_13 AC 515 ms
119,804 KB
testcase_14 AC 228 ms
98,000 KB
testcase_15 AC 279 ms
98,324 KB
testcase_16 AC 436 ms
111,416 KB
testcase_17 AC 267 ms
91,648 KB
testcase_18 AC 593 ms
130,500 KB
testcase_19 AC 570 ms
136,872 KB
testcase_20 AC 491 ms
125,552 KB
testcase_21 AC 357 ms
101,384 KB
testcase_22 AC 433 ms
113,764 KB
testcase_23 AC 443 ms
110,684 KB
testcase_24 AC 392 ms
113,208 KB
testcase_25 AC 491 ms
123,328 KB
testcase_26 AC 247 ms
97,368 KB
testcase_27 AC 280 ms
93,512 KB
testcase_28 AC 307 ms
98,608 KB
testcase_29 AC 359 ms
106,976 KB
testcase_30 AC 291 ms
96,048 KB
testcase_31 AC 461 ms
116,704 KB
testcase_32 AC 340 ms
104,984 KB
testcase_33 AC 588 ms
135,124 KB
testcase_34 AC 597 ms
128,736 KB
testcase_35 AC 198 ms
90,624 KB
testcase_36 AC 413 ms
108,376 KB
testcase_37 AC 498 ms
115,676 KB
testcase_38 AC 176 ms
84,224 KB
testcase_39 AC 242 ms
88,264 KB
testcase_40 AC 218 ms
88,704 KB
testcase_41 AC 621 ms
136,856 KB
testcase_42 AC 475 ms
119,368 KB
testcase_43 AC 714 ms
147,516 KB
testcase_44 AC 723 ms
147,300 KB
testcase_45 AC 722 ms
152,640 KB
testcase_46 AC 715 ms
149,404 KB
testcase_47 AC 711 ms
149,620 KB
testcase_48 AC 730 ms
151,276 KB
testcase_49 AC 730 ms
151,388 KB
testcase_50 AC 703 ms
150,444 KB
testcase_51 AC 739 ms
149,472 KB
testcase_52 AC 702 ms
150,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class SCC:

    def __init__(self,n):
        self.n = n
        self.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] += 1
        for 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]] += 1

    def add_edge(self,u,v):
        self.edges.append((u,v))

    def scc_ids(self):
        self.csr()
        n = self.n
        now_ord = group_num = 0
        visited = []
        low = [0]*n
        order = [-1]*n
        ids = [0]*n
        parent = [-1]*n
        stack = []
        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_ord
                        now_ord += 1
                        visited.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] = v
                            else:
                                low[v] = min(low[v],order[to])
                    else:
                        if low[v] == order[v]:
                            while True:
                                u = visited.pop()
                                order[u] = n
                                ids[u] = group_num
                                if u == v:
                                    break
                            group_num += 1
                        if parent[v] != -1:
                            low[parent[v]] = min(low[parent[v]],low[v])
        for i,x in enumerate(ids):
            ids[i] = group_num-1-x

        return group_num,ids


    def 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 groups

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            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 False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def 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 = 998244353
ans = 1
scc = SCC(n)
for i in range(q):
    a,b,c = map(int,input().split())
    a,b = a-1,b-1
    scc.add_edge(a,b)
    e[a].append([b,c])
    re[b].append([a,c])


vis = [0]*n
id = 0
def search(u,c,id):
    dic = {}

    dic[u] = c
    vis[u] = id

    q = [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 = nc
            else:
                judge = nc^1

            # print(now,nex,nc,judge,dic)
            if nex in dic and dic[nex] != judge:
                ok = 0
            
            if vis[nex] == id:
                continue
            dic[nex] = judge
            vis[nex] = id
            q.append(nex)

        for nex,nc in re[now]:
            if nc == c:
                judge = 0
            else:
                judge = 1
            
            if nex in dic and dic[nex] != judge:
                ok = 0
            
            if vis[nex] == id:
                continue
            dic[nex] = judge
            vis[nex] = id
            q.append(nex)
            

    # print(ok)
    return ok


gr = scc.scc()
for g in gr:
    # print(g)
    if vis[g[0]]:
        continue

    u = g[0]
    num = search(u,0,id+1) + search(u,1,id+2)
    ans *= num
    ans %= mod
    id += 2
print(ans)
        
0