結果
問題 | No.1718 Random Squirrel |
ユーザー | mkawa2 |
提出日時 | 2023-12-26 22:08:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 395 ms / 2,000 ms |
コード長 | 2,534 bytes |
コンパイル時間 | 453 ms |
コンパイル使用メモリ | 82,356 KB |
実行使用メモリ | 113,676 KB |
最終ジャッジ日時 | 2024-09-27 15:17:26 |
合計ジャッジ時間 | 8,538 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
54,040 KB |
testcase_01 | AC | 40 ms
52,880 KB |
testcase_02 | AC | 39 ms
53,224 KB |
testcase_03 | AC | 40 ms
54,108 KB |
testcase_04 | AC | 43 ms
54,324 KB |
testcase_05 | AC | 40 ms
53,012 KB |
testcase_06 | AC | 40 ms
53,032 KB |
testcase_07 | AC | 40 ms
53,080 KB |
testcase_08 | AC | 41 ms
54,348 KB |
testcase_09 | AC | 39 ms
52,944 KB |
testcase_10 | AC | 39 ms
53,552 KB |
testcase_11 | AC | 285 ms
97,412 KB |
testcase_12 | AC | 143 ms
80,808 KB |
testcase_13 | AC | 211 ms
89,708 KB |
testcase_14 | AC | 291 ms
96,676 KB |
testcase_15 | AC | 269 ms
94,904 KB |
testcase_16 | AC | 183 ms
86,344 KB |
testcase_17 | AC | 258 ms
95,576 KB |
testcase_18 | AC | 339 ms
110,184 KB |
testcase_19 | AC | 277 ms
93,776 KB |
testcase_20 | AC | 166 ms
83,952 KB |
testcase_21 | AC | 361 ms
104,752 KB |
testcase_22 | AC | 395 ms
113,656 KB |
testcase_23 | AC | 392 ms
104,756 KB |
testcase_24 | AC | 360 ms
104,868 KB |
testcase_25 | AC | 383 ms
113,676 KB |
testcase_26 | AC | 206 ms
107,452 KB |
testcase_27 | AC | 209 ms
108,780 KB |
testcase_28 | AC | 199 ms
107,460 KB |
testcase_29 | AC | 206 ms
107,800 KB |
testcase_30 | AC | 216 ms
108,196 KB |
testcase_31 | AC | 204 ms
107,956 KB |
testcase_32 | AC | 204 ms
107,616 KB |
ソースコード
import sys # sys.setrecursionlimit(1000005) # sys.set_int_max_str_digits(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = -1-(-1 << 31) inf = -1-(-1 << 62) # md = 10**9+7 md = 998244353 def rerooting(root=0): stack = [root] while stack: u = stack.pop() uu.append(u) vv = [] # !!!check the format of the "to"!!! for v in to[u]: if v == par[u]: continue vv.append(v) par[v] = u stack.append(v) rdp[v] = dp[u] to[u] = vv # bottom up for u in uu[::-1]: p = par[u] if p != -1: trans_up(u) dp[p] = op(dp[p], dp[u]) # top down for u in uu: cs = e() # !!!check the format of the "to"!!! for v in to[u]: rdp[v] = op(rdp[v], cs) cs = op(cs, dp[v]) cs = rdp[u] for v in to[u][::-1]: rdp[v] = op(rdp[v], cs) trans_down(v) cs = op(cs, dp[v]) dp[u] = cs def op(a, b): return max(a,b) # u to parent[u] def trans_up(u): if dp[u]>-1:dp[u]+=1 return # parent[u] to u def trans_down(u): if rdp[u]>-1:rdp[u]+=1 return n,k=LI() to=[[] for _ in range(n)] for _ in range(n-1): u,v=LI1() to[u].append(v) to[v].append(u) dd=LI1() e = lambda: -1 par = [-1]*n # dp初期化 dp = [e() for _ in range(n)] rdp = [e() for _ in range(n)] for d in dd:dp[d]=rdp[d]=0 uu = [] rerooting(dd[0]) # print(uu) # print(dp) near=[-1]*n for d in dd:near[d]=d for u in uu[:0:-1]: if near[u]==u: p=par[u] near[p]=p dist=[0]*n for d in dd:near[d]=d size=n for u in uu: if near[u]==-1: size-=1 p = par[u] dist[u]=dist[p]+1 near[u]=near[p] ans=[(size-1)*2]*n for u in range(n): if near[u]==u:ans[u]-=dp[u] for u in range(n): if near[u]==u:continue v=near[u] ans[u]=ans[v]+dist[u] print(*ans,sep="\n")