Skip to content

Commit 7466142

Browse files
Merge pull request #731 from baekhangyeol/main
[백한결] 117차 라이브 코테 제출
2 parents 18637e4 + fbed407 commit 7466142

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
3+
def main():
4+
input = sys.stdin.readline
5+
6+
N = int(input())
7+
towers = list(map(int, input().split()))
8+
stack = []
9+
result = []
10+
11+
for i in range(len(towers)):
12+
while stack and towers[stack[-1]] < towers[i]:
13+
stack.pop()
14+
15+
if stack:
16+
result.append(stack[-1] + 1)
17+
else:
18+
result.append(0)
19+
20+
stack.append(i)
21+
22+
print(*result)
23+
24+
if __name__ == '__main__':
25+
main()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
from collections import defaultdict
3+
sys.setrecursionlimit(10**6)
4+
5+
6+
def main():
7+
input = sys.stdin.readline
8+
9+
N = int(input())
10+
graph = defaultdict(list)
11+
12+
for _ in range(N-1):
13+
a, b = map(int, input().split())
14+
graph[a].append(b)
15+
graph[b].append(a)
16+
17+
dp = [[0, 0] for _ in range(N+1)]
18+
visited = [False] * (N+1)
19+
20+
def dfs(node):
21+
visited[node] = True
22+
dp[node][0] = 0
23+
dp[node][1] = 1
24+
25+
for child in graph[node]:
26+
if not visited[child]:
27+
dfs(child)
28+
dp[node][0] += dp[child][1]
29+
dp[node][1] += min(dp[child][0], dp[child][1])
30+
31+
dfs(1)
32+
print(min(dp[1][0], dp[1][1]))
33+
34+
if __name__ == '__main__':
35+
main()

0 commit comments

Comments
 (0)