๐Ÿ˜Š ๋ฐฑ์ค€ 2606 : ๋ฐ”์ด๋Ÿฌ์Šค

3Juhwanยท2021๋…„ 2์›” 21์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
6/23

2606: ๋ฐ”์ด๋Ÿฌ์Šค

DFS/BFS ๋‘ ๋ฒˆ์งธ ๋ฌธ์ œ!


๐Ÿ“Œ Try 1

def dfs(graph, start_node, visit):
    visit.append(start_node)
    # print(start_node, end=' ')
    
    for node in graph[start_node]:
        if node not in visit:
            dfs(graph, node, visit)

graph = dict()
visit = list()

N = int(input())
nums = int(input())
for _ in range(nums):
    x, y = map(int, input().split())
    
    if x in graph:
        graph[x].append(y)
    else:
        graph[x] = [y]
    if y in graph:
        graph[y].append(x)
    else:
        graph[y] = [x]
        
dfs(graph, 1, visit)

print(len(visit)-1)

ํ•œ ๋ฒˆ์— ํ’€์—ˆ๋‹ค~! ๊ฐ„๋‹จํ•ด ๋ณด์—ฌ์„œ ์ด์ „์— ์ž‘์„ฑํ•œ dfs ์žฌ๊ท€ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ๋‹ค ์‚ฌ์šฉํ–ˆ๋‹ค.


๐ŸŽ Reference

profile
Codeforces์™€ USACO ํ’€์ด๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ๊ธ€๋„ ๊ณ„์† ์—…๋ฐ์ดํŠธ ๋ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€