結果

問題 No.1502 Many Simple Additions
ユーザー mkawa2mkawa2
提出日時 2024-01-02 02:15:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 484 ms / 2,000 ms
コード長 2,774 bytes
コンパイル時間 3,023 ms
コンパイル使用メモリ 81,444 KB
実行使用メモリ 116,792 KB
最終ジャッジ日時 2024-01-02 02:15:12
合計ジャッジ時間 8,571 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,600 KB
testcase_01 AC 41 ms
55,600 KB
testcase_02 AC 40 ms
55,600 KB
testcase_03 AC 40 ms
55,600 KB
testcase_04 AC 40 ms
55,600 KB
testcase_05 AC 41 ms
55,600 KB
testcase_06 AC 42 ms
55,600 KB
testcase_07 AC 42 ms
55,600 KB
testcase_08 AC 41 ms
55,600 KB
testcase_09 AC 41 ms
55,600 KB
testcase_10 AC 41 ms
55,600 KB
testcase_11 AC 42 ms
55,600 KB
testcase_12 AC 43 ms
55,600 KB
testcase_13 AC 41 ms
55,600 KB
testcase_14 AC 41 ms
55,600 KB
testcase_15 AC 41 ms
55,600 KB
testcase_16 AC 42 ms
55,600 KB
testcase_17 AC 41 ms
55,600 KB
testcase_18 AC 41 ms
55,600 KB
testcase_19 AC 41 ms
55,600 KB
testcase_20 AC 41 ms
55,600 KB
testcase_21 AC 42 ms
55,600 KB
testcase_22 AC 41 ms
55,600 KB
testcase_23 AC 41 ms
55,600 KB
testcase_24 AC 41 ms
55,600 KB
testcase_25 AC 42 ms
55,600 KB
testcase_26 AC 41 ms
55,600 KB
testcase_27 AC 294 ms
115,972 KB
testcase_28 AC 296 ms
115,980 KB
testcase_29 AC 58 ms
72,212 KB
testcase_30 AC 465 ms
116,580 KB
testcase_31 AC 484 ms
116,792 KB
testcase_32 AC 460 ms
116,716 KB
testcase_33 AC 362 ms
112,624 KB
testcase_34 AC 366 ms
112,296 KB
testcase_35 AC 354 ms
112,644 KB
testcase_36 AC 255 ms
110,780 KB
testcase_37 AC 249 ms
110,776 KB
testcase_38 AC 268 ms
111,540 KB
testcase_39 AC 172 ms
95,304 KB
testcase_40 AC 202 ms
90,724 KB
testcase_41 AC 95 ms
79,068 KB
testcase_42 AC 302 ms
107,484 KB
testcase_43 AC 41 ms
55,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 62)

md = 10**9+7
# md = 998244353

from collections import defaultdict

n,m,k=LI()
to=[[] for _ in range(n)]
uv2z=defaultdict(int)
for _ in range(m):
    u,v,z=LI()
    u,v=u-1,v-1
    to[u].append(v)
    to[v].append(u)
    uv2z[u,v]=uv2z[v,u]=z

def dfs(root=0):
    aa[root]=1
    stack = [(u, root) for u in to[root]]
    lmin=rmin=inf
    lmax=rmax=-inf
    while stack:
        u, p = stack.pop()
        z = uv2z[u, p]
        if aa[u]:
            if aa[u]==aa[p]:
                # 2x=au(z-bu-bp)
                x2=aa[u]*(z-bb[u]-bb[p])
                if x2 < 2 or x2 > k*2 or x2&1:
                    print(0)
                    exit()
                x=x2//2
                if xx[root]==None:
                    xx[root]=x
                elif xx[root]!=x:
                    print(0)
                    exit()
            elif bb[u]+bb[p]!=z:
                print(0)
                exit()
        else:
            parent[u]=p
            aa[u]=-aa[p]
            bb[u]=z-bb[p]
            if aa[u]==1:
                lmin,lmax=min(lmin,bb[u]),max(lmax,bb[u])
            else:
                rmin, rmax = min(rmin, bb[u]), max(rmax, bb[u])
            for v in to[u]:
                if aa[v]:continue
                stack.append((v,u))
    # x+lmin>=1
    # x>=1-lmin
    # -x+rmin>=1
    # x<=rmin-1
    # x+lmax<=k
    # x<=k-lmax
    # -x+rmax<=k
    # x>=rmax-k
    if xx[root]==None:
        mn=max(1-lmin,rmax-k,1)
        mx=min(rmin-1,k-lmax,k)
        a1=max(0,mx-mn+1)
        mn=max(1-lmin,rmax-k+1,1)
        mx=min(rmin-1,k-1-lmax,k-1)
        a2=max(0,mx-mn+1)
        return a1,a2
    x=xx[root]
    if x+lmin<1 or -x+rmin<1:
        print(0)
        exit()
    a1=a2=0
    if x+lmax<=k and -x+rmax<=k:a1=1
    if x+lmax<k and -x+rmax<k:a2=1
    return a1,a2

parent = [-1]*n
aa=[0]*n
bb=[0]*n
xx=[None]*n

s1=s2=1
for u in range(n):
    if aa[u]:continue
    a1,a2=dfs(u)
    s1*=a1
    s2*=a2
    s1%=md
    s2%=md
    # print(u,a1,a2,s1,s2)

# print(aa)
# print(bb)
# print(xx)
print((s1-s2)%md)
0