結果

問題 No.1333 Squared Sum
ユーザー shotoyooshotoyoo
提出日時 2021-01-08 23:11:56
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,329 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 137,240 KB
最終ジャッジ日時 2024-04-28 05:06:57
合計ジャッジ時間 28,835 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
51,840 KB
testcase_01 AC 37 ms
52,352 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1,164 ms
136,320 KB
testcase_14 AC 1,226 ms
126,720 KB
testcase_15 AC 1,319 ms
134,116 KB
testcase_16 AC 38 ms
52,480 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 37 ms
51,968 KB
testcase_20 WA -
testcase_21 AC 37 ms
51,968 KB
testcase_22 WA -
testcase_23 AC 35 ms
51,968 KB
testcase_24 AC 36 ms
52,224 KB
testcase_25 AC 37 ms
52,480 KB
testcase_26 AC 1,212 ms
131,456 KB
testcase_27 AC 1,192 ms
125,056 KB
testcase_28 AC 1,193 ms
130,944 KB
testcase_29 AC 1,096 ms
131,200 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 322 ms
137,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
_print = lambda *x: print(*x, file=sys.stderr)

# グラフの読み込み
n = int(input())
ns = [[] for _ in range(n)]
M = 10**9+7
for _ in range(n-1):
    u,v,c = map(int, input().split())
    u -= 1
    v -= 1
    ns[u].append((c,v))
    ns[v].append((c,u))
# 深さ優先探索 (巻き戻しあり) dfs
q = [(0, -1)]
d = [0]*n
s = [0]*n
k = [0]*n
v1 = 0
v2 = 0
while q:
    u,prv = q.pop()
    if u<0:
        # 返るときの処理
        u = ~u
        ss = 0
        ss2 = 0
        kk = 0
        kk2 = 0
        for c,v in ns[u]:
            if v==prv:
                continue
            ss += s[v]
            ss2 += s[v]*s[v]
            kk += k[v]
            kk2 += k[v]**2
        v1 += ((ss**2 - ss2)//2 + ss) * d[u]**2
        v2 += ((kk**2 - kk2)//2 + kk+ss*d[u]) * d[u]
        v1 %= M
        v2 %= M
        s[u] = ss + 1
        k[u] = kk + d[u]
    else:
        q.append((~u,prv))
        for c,v in ns[u]:
            # 進むときの処理
            if v==prv:
                continue
            q.append((v,u))
            d[v] = d[u] + c
S = 0
S2 = 0
for v in d:
    S += v
    S2 += v*v
    S%=M
    S2%=M
ans = n*S2 + S*S - 2*S2 + 4*v1 - 4*v2
print(ans%M)
0