結果
| 問題 |
No.2277 Honest or Dishonest ?
|
| コンテスト | |
| ユーザー |
prin_kemkem
|
| 提出日時 | 2023-04-22 14:49:28 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 758 ms / 2,000 ms |
| コード長 | 3,791 bytes |
| コンパイル時間 | 408 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 121,664 KB |
| 最終ジャッジ日時 | 2024-11-08 06:52:39 |
| 合計ジャッジ時間 | 26,441 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 50 |
ソースコード
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))
prin_kemkem