結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー aaaaaaaaaa2230
提出日時 2021-04-22 16:52:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 590 ms / 2,000 ms
コード長 1,150 bytes
コンパイル時間 1,056 ms
コンパイル使用メモリ 82,288 KB
実行使用メモリ 144,468 KB
最終ジャッジ日時 2024-07-04 06:27:20
合計ジャッジ時間 11,863 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

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