結果

問題 No.2638 Initial fare
ユーザー ああいいああいい
提出日時 2024-02-23 22:39:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 701 ms / 2,000 ms
コード長 680 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 154,272 KB
最終ジャッジ日時 2024-02-23 22:40:14
合計ジャッジ時間 13,955 ms
ジャッジサーバーID
(参考情報)
judge13 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 37 ms
53,460 KB
testcase_03 AC 38 ms
53,460 KB
testcase_04 AC 604 ms
115,080 KB
testcase_05 AC 671 ms
116,936 KB
testcase_06 AC 37 ms
53,460 KB
testcase_07 AC 631 ms
133,488 KB
testcase_08 AC 415 ms
138,596 KB
testcase_09 AC 566 ms
154,272 KB
testcase_10 AC 469 ms
154,216 KB
testcase_11 AC 37 ms
53,460 KB
testcase_12 AC 615 ms
130,012 KB
testcase_13 AC 414 ms
143,020 KB
testcase_14 AC 614 ms
152,792 KB
testcase_15 AC 480 ms
147,788 KB
testcase_16 AC 39 ms
53,460 KB
testcase_17 AC 593 ms
129,804 KB
testcase_18 AC 520 ms
133,880 KB
testcase_19 AC 661 ms
128,664 KB
testcase_20 AC 622 ms
128,144 KB
testcase_21 AC 667 ms
126,480 KB
testcase_22 AC 37 ms
53,460 KB
testcase_23 AC 633 ms
127,144 KB
testcase_24 AC 701 ms
128,072 KB
testcase_25 AC 38 ms
53,460 KB
testcase_26 AC 695 ms
124,912 KB
testcase_27 AC 686 ms
128,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())

G = [[] for _ in range(N)]
for _ in range(N- 1):
	u,v = map(int,input().split())
	u -= 1
	v -= 1
	G[u].append(v)
	G[v].append(u)
	
dp = [[0] * 2 for _ in range(N)]
stack = [(0,-1),(~0,-1)]
ans = 0
while stack:
	now,p = stack.pop()
	if now < 0:
		now = ~now
		for v in G[now]:
			if v != p:
				stack.append((v,now))
				stack.append((~v,now))
	else:
		t1 = 0
		tt = 0
		t2 = 0
		for v in G[now]:
			if v != p:
				tt += 1
				dp[now][0] += 1
				dp[now][1] += dp[v][0]
				t1 += dp[v][0]
				t2 += dp[v][1]
				
				ans += 1 + dp[v][0] + dp[v][1]
		tmp = 0
		for v in G[now]:
			if v != p:
				tmp += (t1 - dp[v][0])
		ans += tmp + (tt - 1) * tt // 2
print(ans)
0