結果

問題 No.2277 Honest or Dishonest ?
ユーザー prin_kemkemprin_kemkem
提出日時 2023-04-22 14:49:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 649 ms / 2,000 ms
コード長 3,791 bytes
コンパイル時間 325 ms
コンパイル使用メモリ 82,552 KB
実行使用メモリ 121,924 KB
最終ジャッジ日時 2024-04-25 19:15:50
合計ジャッジ時間 21,783 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
61,640 KB
testcase_01 AC 44 ms
61,768 KB
testcase_02 AC 46 ms
62,248 KB
testcase_03 AC 516 ms
111,364 KB
testcase_04 AC 533 ms
114,076 KB
testcase_05 AC 181 ms
93,608 KB
testcase_06 AC 413 ms
101,900 KB
testcase_07 AC 399 ms
100,276 KB
testcase_08 AC 140 ms
95,116 KB
testcase_09 AC 413 ms
101,724 KB
testcase_10 AC 310 ms
93,896 KB
testcase_11 AC 226 ms
85,432 KB
testcase_12 AC 181 ms
85,556 KB
testcase_13 AC 500 ms
108,472 KB
testcase_14 AC 237 ms
87,760 KB
testcase_15 AC 189 ms
91,368 KB
testcase_16 AC 432 ms
101,140 KB
testcase_17 AC 262 ms
88,308 KB
testcase_18 AC 505 ms
112,344 KB
testcase_19 AC 545 ms
113,892 KB
testcase_20 AC 435 ms
105,860 KB
testcase_21 AC 366 ms
96,468 KB
testcase_22 AC 383 ms
100,656 KB
testcase_23 AC 370 ms
101,484 KB
testcase_24 AC 385 ms
99,068 KB
testcase_25 AC 457 ms
106,112 KB
testcase_26 AC 114 ms
92,420 KB
testcase_27 AC 293 ms
89,196 KB
testcase_28 AC 211 ms
90,860 KB
testcase_29 AC 313 ms
98,168 KB
testcase_30 AC 275 ms
90,508 KB
testcase_31 AC 433 ms
102,168 KB
testcase_32 AC 271 ms
96,056 KB
testcase_33 AC 542 ms
112,180 KB
testcase_34 AC 523 ms
113,280 KB
testcase_35 AC 93 ms
85,580 KB
testcase_36 AC 447 ms
100,428 KB
testcase_37 AC 424 ms
104,756 KB
testcase_38 AC 196 ms
82,564 KB
testcase_39 AC 267 ms
86,344 KB
testcase_40 AC 113 ms
84,068 KB
testcase_41 AC 529 ms
116,476 KB
testcase_42 AC 444 ms
103,796 KB
testcase_43 AC 548 ms
119,496 KB
testcase_44 AC 587 ms
120,916 KB
testcase_45 AC 609 ms
121,924 KB
testcase_46 AC 606 ms
120,328 KB
testcase_47 AC 649 ms
120,648 KB
testcase_48 AC 592 ms
121,332 KB
testcase_49 AC 541 ms
120,800 KB
testcase_50 AC 465 ms
119,192 KB
testcase_51 AC 581 ms
120,404 KB
testcase_52 AC 577 ms
119,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque, Counter
import copy
from itertools import combinations, permutations, product, accumulate
from heapq import heapify, heappop, heappush
import math
import bisect
import sys
# sys.setrecursionlimit(700000)
input = lambda: sys.stdin.readline().rstrip('\n')
inf = float('inf')
mod1 = 10**9+7
mod2 = 998244353
def ceil_div(x, y): return -(-x//y)

#################################################

class Weight:
    def __init__(self, x) -> None:
        self.x = x
    def __add__(self, other):
        return Weight(self.x^other.x)
    def __sub__(self, other):
        return Weight(self.x^other.x)
    def __neg__(self):
        return Weight(self.x)
    def __eq__(self, other):
        return self.x == other.x
    def e():
        return Weight(0)

class WeightedUnionFind:
    #コンストラクタ
    def __init__(self, n, Weight=None):
        self.n = n
        self.parents = [-1]*n
        self.C = Weight
        if self.C is None:
            self.weight = [0]*n
        else:
            self.weight = [self.C.e() for _ in range(N)]

    #点xの根と根までの距離を調べる+親が根になるよう移動し辺の重みを更新
    def find(self, x):
        if self.parents[x] < 0:
            return x, 0 if self.C is None else Weight.e()
        else:
            r, w = self.find(self.parents[x])
            self.parents[x] = r
            self.weight[x] += w
            return self.parents[x], self.weight[x]

    #点x,yの属する集合同士を連結(要素数が少ない方を多い方に連結)
    #x->yの距離がwとなるように辺を張る
    #既に連結済みである場合、x->yの距離がwでなければ何もせずにFalseをreturn
    #それ以外の場合はTrueをreturn
    def union(self, x, y, w):
        rx, wx = self.find(x)
        ry, wy = self.find(y)
        if rx == ry:
            return wx-wy == w
        if self.parents[rx] > self.parents[ry]:
            self.weight[ry] = -w+wx-wy
            self.parents[rx] += self.parents[ry]
            self.parents[ry] = rx
        else:
            self.weight[rx] = w-wx+wy
            self.parents[ry] += self.parents[rx]
            self.parents[rx] = ry
        return True

    #点xが属する集合の要素数を取得
    def size(self, x):
        return -self.parents[self.find(x)[0]]

    #点x,yが同じ集合に属しているか判定
    def same(self, x, y):
        return self.find(x)[0] == self.find(y)[0]

    #点xの属する集合の全要素を取得
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i)[0] == 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 get_groups(self):
        groups = defaultdict(list)
        for x in range(self.n):
            groups[self.find(x)[0]].append(x)
        return groups

    # x->yの距離を取得
    # 非連結の場合Noneをreturn
    def dist(self, x, y):
        rx, wx = self.find(x)
        ry, wy = self.find(y)
        if rx != ry:
            return None
        else:
            return wx-wy

    #print(インスタンス)で、全集合の「根と全要素」を出力
    def __str__(self):
        return '\n'.join('{}:{}'.format(r, self.menbers(r)) for r in self.roots())

N, Q = map(int, input().split())
wuf = WeightedUnionFind(N, Weight)
qs = [tuple(map(int, input().split())) for _ in range(Q)]
for a, b, c in qs:
    a -= 1; b -= 1
    if not wuf.union(a, b, Weight(c)):
        print(0)
        exit()
print(pow(2, wuf.group_count(), mod2))
0