結果

問題 No.806 木を道に
ユーザー Coki628Coki628
提出日時 2020-05-11 18:18:21
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 320 ms / 2,000 ms
コード長 1,147 bytes
コンパイル時間 127 ms
コンパイル使用メモリ 11,344 KB
実行使用メモリ 32,428 KB
最終ジャッジ日時 2023-09-25 20:14:58
合計ジャッジ時間 5,165 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,672 KB
testcase_01 AC 16 ms
8,500 KB
testcase_02 AC 17 ms
8,552 KB
testcase_03 AC 17 ms
8,568 KB
testcase_04 AC 16 ms
8,536 KB
testcase_05 AC 17 ms
8,628 KB
testcase_06 AC 17 ms
8,576 KB
testcase_07 AC 16 ms
8,532 KB
testcase_08 AC 17 ms
8,444 KB
testcase_09 AC 16 ms
8,588 KB
testcase_10 AC 74 ms
12,156 KB
testcase_11 AC 63 ms
11,816 KB
testcase_12 AC 270 ms
22,660 KB
testcase_13 AC 185 ms
18,600 KB
testcase_14 AC 243 ms
21,828 KB
testcase_15 AC 265 ms
22,660 KB
testcase_16 AC 95 ms
13,712 KB
testcase_17 AC 229 ms
20,916 KB
testcase_18 AC 35 ms
9,856 KB
testcase_19 AC 76 ms
12,132 KB
testcase_20 AC 219 ms
20,916 KB
testcase_21 AC 123 ms
15,564 KB
testcase_22 AC 304 ms
25,016 KB
testcase_23 AC 320 ms
25,056 KB
testcase_24 AC 126 ms
17,480 KB
testcase_25 AC 179 ms
21,768 KB
testcase_26 AC 83 ms
15,788 KB
testcase_27 AC 241 ms
32,428 KB
testcase_28 AC 23 ms
9,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
EPS = 10 ** -10

def dfs(nodes, src):
    """ DFS(木、スタック、重みなし) """

    stack = [(src, -1)]
    ans = 0
    while stack:
        u, prev = stack.pop()
        ans += max(len(nodes[u]) - 2, 0)
        for v in nodes[u]:
            if v != prev:
                stack.append((v, u))
    return ans

N = INT()
nodes = [[] for i in range(N)]
for i in range(N-1):
    a, b = MAP()
    a -= 1; b -= 1
    nodes[a].append(b)
    nodes[b].append(a)

ans = dfs(nodes, 0)
print(ans)
0