結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー ygd.ygd.
提出日時 2021-04-22 20:51:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,542 ms / 2,000 ms
コード長 784 bytes
コンパイル時間 961 ms
コンパイル使用メモリ 10,720 KB
実行使用メモリ 126,164 KB
最終ジャッジ日時 2023-09-17 10:20:09
合計ジャッジ時間 27,548 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,800 KB
testcase_01 AC 17 ms
7,744 KB
testcase_02 AC 17 ms
7,828 KB
testcase_03 AC 16 ms
7,836 KB
testcase_04 AC 35 ms
9,236 KB
testcase_05 AC 102 ms
12,620 KB
testcase_06 AC 110 ms
12,880 KB
testcase_07 AC 65 ms
10,984 KB
testcase_08 AC 129 ms
13,960 KB
testcase_09 AC 117 ms
13,120 KB
testcase_10 AC 107 ms
12,840 KB
testcase_11 AC 93 ms
12,196 KB
testcase_12 AC 82 ms
11,792 KB
testcase_13 AC 23 ms
8,496 KB
testcase_14 AC 1,194 ms
54,124 KB
testcase_15 AC 1,120 ms
51,928 KB
testcase_16 AC 1,042 ms
48,928 KB
testcase_17 AC 769 ms
38,924 KB
testcase_18 AC 1,286 ms
58,484 KB
testcase_19 AC 1,526 ms
66,540 KB
testcase_20 AC 1,530 ms
66,632 KB
testcase_21 AC 1,509 ms
66,664 KB
testcase_22 AC 1,542 ms
66,632 KB
testcase_23 AC 1,521 ms
66,680 KB
testcase_24 AC 1,498 ms
66,720 KB
testcase_25 AC 1,491 ms
66,716 KB
testcase_26 AC 1,517 ms
66,640 KB
testcase_27 AC 1,505 ms
66,696 KB
testcase_28 AC 1,517 ms
66,588 KB
testcase_29 AC 1,384 ms
126,164 KB
testcase_30 AC 1,506 ms
90,260 KB
testcase_31 AC 919 ms
61,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(500000)
N = int(input())
G = [[] for _ in range(N)]
for i in range(N-1):
  a,b = map(int,input().split())
  a-=1;b-=1
  G[a].append((1,b))
  G[b].append((1,a))

def dfs(G,u,par): #頂点uから子に向かって最も遠い頂点とコストのペアを返す。
  ret = [0,u] #[cost,頂点]が返す値。
  for cost,v in G[u]:
    if v == par:
      continue
    nxt = dfs(G,v,u)
    nxt[0] += cost
    ret = max(ret,nxt) #大きいほうを返す。
  return ret

p = dfs(G,0,-1) #始点0からスタートして最も遠い頂点p[1]を求める。
q = dfs(G,p[1],-1) #p[1]は直径の端点なので、そこから最も遠い頂点q[1]を求める。
dia = q[0]#p[1]->q[1]間のコストq[0]が木の直径。
ans = dia + (N-1-dia)*2
print(ans) 
0