結果

問題 No.1502 Many Simple Additions
ユーザー とりゐとりゐ
提出日時 2021-04-20 03:18:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 716 ms / 2,000 ms
コード長 3,271 bytes
コンパイル時間 394 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 137,856 KB
最終ジャッジ日時 2024-09-15 18:08:14
合計ジャッジ時間 9,411 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
54,400 KB
testcase_01 AC 46 ms
54,400 KB
testcase_02 AC 46 ms
54,528 KB
testcase_03 AC 46 ms
54,272 KB
testcase_04 AC 46 ms
54,144 KB
testcase_05 AC 45 ms
54,016 KB
testcase_06 AC 45 ms
54,400 KB
testcase_07 AC 46 ms
54,656 KB
testcase_08 AC 45 ms
54,016 KB
testcase_09 AC 46 ms
54,272 KB
testcase_10 AC 45 ms
54,272 KB
testcase_11 AC 47 ms
54,656 KB
testcase_12 AC 47 ms
54,400 KB
testcase_13 AC 47 ms
54,272 KB
testcase_14 AC 46 ms
54,784 KB
testcase_15 AC 47 ms
54,528 KB
testcase_16 AC 48 ms
54,272 KB
testcase_17 AC 49 ms
55,168 KB
testcase_18 AC 49 ms
54,912 KB
testcase_19 AC 49 ms
54,400 KB
testcase_20 AC 50 ms
55,296 KB
testcase_21 AC 49 ms
54,784 KB
testcase_22 AC 48 ms
54,912 KB
testcase_23 AC 49 ms
55,168 KB
testcase_24 AC 46 ms
54,656 KB
testcase_25 AC 48 ms
54,144 KB
testcase_26 AC 46 ms
54,784 KB
testcase_27 AC 265 ms
106,332 KB
testcase_28 AC 265 ms
106,368 KB
testcase_29 AC 155 ms
103,808 KB
testcase_30 AC 697 ms
131,772 KB
testcase_31 AC 691 ms
132,992 KB
testcase_32 AC 716 ms
137,856 KB
testcase_33 AC 438 ms
105,344 KB
testcase_34 AC 472 ms
107,648 KB
testcase_35 AC 450 ms
105,088 KB
testcase_36 AC 190 ms
94,336 KB
testcase_37 AC 192 ms
94,080 KB
testcase_38 AC 312 ms
96,512 KB
testcase_39 AC 176 ms
85,120 KB
testcase_40 AC 328 ms
91,264 KB
testcase_41 AC 181 ms
84,992 KB
testcase_42 AC 536 ms
125,440 KB
testcase_43 AC 47 ms
54,272 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,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,0
        else:
          if seen[x[0]][1]+seen[V][0]!=W:
            return 0,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),max(min((k-1)-max0,min1-1)-max(1-min0,max1-(k-1),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[g[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
  num1=0
  num2=0
  if flag and min(seen2)>0 and max(seen2)<=k:
    num1=1
  if flag and min(seen2)>0 and max(seen2)<=k-1:
    num2=1
  return num1,num2

cnt1=1
cnt2=1
for i in list(uf.all_group_members().values()):
  a,b=f(i)
  cnt1*=a
  cnt2*=b
  cnt1=cnt1%mod
  cnt2=cnt2%mod
print((cnt1-cnt2)%mod)
0