結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー ygd.ygd.
提出日時 2021-04-22 20:47:04
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 743 bytes
コンパイル時間 349 ms
コンパイル使用メモリ 86,844 KB
実行使用メモリ 116,348 KB
最終ジャッジ日時 2023-09-17 10:17:02
合計ジャッジ時間 14,937 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,392 KB
testcase_01 AC 72 ms
71,128 KB
testcase_02 AC 73 ms
71,308 KB
testcase_03 AC 73 ms
71,092 KB
testcase_04 AC 124 ms
78,224 KB
testcase_05 AC 149 ms
79,712 KB
testcase_06 AC 155 ms
80,580 KB
testcase_07 AC 131 ms
79,060 KB
testcase_08 AC 158 ms
80,640 KB
testcase_09 AC 152 ms
80,332 KB
testcase_10 AC 155 ms
80,288 KB
testcase_11 AC 141 ms
79,792 KB
testcase_12 AC 140 ms
79,796 KB
testcase_13 AC 114 ms
77,960 KB
testcase_14 AC 597 ms
103,376 KB
testcase_15 AC 568 ms
102,512 KB
testcase_16 AC 511 ms
100,304 KB
testcase_17 AC 396 ms
94,696 KB
testcase_18 AC 609 ms
105,772 KB
testcase_19 AC 683 ms
110,212 KB
testcase_20 AC 692 ms
110,212 KB
testcase_21 AC 711 ms
110,640 KB
testcase_22 AC 703 ms
110,320 KB
testcase_23 AC 714 ms
110,200 KB
testcase_24 AC 708 ms
110,296 KB
testcase_25 AC 719 ms
110,160 KB
testcase_26 AC 712 ms
110,232 KB
testcase_27 AC 709 ms
110,340 KB
testcase_28 AC 697 ms
110,328 KB
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 365 ms
116,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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