結果

問題 No.1502 Many Simple Additions
ユーザー とりゐとりゐ
提出日時 2021-04-17 02:49:43
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,057 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 87,224 KB
実行使用メモリ 149,064 KB
最終ジャッジ日時 2023-10-13 02:09:30
合計ジャッジ時間 11,352 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 90 ms
71,944 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 92 ms
71,816 KB
testcase_07 AC 93 ms
71,912 KB
testcase_08 WA -
testcase_09 AC 91 ms
71,444 KB
testcase_10 AC 91 ms
71,456 KB
testcase_11 AC 92 ms
71,808 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 93 ms
71,432 KB
testcase_15 AC 93 ms
71,596 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 RE -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 RE -
testcase_23 RE -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 226 ms
96,780 KB
testcase_37 WA -
testcase_38 AC 338 ms
98,232 KB
testcase_39 AC 205 ms
89,096 KB
testcase_40 AC 355 ms
93,416 KB
testcase_41 AC 207 ms
86,612 KB
testcase_42 AC 553 ms
121,960 KB
testcase_43 AC 92 ms
71,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from collections import deque

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == 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 all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

mod=10**9+7
INF=10**100
n,m,k=map(int,input().split())
edge=[[] for i in range(n)]
uf=UnionFind(n)
for i in range(m):
  a,b,c=map(int,input().split())
  edge[a-1].append([b-1,c])
  edge[b-1].append([a-1,c])
  uf.union(a-1,b-1)

def f(G):
  d={}
  for i in range(len(G)):
    d[G[i]]=i
  q=deque()
  q.append([0,0])
  seen=[[INF,INF] for i in range(len(G))]
  seen[0][0]=0
  while q:
    x=q.pop()
    for v in edge[G[x[0]]]:
      V=d[v[0]]
      W=v[1]
      if seen[V][(x[1]+1)%2]==INF:
        if x[1]==0:
          seen[V][1]=W-seen[x[0]][0]
        else:
          seen[V][0]=W-seen[x[0]][1]
        if seen[V][0]!=INF and seen[V][1]!=INF:
          if (seen[V][0]+seen[V][1])%2==1:
            return 0
          else:
            return bfs((seen[V][1]-seen[V][0])//2,G)
        else:
          q.append([V,(x[1]+1)%2])
      else:
        if x[1]==0:
          if seen[x[0]][0]+seen[V][1]!=W:
            return 0
        else:
          if seen[x[0]][1]+seen[V][0]!=W:
            return 0

  min0=INF
  min1=INF
  max0=-INF
  max1=-INF
  for i in range(len(G)):
    if seen[i][0]!=INF:
      min0=min(min0,seen[i][0])
      max0=max(max0,seen[i][0])
    if seen[i][1]!=INF:
      min1=min(min1,seen[i][1])
      max1=max(max1,seen[i][1])
  return max(min(k-max0,min1-1)-max(1-min0,max1-k,0)+1,0)

def bfs(a,g):
  seen2=[INF]*len(g)
  seen2[0]=a
  d2={}
  for i in range(len(g)):
    d2[g[i]]=i
  q2=deque()
  q2.append(0)
  flag=True
  while q2:
    y=q2.pop()
    for vertex2 in edge[y]:
      V2=d2[vertex2[0]]
      W2=vertex2[1]
      if seen2[V2]==INF:
        seen2[V2]=W2-seen2[y]
        q2.append(V2)
      else:
        if seen2[V2]!=W2-seen2[y]:
          flag=False
  if flag and min(seen2)>0:
    return 1
  else:
    return 0

ans=1
for i in list(uf.all_group_members().values()):
  ans*=f(i)
  ans=ans%mod
print(ans)
0