File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """
2+ dp[현재노드] = min(dp[자식노드][얼리어답터인 경우;1], dp[자식노드][아닌 경우;0])
3+ """
4+
15import sys
26from collections import *
7+ sys .setrecursionlimit (10 ** 6 )
38
49input = sys .stdin .readline
510
813for _ in range (n - 1 ):
914 u , v = map (int , input ().split ())
1015 graph [u ].append (v )
16+ graph [v ].append (u )
1117
18+ dp = [[0 , 0 ] for _ in range (n + 1 )] # [노드번호][얼리어답터 체크]
19+ visited = [0 for _ in range (n + 1 )]
20+ def dfs (start ):
21+ visited [start ] = 1
22+ if len (graph [start ]) == 0 :
23+ dp [start ][1 ] = 1
24+ dp [start ][0 ] = 0
25+ else :
26+ for i in graph [start ]:
27+ if not visited [i ]:
28+ dfs (i )
29+ dp [start ][1 ] += min (dp [i ][0 ], dp [i ][1 ])
30+ dp [start ][0 ] += dp [i ][1 ]
31+ dp [start ][1 ] += 1
1232
13- # def parent(x):
14- # x_root = set()
15- # while x in graph[x]:
16- # x_root.add(x)
17- # x = graph[x]
18- # x_root.add(x)
19-
20-
21- # parent(2)
33+ dfs (1 )
34+ print (min (dp [1 ][0 ], dp [1 ][1 ]))
You can’t perform that action at this time.
0 commit comments