結果

問題 No.2638 Initial fare
ユーザー tassei903tassei903
提出日時 2024-02-19 21:39:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,208 ms / 2,000 ms
コード長 1,080 bytes
コンパイル時間 503 ms
コンパイル使用メモリ 82,532 KB
実行使用メモリ 182,460 KB
最終ジャッジ日時 2024-04-10 11:16:06
合計ジャッジ時間 20,018 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,064 KB
testcase_01 AC 44 ms
52,676 KB
testcase_02 AC 39 ms
53,128 KB
testcase_03 AC 46 ms
58,256 KB
testcase_04 AC 1,082 ms
144,452 KB
testcase_05 AC 1,119 ms
152,388 KB
testcase_06 AC 39 ms
52,944 KB
testcase_07 AC 885 ms
165,404 KB
testcase_08 AC 561 ms
171,388 KB
testcase_09 AC 642 ms
181,980 KB
testcase_10 AC 610 ms
182,460 KB
testcase_11 AC 40 ms
53,576 KB
testcase_12 AC 866 ms
158,952 KB
testcase_13 AC 564 ms
159,584 KB
testcase_14 AC 690 ms
178,336 KB
testcase_15 AC 638 ms
179,660 KB
testcase_16 AC 45 ms
59,776 KB
testcase_17 AC 837 ms
155,880 KB
testcase_18 AC 625 ms
158,436 KB
testcase_19 AC 1,016 ms
152,900 KB
testcase_20 AC 1,087 ms
154,308 KB
testcase_21 AC 1,066 ms
150,624 KB
testcase_22 AC 44 ms
58,964 KB
testcase_23 AC 1,130 ms
155,860 KB
testcase_24 AC 1,203 ms
150,576 KB
testcase_25 AC 45 ms
58,432 KB
testcase_26 AC 1,137 ms
153,992 KB
testcase_27 AC 1,208 ms
149,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes")
no = lambda :print("no");No = lambda :print("No")
#######################################################################
n = ni()
g = [[] for i in range(n)]
for i in range(n-1):
    a,b = na()
    g[a-1].append(b-1)
    g[b-1].append(a-1)

dp = [[1,0,0,0] for i in range(n)]
seen = [0]*n
q = [0]
et = []
seen[0] = 1
p = [-1] * n
chi = [[] for i in range(n)]
while q:
    x = q.pop()
    et.append(x)
    for y in g[x]:
        if seen[y]:
            continue
        seen[y] = 1
        p[y] = x
        chi[x].append(y)
        q.append(y)

for x in et[::-1]:
    for y in chi[x]:
        dp[x][1] += dp[y][0]
        dp[x][2] += dp[y][1]
        dp[x][3] += dp[y][2]

ans = 0
for i in range(n):
    x = i
    t = 3
    s = sum(dp[x])
    while p[x] != -1 and t:
        y = x
        x = p[x]
        s += sum(dp[x][:t]) - sum(dp[y][:t-1])
        t -= 1
    ans += s
    #print(s)
print((ans-n)//2)
0