結果

問題 No.2598 Kadomatsu on Tree
ユーザー hiro1729hiro1729
提出日時 2024-01-01 09:47:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,929 ms / 2,000 ms
コード長 790 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 321,344 KB
最終ジャッジ日時 2024-02-26 04:07:34
合計ジャッジ時間 43,467 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 37 ms
53,460 KB
testcase_03 AC 37 ms
53,460 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 44 ms
53,460 KB
testcase_07 AC 39 ms
53,460 KB
testcase_08 AC 37 ms
53,460 KB
testcase_09 AC 37 ms
53,460 KB
testcase_10 AC 37 ms
53,460 KB
testcase_11 AC 37 ms
53,460 KB
testcase_12 AC 37 ms
53,460 KB
testcase_13 AC 179 ms
78,736 KB
testcase_14 AC 165 ms
78,736 KB
testcase_15 AC 178 ms
78,736 KB
testcase_16 AC 194 ms
78,992 KB
testcase_17 AC 148 ms
77,596 KB
testcase_18 AC 170 ms
78,732 KB
testcase_19 AC 173 ms
78,736 KB
testcase_20 AC 155 ms
78,096 KB
testcase_21 AC 155 ms
78,096 KB
testcase_22 AC 168 ms
78,736 KB
testcase_23 AC 881 ms
131,484 KB
testcase_24 AC 877 ms
124,840 KB
testcase_25 AC 865 ms
124,716 KB
testcase_26 AC 886 ms
131,480 KB
testcase_27 AC 964 ms
131,740 KB
testcase_28 AC 811 ms
124,712 KB
testcase_29 AC 844 ms
124,976 KB
testcase_30 AC 1,009 ms
131,480 KB
testcase_31 AC 920 ms
124,844 KB
testcase_32 AC 944 ms
131,872 KB
testcase_33 AC 887 ms
128,700 KB
testcase_34 AC 1,090 ms
131,344 KB
testcase_35 AC 909 ms
129,888 KB
testcase_36 AC 902 ms
128,156 KB
testcase_37 AC 821 ms
128,432 KB
testcase_38 AC 925 ms
129,936 KB
testcase_39 AC 873 ms
129,556 KB
testcase_40 AC 889 ms
129,924 KB
testcase_41 AC 1,085 ms
129,968 KB
testcase_42 AC 897 ms
129,840 KB
testcase_43 AC 908 ms
129,956 KB
testcase_44 AC 917 ms
130,020 KB
testcase_45 AC 975 ms
129,968 KB
testcase_46 AC 1,023 ms
130,048 KB
testcase_47 AC 927 ms
129,816 KB
testcase_48 AC 1,929 ms
305,296 KB
testcase_49 AC 319 ms
96,332 KB
testcase_50 AC 1,197 ms
255,244 KB
testcase_51 AC 491 ms
125,272 KB
testcase_52 AC 672 ms
151,364 KB
testcase_53 AC 314 ms
101,508 KB
testcase_54 AC 275 ms
95,904 KB
testcase_55 AC 173 ms
82,904 KB
testcase_56 AC 416 ms
117,316 KB
testcase_57 AC 224 ms
89,604 KB
testcase_58 AC 800 ms
131,872 KB
testcase_59 AC 623 ms
124,464 KB
testcase_60 AC 1,819 ms
298,036 KB
testcase_61 AC 1,839 ms
304,376 KB
testcase_62 AC 1,779 ms
321,344 KB
権限があれば一括ダウンロードができます

ソースコード

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 % 998244353)
0