結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-04-22 16:52:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 729 ms / 2,000 ms
コード長 1,150 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 150,548 KB
最終ジャッジ日時 2023-09-17 10:11:08
合計ジャッジ時間 14,855 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,708 KB
testcase_01 AC 97 ms
71,916 KB
testcase_02 AC 95 ms
71,904 KB
testcase_03 AC 94 ms
71,716 KB
testcase_04 AC 149 ms
79,324 KB
testcase_05 AC 176 ms
80,532 KB
testcase_06 AC 177 ms
80,944 KB
testcase_07 AC 160 ms
79,928 KB
testcase_08 AC 177 ms
81,448 KB
testcase_09 AC 178 ms
81,132 KB
testcase_10 AC 174 ms
80,700 KB
testcase_11 AC 163 ms
80,544 KB
testcase_12 AC 162 ms
80,144 KB
testcase_13 AC 144 ms
78,720 KB
testcase_14 AC 570 ms
103,196 KB
testcase_15 AC 521 ms
102,020 KB
testcase_16 AC 484 ms
100,532 KB
testcase_17 AC 382 ms
95,224 KB
testcase_18 AC 580 ms
105,392 KB
testcase_19 AC 719 ms
111,544 KB
testcase_20 AC 674 ms
111,900 KB
testcase_21 AC 729 ms
111,532 KB
testcase_22 AC 705 ms
111,684 KB
testcase_23 AC 695 ms
111,588 KB
testcase_24 AC 708 ms
111,400 KB
testcase_25 AC 713 ms
111,580 KB
testcase_26 AC 704 ms
110,552 KB
testcase_27 AC 708 ms
111,452 KB
testcase_28 AC 684 ms
110,700 KB
testcase_29 AC 319 ms
117,512 KB
testcase_30 AC 342 ms
118,448 KB
testcase_31 AC 359 ms
150,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def bfs(x):
    dis = [n]*(n)
    dis[x] = 0
    q = deque([x])
    while q:
        now = q.popleft()
        for nex in e[now]:
            if dis[nex] > dis[now]+1:
                dis[nex] = dis[now]+1
                q.append(nex)
    return dis

def Euler_Tour(x):
    vis = [0]*n
    stack = [~x,x]
    ans = [0]*n
    vis[x] = 1
    count = -1
    while stack:
        now = stack.pop()
        count += 1
        if now >= 0:
            ans[now] = count
            for nex in e[now]:
                if dis[nex] == dis[now]-1 and vis[nex] == 0:
                    vis[nex] = 1
                    stack.append(~nex)
                    stack.append(nex)
            
            for nex in e[now]:
                if vis[nex] == 0:
                    vis[nex] = 1
                    stack.append(~nex)
                    stack.append(nex)
    return ans

n = int(input())
e = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append(b)
    e[b].append(a)

dis = bfs(0)
dis = bfs(dis.index(max(dis)))
print(max(Euler_Tour(dis.index(max(dis)))))
0