Skip to content

Commit

Permalink
Merge pull request #48 from AlgoLeadMe/15-pu2rile
Browse files Browse the repository at this point in the history
15-pu2rile
  • Loading branch information
pu2rile authored Sep 4, 2024
2 parents 6663b60 + bc726e9 commit 595b53f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pu2rile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
| 10์ฐจ์‹œ | 2024.07.11 | ์Šคํƒ | [ํ™”ํ•™์‹๋Ÿ‰](https://www.acmicpc.net/problem/2257) | [#35](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/35#issue-2403173169)
| 11์ฐจ์‹œ | 2024.07.13 | ์šฐ์„ ์ˆœ์œ„ ํ | [๊ฐ•์˜์‹ค](https://www.acmicpc.net/problem/1374) | [#37](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/37#issue-2406937336)
| 12์ฐจ์‹œ | 2024.07.23 | ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ | [1ํ•™๋…„](https://www.acmicpc.net/problem/5557) | [#40](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/40)
| 13์ฐจ์‹œ | 2024.07.26 | ์Šคํƒ | [ํ›„์œ„ ํ‘œ๊ธฐ์‹](https://www.acmicpc.net/problem/1918) | [#43](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/43)
| 13์ฐจ์‹œ | 2024.07.26 | ์Šคํƒ | [ํ›„์œ„ ํ‘œ๊ธฐ์‹](https://www.acmicpc.net/problem/1918) | [#43](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/43)
| 14์ฐจ์‹œ | 2024.08.05 | ํŠธ๋ฆฌ | [์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ](https://www.acmicpc.net/problem/5639) | [#45](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/45)
| 15์ฐจ์‹œ | 2024.08.15 | ํŠธ๋ฆฌ | [๋‚˜๋ฌด ํƒˆ์ถœ](https://www.acmicpc.net/problem/15900) | [#48](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/48)
30 changes: 30 additions & 0 deletions pu2rile/ํŠธ๋ฆฌ/๋‚˜๋ฌด ํƒˆ์ถœ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys
sys.setrecursionlimit(10 ** 6)

n = int(sys.stdin.readline().strip())
# ํŠธ๋ฆฌ์— ์ €์žฅ
tree = [[] for _ in range (n+1)]
for _ in range(n-1):
a,b = map(int, sys.stdin.readline().strip().split())
tree[a].append(b)
tree[b].append(a)

arr = [0] * (n + 1)
def dfs(cur, prv, dep):
arr[cur] = dep # cnt๋Š” ํ˜„์žฌ ๋…ธ๋“œ(cur)๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ arr ๋ฆฌ์ŠคํŠธ์˜ cur ์ธ๋ฑ์Šค์— ์ €์žฅ
for next in tree[cur]: # ํ˜„์žฌ ๋…ธ๋“œ(cur)์— ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ๋ฅผ ์ˆœํšŒ
if next == prv: # ๋‹ค์Œ ๋…ธ๋“œ(next)๊ฐ€ ์ด์ „์— ๋ฐฉ๋ฌธํ•œ ๋ถ€๋ชจ ๋…ธ๋“œ(prv)๋ผ๋ฉด
continue # ๊ฑด๋„ˆ๋›ฐ๊ธฐ
dfs(next,cur,dep+1) # ์žฌ๊ท€์ ์œผ๋กœ dfs ์ˆ˜ํ–‰, cur์ด next ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๊ฐ€ ๋˜๊ณ , cnt + 1์„ ํ†ตํ•ด ํ˜„์žฌ ๊นŠ์ด๋ฅผ 1 ์ฆ๊ฐ€

dfs(1, 0, 0) # ํŠธ๋ฆฌ ํƒ์ƒ‰ ์‹œ์ž‘
cnt = 0
for i in range(2, n+1):
# ๋ฆฌํ”„ ๋…ธ๋“œ์˜ ๊นŠ์ด ๋”ํ•˜๊ธฐ
if len(tree[i]) == 1:
cnt += arr[i]

if cnt % 2 == 1:
print("Yes")
else:
print("No")

0 comments on commit 595b53f

Please sign in to comment.