結果

問題 No.2277 Honest or Dishonest ?
ユーザー 👑 Kazun
提出日時 2023-04-22 01:17:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 247 ms / 2,000 ms
コード長 2,667 bytes
コンパイル時間 386 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 98,432 KB
最終ジャッジ日時 2024-11-08 06:48:26
合計ジャッジ時間 11,293 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

class Separate_Alternative:
    def __init__(self, N):
        self.N=N

        self.same_edge=[[] for _ in range(N)]
        self.diff_edge=[[] for _ in range(N)]

    def same(self, x, y):
        self.same_edge[x].append(y)
        self.same_edge[y].append(x)

    def difference(self, x, y):
        self.diff_edge[x].append(y)
        self.diff_edge[y].append(x)

    def is_reasonable(self):
        T=[0]*self.N
        for x in range(self.N):
            if T[x]!=0:
                continue

            T[x]=1
            S=[x]
            while S:
                y=S.pop()
                for z in self.same_edge[y]:
                    if T[z]==0:
                        T[z]=T[y]
                        S.append(z)
                    elif T[z]!=T[y]:
                        return False

                for z in self.diff_edge[y]:
                    if T[z]==0:
                        T[z]=-T[y]
                        S.append(z)
                    elif T[z]==T[y]:
                        return False
        return True

    def separate(self):
        Sep=[]
        seen=[0]*self.N

        for x in range(self.N):
            if seen[x]!=0:
                continue

            seen[x]=1
            U=[x]; V=[]
            S=[x]
            while S:
                y=S.pop()
                for z in self.same_edge[y]:
                    if seen[z]==0:
                        seen[z]=seen[y]
                        S.append(z)

                        if seen[z]==1:
                            U.append(z)
                        else:
                            V.append(z)

                    elif seen[z]!=seen[y]:
                        return None

                for z in self.diff_edge[y]:
                    if seen[z]==0:
                        seen[z]=-seen[y]
                        S.append(z)

                        if seen[z]==1:
                            U.append(z)
                        else:
                            V.append(z)

                    elif seen[z]==seen[y]:
                        return None

            Sep.append((U,V))
        return Sep

#==================================================
def solve():
    N,Q=map(int,input().split())

    S=Separate_Alternative(N)
    for i in range(Q):
        A,B,C=map(int,input().split())
        A-=1; B-=1
        if C==0:
            S.same(A,B)
        else:
            S.difference(A,B)

    Sep=S.separate()
    if Sep is None:
        return 0
    else:
        return pow(2, len(Sep), 998244353)

#==================================================
import sys
input=sys.stdin.readline
write=sys.stdout.write

print(solve())
0