結果

問題 No.2277 Honest or Dishonest ?
ユーザー 👑 KazunKazun
提出日時 2023-04-22 01:17:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 251 ms / 2,000 ms
コード長 2,667 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 98,812 KB
最終ジャッジ日時 2024-04-25 19:11:51
合計ジャッジ時間 9,855 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
52,520 KB
testcase_01 AC 32 ms
53,584 KB
testcase_02 AC 33 ms
53,148 KB
testcase_03 AC 196 ms
89,568 KB
testcase_04 AC 214 ms
94,448 KB
testcase_05 AC 148 ms
89,104 KB
testcase_06 AC 146 ms
84,168 KB
testcase_07 AC 137 ms
85,040 KB
testcase_08 AC 156 ms
94,616 KB
testcase_09 AC 169 ms
88,152 KB
testcase_10 AC 130 ms
81,064 KB
testcase_11 AC 105 ms
78,512 KB
testcase_12 AC 103 ms
78,428 KB
testcase_13 AC 163 ms
86,828 KB
testcase_14 AC 93 ms
77,600 KB
testcase_15 AC 134 ms
87,424 KB
testcase_16 AC 136 ms
83,908 KB
testcase_17 AC 116 ms
80,188 KB
testcase_18 AC 202 ms
94,156 KB
testcase_19 AC 204 ms
94,500 KB
testcase_20 AC 164 ms
88,980 KB
testcase_21 AC 125 ms
81,520 KB
testcase_22 AC 154 ms
87,124 KB
testcase_23 AC 130 ms
82,488 KB
testcase_24 AC 149 ms
86,224 KB
testcase_25 AC 169 ms
90,896 KB
testcase_26 AC 118 ms
98,812 KB
testcase_27 AC 112 ms
80,420 KB
testcase_28 AC 135 ms
85,848 KB
testcase_29 AC 159 ms
91,880 KB
testcase_30 AC 114 ms
81,092 KB
testcase_31 AC 128 ms
83,708 KB
testcase_32 AC 160 ms
90,080 KB
testcase_33 AC 177 ms
92,088 KB
testcase_34 AC 169 ms
89,392 KB
testcase_35 AC 92 ms
84,320 KB
testcase_36 AC 132 ms
83,440 KB
testcase_37 AC 153 ms
83,276 KB
testcase_38 AC 84 ms
77,484 KB
testcase_39 AC 86 ms
77,860 KB
testcase_40 AC 90 ms
82,564 KB
testcase_41 AC 160 ms
88,228 KB
testcase_42 AC 154 ms
85,692 KB
testcase_43 AC 189 ms
92,920 KB
testcase_44 AC 223 ms
97,232 KB
testcase_45 AC 230 ms
97,428 KB
testcase_46 AC 228 ms
96,840 KB
testcase_47 AC 251 ms
97,444 KB
testcase_48 AC 222 ms
97,504 KB
testcase_49 AC 183 ms
94,064 KB
testcase_50 AC 191 ms
93,888 KB
testcase_51 AC 204 ms
93,980 KB
testcase_52 AC 207 ms
95,456 KB
権限があれば一括ダウンロードができます

ソースコード

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