結果

問題 No.2277 Honest or Dishonest ?
ユーザー lloyzlloyz
提出日時 2023-04-21 23:12:53
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,581 bytes
コンパイル時間 478 ms
コンパイル使用メモリ 82,548 KB
実行使用メモリ 118,148 KB
最終ジャッジ日時 2024-04-25 19:07:29
合計ジャッジ時間 22,215 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
56,336 KB
testcase_01 AC 41 ms
56,120 KB
testcase_02 AC 43 ms
55,320 KB
testcase_03 AC 525 ms
114,116 KB
testcase_04 AC 480 ms
107,796 KB
testcase_05 AC 245 ms
97,920 KB
testcase_06 AC 450 ms
103,196 KB
testcase_07 WA -
testcase_08 AC 204 ms
91,388 KB
testcase_09 AC 352 ms
94,044 KB
testcase_10 WA -
testcase_11 AC 295 ms
84,608 KB
testcase_12 AC 229 ms
88,616 KB
testcase_13 AC 507 ms
108,276 KB
testcase_14 AC 208 ms
91,380 KB
testcase_15 AC 225 ms
91,104 KB
testcase_16 AC 446 ms
98,476 KB
testcase_17 WA -
testcase_18 AC 435 ms
101,636 KB
testcase_19 AC 458 ms
102,268 KB
testcase_20 WA -
testcase_21 AC 369 ms
94,652 KB
testcase_22 WA -
testcase_23 AC 411 ms
103,444 KB
testcase_24 AC 363 ms
91,560 KB
testcase_25 AC 361 ms
95,792 KB
testcase_26 AC 183 ms
93,664 KB
testcase_27 AC 316 ms
89,944 KB
testcase_28 AC 233 ms
94,068 KB
testcase_29 AC 271 ms
92,032 KB
testcase_30 WA -
testcase_31 AC 473 ms
104,120 KB
testcase_32 AC 274 ms
97,188 KB
testcase_33 WA -
testcase_34 AC 548 ms
115,544 KB
testcase_35 AC 144 ms
86,032 KB
testcase_36 WA -
testcase_37 AC 499 ms
106,220 KB
testcase_38 AC 199 ms
83,116 KB
testcase_39 AC 279 ms
84,696 KB
testcase_40 AC 162 ms
86,664 KB
testcase_41 AC 569 ms
115,180 KB
testcase_42 AC 460 ms
103,904 KB
testcase_43 WA -
testcase_44 AC 530 ms
116,012 KB
testcase_45 AC 522 ms
117,012 KB
testcase_46 AC 522 ms
116,560 KB
testcase_47 AC 537 ms
116,808 KB
testcase_48 AC 530 ms
114,448 KB
testcase_49 AC 533 ms
114,720 KB
testcase_50 WA -
testcase_51 AC 552 ms
118,148 KB
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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:
        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)
0