結果

問題 No.763 Noelちゃんと木遊び
ユーザー NoneNone
提出日時 2021-04-14 03:39:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 312 ms / 2,000 ms
コード長 918 bytes
コンパイル時間 1,655 ms
コンパイル使用メモリ 86,548 KB
実行使用メモリ 113,484 KB
最終ジャッジ日時 2023-09-12 16:45:59
合計ジャッジ時間 8,186 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 206 ms
113,484 KB
testcase_01 AC 170 ms
86,140 KB
testcase_02 AC 259 ms
99,196 KB
testcase_03 AC 208 ms
92,808 KB
testcase_04 AC 178 ms
87,112 KB
testcase_05 AC 199 ms
92,888 KB
testcase_06 AC 300 ms
101,776 KB
testcase_07 AC 283 ms
102,972 KB
testcase_08 AC 219 ms
93,308 KB
testcase_09 AC 180 ms
86,976 KB
testcase_10 AC 146 ms
81,100 KB
testcase_11 AC 312 ms
99,408 KB
testcase_12 AC 284 ms
101,660 KB
testcase_13 AC 278 ms
101,440 KB
testcase_14 AC 255 ms
95,252 KB
testcase_15 AC 211 ms
92,604 KB
testcase_16 AC 133 ms
78,808 KB
testcase_17 AC 210 ms
92,652 KB
testcase_18 AC 304 ms
101,888 KB
testcase_19 AC 287 ms
102,084 KB
testcase_20 AC 286 ms
101,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def dfs(start=0,goal=None):
    parents={}
    p,t=start,0
    parents[p]=-2
    next_set=[(~p,t),(p,t)]
    if p==goal:
        return t
    while next_set:
        p,t=next_set.pop()
        if p>=0:
            for q in edges[p]:
                if q in parents:
                    continue
                if q==goal:
                    return t+1
                parents[q]=p
                next_set.append((~q,t+1))
                next_set.append((q,t+1))
        else: # 帰りがけ処理
            p=~p
            for q in edges[p]:
                dp[0][p]+=max(dp[1][q],dp[0][q])
                dp[1][p]+=dp[0][q]
            dp[1][p]+=1
    return -1

N=int(input())

edges=[[] for _ in range(N)]
for _ in range(N-1):
    a,b=map(int, input().split())
    a,b=a-1,b-1
    edges[a].append(b)
    edges[b].append(a)

dp=[[0]*N for _ in range(2)]

dfs(start=0,goal=None)

print(max(dp[0][0],dp[1][0]))
0