結果
問題 | No.1502 Many Simple Additions |
ユーザー | mkawa2 |
提出日時 | 2024-01-02 02:15:00 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 428 ms / 2,000 ms |
コード長 | 2,774 bytes |
コンパイル時間 | 699 ms |
コンパイル使用メモリ | 82,892 KB |
実行使用メモリ | 117,380 KB |
最終ジャッジ日時 | 2024-09-27 17:38:37 |
合計ジャッジ時間 | 7,145 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
54,920 KB |
testcase_01 | AC | 43 ms
55,500 KB |
testcase_02 | AC | 41 ms
55,188 KB |
testcase_03 | AC | 45 ms
55,740 KB |
testcase_04 | AC | 40 ms
54,664 KB |
testcase_05 | AC | 41 ms
55,856 KB |
testcase_06 | AC | 41 ms
55,056 KB |
testcase_07 | AC | 40 ms
54,860 KB |
testcase_08 | AC | 40 ms
55,328 KB |
testcase_09 | AC | 41 ms
56,100 KB |
testcase_10 | AC | 40 ms
55,240 KB |
testcase_11 | AC | 41 ms
55,884 KB |
testcase_12 | AC | 41 ms
55,664 KB |
testcase_13 | AC | 41 ms
54,824 KB |
testcase_14 | AC | 44 ms
55,060 KB |
testcase_15 | AC | 43 ms
55,896 KB |
testcase_16 | AC | 43 ms
55,816 KB |
testcase_17 | AC | 45 ms
56,124 KB |
testcase_18 | AC | 45 ms
54,756 KB |
testcase_19 | AC | 44 ms
54,696 KB |
testcase_20 | AC | 45 ms
55,476 KB |
testcase_21 | AC | 45 ms
55,268 KB |
testcase_22 | AC | 44 ms
55,308 KB |
testcase_23 | AC | 45 ms
54,940 KB |
testcase_24 | AC | 44 ms
55,116 KB |
testcase_25 | AC | 45 ms
54,572 KB |
testcase_26 | AC | 45 ms
55,300 KB |
testcase_27 | AC | 260 ms
116,360 KB |
testcase_28 | AC | 259 ms
116,548 KB |
testcase_29 | AC | 58 ms
72,132 KB |
testcase_30 | AC | 428 ms
117,064 KB |
testcase_31 | AC | 419 ms
117,104 KB |
testcase_32 | AC | 402 ms
117,380 KB |
testcase_33 | AC | 307 ms
113,408 KB |
testcase_34 | AC | 319 ms
112,948 KB |
testcase_35 | AC | 327 ms
113,180 KB |
testcase_36 | AC | 243 ms
111,312 KB |
testcase_37 | AC | 241 ms
111,324 KB |
testcase_38 | AC | 241 ms
112,208 KB |
testcase_39 | AC | 173 ms
95,868 KB |
testcase_40 | AC | 195 ms
91,124 KB |
testcase_41 | AC | 93 ms
79,652 KB |
testcase_42 | AC | 267 ms
107,860 KB |
testcase_43 | AC | 41 ms
55,492 KB |
ソースコード
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)