結果

問題 No.1502 Many Simple Additions
ユーザー ansainansain
提出日時 2021-05-07 23:23:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,243 ms / 2,000 ms
コード長 3,574 bytes
コンパイル時間 509 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 85,044 KB
最終ジャッジ日時 2024-09-15 18:25:21
合計ジャッジ時間 13,796 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
11,008 KB
testcase_01 AC 32 ms
11,008 KB
testcase_02 AC 31 ms
11,008 KB
testcase_03 AC 32 ms
10,880 KB
testcase_04 AC 32 ms
11,136 KB
testcase_05 AC 32 ms
11,008 KB
testcase_06 AC 32 ms
11,008 KB
testcase_07 AC 33 ms
11,136 KB
testcase_08 AC 31 ms
11,008 KB
testcase_09 AC 32 ms
11,008 KB
testcase_10 AC 32 ms
10,880 KB
testcase_11 AC 32 ms
10,880 KB
testcase_12 AC 32 ms
11,008 KB
testcase_13 AC 32 ms
10,880 KB
testcase_14 AC 32 ms
10,880 KB
testcase_15 AC 32 ms
10,880 KB
testcase_16 AC 31 ms
11,008 KB
testcase_17 AC 32 ms
10,880 KB
testcase_18 AC 32 ms
10,880 KB
testcase_19 AC 32 ms
11,008 KB
testcase_20 AC 31 ms
10,880 KB
testcase_21 AC 32 ms
11,008 KB
testcase_22 AC 32 ms
11,008 KB
testcase_23 AC 32 ms
10,880 KB
testcase_24 AC 32 ms
10,880 KB
testcase_25 AC 32 ms
11,008 KB
testcase_26 AC 32 ms
11,008 KB
testcase_27 AC 536 ms
59,556 KB
testcase_28 AC 538 ms
60,044 KB
testcase_29 AC 700 ms
26,624 KB
testcase_30 AC 1,236 ms
85,044 KB
testcase_31 AC 1,243 ms
84,800 KB
testcase_32 AC 1,224 ms
84,704 KB
testcase_33 AC 826 ms
72,012 KB
testcase_34 AC 849 ms
73,440 KB
testcase_35 AC 856 ms
72,324 KB
testcase_36 AC 522 ms
61,784 KB
testcase_37 AC 542 ms
61,664 KB
testcase_38 AC 547 ms
65,768 KB
testcase_39 AC 347 ms
42,440 KB
testcase_40 AC 168 ms
22,144 KB
testcase_41 AC 220 ms
16,256 KB
testcase_42 AC 686 ms
65,516 KB
testcase_43 AC 32 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict, Counter, deque
from itertools import permutations, combinations, product, combinations_with_replacement, groupby, accumulate
import operator
from math import sqrt, gcd, factorial
# from math import isqrt, prod,comb  # python3.8用(notpypy)
#from bisect import bisect_left,bisect_right
#from functools import lru_cache,reduce
#from heapq import heappush,heappop,heapify,heappushpop,heapreplace
#import numpy as np
#import networkx as nx
#from networkx.utils import UnionFind
#from numba import njit, b1, i1, i4, i8, f8
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, NegativeCycleError
# numba例 @njit(i1(i4[:], i8[:, :]),cache=True) 引数i4配列、i8 2次元配列,戻り値i1
def input(): return sys.stdin.readline().rstrip()
def divceil(n, k): return 1+(n-1)//k  # n/kの切り上げを返す
def yn(hantei, yes='Yes', no='No'): print(yes if hantei else no)


def main():
    mod = 10**9+7
    mod2 = 998244353
    n, m, k = map(int, input().split())

    graph = [[] for _ in range(n)]

    for i in range(m):
        a, b, c = map(lambda x: int(x)-1, input().split())
        graph[a].append((b, c+1))
        graph[b].append((a, c+1))

    dist = [-1]*n
    kans = 1
    lessk = 1
    for i in range(n):
        if dist[i] != -1:
            continue
        dist[i] = [1, 0]

        d = deque()
        d.append(i)
        used = dict()
        point = set([i])
        while d:
            v = d.popleft()
            for i, num in graph[v]:
                used[(v, i)] = num
                if dist[i] != -1:
                    continue
                dist[i] = [-dist[v][0], -dist[v][1]+num]
                point.add(i)
                d.append(i)
        kakutei = None
        for (a, b), num in used.items():
            aa1, aa2 = dist[a]
            bb1, bb2 = dist[b]
            if aa1+bb1 == 0:
                if aa2+bb2 != num:
                    print(0)
                    return
            else:
                if (num-aa2-bb2) % 2 == 1:
                    print(0)
                    return
                else:
                    if kakutei == None:
                        kakutei = (num-aa2-bb2)//(aa1+bb1)
                    elif kakutei != (num-aa2-bb2)//(aa1+bb1):
                        print(0)
                        return
        if kakutei == None:
            plusmax = max(dist[p][1] if dist[p][0] ==
                          1 else -10**10 for p in point)
            plusmin = min(dist[p][1] if dist[p][0] ==
                          1 else 10**10 for p in point)
            minusmax = max(dist[p][1] if dist[p][0] == -
                           1 else -10**10 for p in point)
            minusmin = min(dist[p][1] if dist[p][0] == -
                           1 else 10**10 for p in point)
            kans *= max(0, min(k-plusmax, minusmin-1) -
                        max(-plusmin+1, -k+minusmax)+1)
            kans %= mod
            lessk *= max(0, min(k-1-plusmax, minusmin-1) -
                         max(-plusmin+1, -k+1+minusmax)+1)
            lessk %= mod
        else:
            kans *= int(all(1 <= dist[p][0]*kakutei +
                            dist[p][1] <= k for p in point))
            lessk *= int(all(1 <= dist[p][0]*kakutei +
                             dist[p][1] <= k-1 for p in point))
    print((kans-lessk)%mod)
    #print(kans,lessk,kakutei,dist,min(k-plusmax, minusmin+1),max(-plusmin-1, -k+minusmax))


if __name__ == '__main__':
    main()
0