結果

問題 No.2598 Kadomatsu on Tree
ユーザー hiro1729hiro1729
提出日時 2024-01-01 09:47:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 778 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 321,344 KB
最終ジャッジ日時 2024-01-01 09:48:21
合計ジャッジ時間 40,634 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,332 KB
testcase_01 AC 34 ms
53,332 KB
testcase_02 AC 35 ms
53,332 KB
testcase_03 AC 34 ms
53,332 KB
testcase_04 AC 35 ms
53,332 KB
testcase_05 AC 35 ms
53,332 KB
testcase_06 AC 34 ms
53,332 KB
testcase_07 AC 35 ms
53,332 KB
testcase_08 AC 35 ms
53,332 KB
testcase_09 AC 34 ms
53,332 KB
testcase_10 AC 35 ms
53,332 KB
testcase_11 AC 34 ms
53,332 KB
testcase_12 AC 34 ms
53,332 KB
testcase_13 AC 166 ms
78,736 KB
testcase_14 AC 154 ms
78,736 KB
testcase_15 AC 164 ms
78,736 KB
testcase_16 AC 184 ms
78,864 KB
testcase_17 AC 142 ms
77,596 KB
testcase_18 AC 158 ms
78,732 KB
testcase_19 AC 161 ms
78,736 KB
testcase_20 AC 147 ms
78,096 KB
testcase_21 AC 149 ms
78,096 KB
testcase_22 AC 159 ms
78,736 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
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 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 AC 164 ms
82,904 KB
testcase_56 WA -
testcase_57 AC 209 ms
89,604 KB
testcase_58 AC 770 ms
131,872 KB
testcase_59 AC 537 ms
124,464 KB
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; sys.setrecursionlimit(2 * 10 ** 5)

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)
a = list(map(int, input().split()))


vis = [False] * N
ans = 0
def dfs(u):
	global ans
	d = []
	vis[u] = True
	res = 0
	s = 1
	j = -1
	l = []
	r = []
	for i in g[u]:
		if vis[i]:
			if a[i] > a[u]:
				j = len(l)
				l.append(0)
			if a[i] < a[u]:
				j = len(r) + N
				r.append(0)
		else:
			t = dfs(i)
			s += t
			if a[i] > a[u]:
				l.append(t)
			if a[i] < a[u]:
				r.append(t)
	if j != -1:
		if j < N:
			l[j] = N - s
		else:
			r[j - N] = N - s
	ans += (sum(l) ** 2 - sum(i ** 2 for i in l)) // 2 + (sum(r) ** 2 - sum(i ** 2 for i in r)) // 2
	return s

dfs(0)
print(ans)
0