結果

問題 No.3206 う し た ウ ニ 木 あ く ん 笑
ユーザー timi
提出日時 2025-07-18 22:48:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,094 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 82,640 KB
実行使用メモリ 148,856 KB
最終ジャッジ日時 2025-07-18 22:48:48
合計ジャッジ時間 10,673 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
D=[[] for i in range(N)]
for i in range(N-1):
  a,b=map(int, input().split())
  a-=1;b-=1
  D[a].append(b)
  D[b].append(a)
  
from collections import deque
d=deque()
V=[-1]*N
d.append(0);V[0]=0
while d:
  now=d.popleft()
  for nex in D[now]:
    if V[nex]==-1:
      V[nex]=V[now]+1
      d.append(nex)

x=V.index(max(V))
V=[-1]*N
d.append(x);V[x]=0
P=[-1]*N
while d:
  now=d.popleft()
  for nex in D[now]:
    if V[nex]==-1:
      V[nex]=V[now]+1
      d.append(nex)
      P[nex]=now
ans=1
y=V.index(max(V))
E=[]
E.append(y)
while True:
  if E[-1]==x:
    break 
  E.append(P[E[-1]])

EE=set(E)
for i in range(len(E)):
  l,r=i,len(E)-i-1
  now=E[i]
  d=deque()
  F={}
  for nex in D[now]:
    if nex in EE:
      continue
    d.append((nex,now,nex,1))
    F[nex]=1
  while d:
    now,p,c,cnt=d.popleft()
    for nex in D[now]:
      if p==nex:
        continue 
      d.append((nex,now,c,cnt+1))
      F[c]=max(F[c],cnt+1)
  G=[]
  G.append(l)
  G.append(r)
  for i in F:
    G.append(F[i])
  G=sorted(G)[::-1]
  for i in range(len(G)):
    ans=max(ans,1+G[i]*(i+1))
print(ans)
0