結果

問題 No.2643 Many Range Sums Problems
ユーザー PNJPNJ
提出日時 2024-02-19 22:16:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,840 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 87,988 KB
最終ジャッジ日時 2024-02-19 22:17:23
合計ジャッジ時間 29,304 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 41 ms
61,460 KB
testcase_03 AC 1,640 ms
86,116 KB
testcase_04 AC 1,516 ms
84,516 KB
testcase_05 AC 1,345 ms
83,656 KB
testcase_06 AC 1,936 ms
87,988 KB
testcase_07 AC 788 ms
82,424 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 721 ms
81,340 KB
testcase_11 AC 625 ms
80,124 KB
testcase_12 AC 790 ms
83,632 KB
testcase_13 AC 1,407 ms
86,604 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 1,506 ms
85,428 KB
testcase_18 AC 590 ms
80,952 KB
testcase_19 AC 214 ms
78,128 KB
testcase_20 WA -
testcase_21 AC 207 ms
77,872 KB
testcase_22 AC 313 ms
78,752 KB
testcase_23 AC 426 ms
79,488 KB
testcase_24 AC 650 ms
83,008 KB
testcase_25 AC 624 ms
82,024 KB
testcase_26 AC 198 ms
78,644 KB
testcase_27 AC 194 ms
78,128 KB
testcase_28 AC 31 ms
53,460 KB
testcase_29 AC 30 ms
53,460 KB
testcase_30 AC 835 ms
82,708 KB
testcase_31 WA -
testcase_32 AC 805 ms
82,900 KB
testcase_33 AC 762 ms
82,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class WeightedUnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        # 根への距離を管理
        self.weight = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            y = self.find(self.par[x])
            # 親への重みを追加しながら根まで走査
            self.weight[x] += self.weight[self.par[x]]
            self.par[x] = y
            return y

    # 併合
    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        # xの木の高さ < yの木の高さ
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        # xの木の高さ ≧ yの木の高さ
        else:
            self.par[ry] = rx
            self.weight[ry] = -w - self.weight[y] + self.weight[x]
            # 木の高さが同じだった場合の処理
            if self.rank[rx] == self.rank[ry]:
                self.rank[rx] += 1

    # 同じ集合に属するか
    def same(self, x, y):
        return self.find(x) == self.find(y)

    # xからyへのコスト
    def diff(self, x, y):
        return self.weight[x] - self.weight[y]

def Map():
  return list(map(int,input().split()))

inf = 10**18
N,K = Map()
E = []
for j in range(N):
  R,X = Map()
  E.append((j,R,X))


for j in range(N):
  uf = WeightedUnionFind(N)
  ans = 'Yes'
  for i,r,x in E:
    if i == j:
      continue
    if uf.same(i,r):
      if uf.diff(i,r) == x:
        continue
      else:
        ans = 'No'
        break
    else:
      uf.union(i,r,x)
  for i in range(1,N+1):
    if not uf.same(i-1,i):
      continue
    if 0 <= uf.diff(i-1,i) <= K:
      continue
    ans = 'No'
    break
  print(ans)
0