結果

問題 No.1582 Vertexes vs Edges
ユーザー shotoyooshotoyoo
提出日時 2021-07-02 21:54:10
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 1,009 bytes
コンパイル時間 392 ms
コンパイル使用メモリ 86,976 KB
実行使用メモリ 91,024 KB
最終ジャッジ日時 2023-09-11 21:46:30
合計ジャッジ時間 7,526 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,340 KB
testcase_01 AC 76 ms
71,320 KB
testcase_02 AC 74 ms
71,452 KB
testcase_03 AC 77 ms
71,216 KB
testcase_04 AC 104 ms
77,376 KB
testcase_05 AC 160 ms
88,664 KB
testcase_06 AC 106 ms
78,092 KB
testcase_07 AC 115 ms
79,364 KB
testcase_08 AC 131 ms
83,260 KB
testcase_09 AC 157 ms
88,312 KB
testcase_10 AC 133 ms
84,172 KB
testcase_11 AC 106 ms
78,276 KB
testcase_12 AC 122 ms
80,844 KB
testcase_13 AC 154 ms
84,436 KB
testcase_14 AC 149 ms
84,716 KB
testcase_15 AC 176 ms
87,784 KB
testcase_16 AC 143 ms
82,700 KB
testcase_17 AC 158 ms
84,528 KB
testcase_18 AC 179 ms
89,144 KB
testcase_19 AC 146 ms
84,512 KB
testcase_20 AC 153 ms
84,336 KB
testcase_21 AC 142 ms
83,568 KB
testcase_22 AC 174 ms
88,524 KB
testcase_23 AC 132 ms
80,348 KB
testcase_24 AC 115 ms
79,504 KB
testcase_25 AC 150 ms
83,292 KB
testcase_26 AC 132 ms
80,512 KB
testcase_27 AC 190 ms
87,484 KB
testcase_28 AC 125 ms
80,216 KB
testcase_29 AC 175 ms
85,080 KB
testcase_30 AC 150 ms
82,944 KB
testcase_31 AC 180 ms
85,140 KB
testcase_32 AC 126 ms
80,452 KB
testcase_33 AC 227 ms
90,696 KB
testcase_34 AC 228 ms
90,816 KB
testcase_35 AC 227 ms
91,024 KB
testcase_36 AC 76 ms
71,160 KB
testcase_37 AC 73 ms
71,392 KB
testcase_38 AC 75 ms
71,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))

### 木の読み込み yomikomi
n = int(input())
ns = [[] for _ in range(n)]
for _ in range(n-1):
    u,v = map(int, input().split())
    u -= 1
    v -= 1
    ns[u].append(v)
    ns[v].append(u)
    
d0 = [0]*n
d1 = [0]*n
# 深さ優先探索 (巻き戻しあり) dfs tree
q = [(0, -1)]
while q:
    u,prv = q.pop()
    if u<0:
        # 返るときの処理
        u = ~u
        v0 = 0
        v1 = 1
        for v in ns[u]:
            if v==prv:
                continue
            v0 += d1[v]
            v1 += min(d0[v], d1[v])
        d0[u] = v0
        d1[u] = v1
    else:
        q.append((~u,prv))
        for v in ns[u]:
            # 進むときの処理
            if v==prv:
                continue
            q.append((v,u))
ans = min(d0[0], d1[0])
print(ans)
0