結果

問題 No.1582 Vertexes vs Edges
ユーザー ansainansain
提出日時 2021-07-02 21:51:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 411 ms / 2,000 ms
コード長 1,912 bytes
コンパイル時間 727 ms
コンパイル使用メモリ 10,960 KB
実行使用メモリ 37,160 KB
最終ジャッジ日時 2023-09-11 21:41:19
合計ジャッジ時間 8,741 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,788 KB
testcase_01 AC 21 ms
8,792 KB
testcase_02 AC 20 ms
8,740 KB
testcase_03 AC 21 ms
8,756 KB
testcase_04 AC 30 ms
9,624 KB
testcase_05 AC 281 ms
33,952 KB
testcase_06 AC 32 ms
9,872 KB
testcase_07 AC 58 ms
12,240 KB
testcase_08 AC 145 ms
21,028 KB
testcase_09 AC 255 ms
32,348 KB
testcase_10 AC 170 ms
23,680 KB
testcase_11 AC 37 ms
10,384 KB
testcase_12 AC 118 ms
18,920 KB
testcase_13 AC 212 ms
24,568 KB
testcase_14 AC 226 ms
24,744 KB
testcase_15 AC 294 ms
30,716 KB
testcase_16 AC 151 ms
19,588 KB
testcase_17 AC 211 ms
24,008 KB
testcase_18 AC 344 ms
35,652 KB
testcase_19 AC 203 ms
25,496 KB
testcase_20 AC 205 ms
22,876 KB
testcase_21 AC 167 ms
21,716 KB
testcase_22 AC 290 ms
32,764 KB
testcase_23 AC 120 ms
17,336 KB
testcase_24 AC 65 ms
12,828 KB
testcase_25 AC 172 ms
20,972 KB
testcase_26 AC 128 ms
18,312 KB
testcase_27 AC 304 ms
30,208 KB
testcase_28 AC 98 ms
15,428 KB
testcase_29 AC 260 ms
26,664 KB
testcase_30 AC 163 ms
20,232 KB
testcase_31 AC 257 ms
27,388 KB
testcase_32 AC 110 ms
16,656 KB
testcase_33 AC 407 ms
37,160 KB
testcase_34 AC 408 ms
37,048 KB
testcase_35 AC 411 ms
37,044 KB
testcase_36 AC 21 ms
8,736 KB
testcase_37 AC 21 ms
8,740 KB
testcase_38 AC 21 ms
8,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict, Counter, deque
from itertools import permutations, combinations, product, combinations_with_replacement, groupby, accumulate
import operator
from math import sqrt, gcd, factorial
# from math import isqrt, prod,comb  # python3.8用(notpypy)
#from bisect import bisect_left,bisect_right
from functools import lru_cache, reduce
#from heapq import heappush,heappop,heapify,heappushpop,heapreplace
#import numpy as np
#import networkx as nx
#from networkx.utils import UnionFind
#from numba import njit, b1, i1, i4, i8, f8
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, NegativeCycleError
# numba例 @njit(i1(i4[:], i8[:, :]),cache=True) 引数i4配列、i8 2次元配列,戻り値i1
def input(): return sys.stdin.readline().rstrip()
def divceil(n, k): return 1+(n-1)//k  # n/kの切り上げを返す
def yn(hantei, yes='Yes', no='No'): print(yes if hantei else no)


def main():
    """
    グラフが隣接辺で与えられる場合
    """
    mod = 10**9+7
    mod2 = 998244353
    n = int(input())

    graph = [[] for _ in range(n)]

    for i in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        graph[a].append(b)
        graph[b].append(a)

    mother = [[] for _ in range(n)]
    dp = [1]*n #白1:
    # print(dp)
    visited = [False]*n
    visited[0] = True

    d = deque()
    d.append(0)


    while d:
        v = d[-1]
        if graph[v] == []:  # 帰りがけ
            if mother[v]:
                dp[v]= 0 if any(dp[m] for m in mother[v]) else 1
            d.pop()
        else:  # 行きがけ
            i = graph[v].pop()
            if visited[i]:
                continue
            visited[i] = True
            d.append(i)
            mother[v].append(i)
    print(n-sum(dp))



if __name__ == '__main__':
    main()
0