結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー mkawa2mkawa2
提出日時 2023-10-03 10:03:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 558 ms / 2,000 ms
コード長 1,390 bytes
コンパイル時間 3,764 ms
コンパイル使用メモリ 86,716 KB
実行使用メモリ 156,356 KB
最終ジャッジ日時 2023-10-03 10:04:14
合計ジャッジ時間 14,750 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
71,532 KB
testcase_01 AC 69 ms
71,160 KB
testcase_02 AC 67 ms
71,296 KB
testcase_03 AC 68 ms
71,068 KB
testcase_04 AC 117 ms
78,340 KB
testcase_05 AC 116 ms
78,896 KB
testcase_06 AC 111 ms
78,932 KB
testcase_07 AC 106 ms
78,476 KB
testcase_08 AC 119 ms
80,944 KB
testcase_09 AC 113 ms
79,612 KB
testcase_10 AC 112 ms
78,976 KB
testcase_11 AC 108 ms
78,624 KB
testcase_12 AC 107 ms
78,544 KB
testcase_13 AC 96 ms
77,576 KB
testcase_14 AC 441 ms
117,536 KB
testcase_15 AC 400 ms
114,504 KB
testcase_16 AC 375 ms
114,252 KB
testcase_17 AC 277 ms
107,764 KB
testcase_18 AC 475 ms
119,024 KB
testcase_19 AC 521 ms
130,948 KB
testcase_20 AC 558 ms
130,940 KB
testcase_21 AC 527 ms
130,212 KB
testcase_22 AC 553 ms
135,876 KB
testcase_23 AC 502 ms
135,772 KB
testcase_24 AC 540 ms
135,708 KB
testcase_25 AC 528 ms
135,704 KB
testcase_26 AC 509 ms
135,880 KB
testcase_27 AC 530 ms
135,744 KB
testcase_28 AC 515 ms
135,644 KB
testcase_29 AC 252 ms
133,116 KB
testcase_30 AC 267 ms
122,380 KB
testcase_31 AC 275 ms
156,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# 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 << 63)
md = 10**9+7
# md = 998244353

def max_index(aa):
    mi, v = 0, aa[0]
    for i, a in enumerate(aa[1:], 1):
        if a > v: v, mi = a, i
    return mi

n=II()
to=[[] for _ in range(n)]
for _ in range(n-1):
    u,v=LI1()
    to[u].append(v)
    to[v].append(u)

def dfs(root=0):
    uu, stack = [], [root]
    while stack:
        u = stack.pop()
        uu.append(u)
        for v in to[u]:
            if v == parent[u]: continue
            parent[v] = u
            depth[v] = depth[u]+1
            stack.append(v)
    return uu

parent, depth = [-1]*n, [0]*n
dfs()
u=max_index(depth)
parent, depth = [-1]*n, [0]*n
dfs(u)
print(2*(n-1)-max(depth))
0