ν°μ€ν 리 λ·°
π©π»π» λ¬Έμ
βπ» νμ΄
π¨ Go
// https://www.acmicpc.net/problem/2606
package main
import (
"bufio"
"fmt"
"os"
)
var (
graph [][]int
visited []bool
writer *bufio.Writer
count int
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer = bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n, edge int
fmt.Fscanln(reader, &n)
fmt.Fscanln(reader, &edge)
graph = make([][]int, n+1)
for i := 0; i < n+1; i++ {
graph[i] = make([]int, n+1)
}
visited = make([]bool, n+1)
for i := 0; i < edge; i++ {
var computer1, computer2 int
fmt.Fscanln(reader, &computer1, &computer2)
graph[computer1][computer2] = 1
graph[computer2][computer1] = 1
}
dfs(1)
fmt.Fprintln(writer, count-1)
}
func dfs(start int) {
visited[start] = true
count++
for i := 0; i < len(graph[start]); i++ {
if graph[start][i] == 1 && !visited[i] {
dfs(i)
}
}
}
π¨ Python3
# https://www.acmicpc.net/problem/2606
import sys
count = 0
def dfs(start):
visited[start] = True
global count
count += 1
for i in range(len(graph[start])):
if graph[start][i] == 1 and not visited[i]:
dfs(i)
if __name__ == "__main__":
n = int(sys.stdin.readline())
edge = int(sys.stdin.readline())
graph = [[0 for i in range(n+1)] for j in range(n+1)]
visited = [False for i in range(n+1)]
for i in range(edge):
computer1, computer2 = list(map(int, sys.stdin.readline().split()))
graph[computer1][computer2] = 1
graph[computer2][computer1] = 1
dfs(1)
print(count-1)
728x90
'dev > algorithm' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
BOJ / 1934λ² / μ΅μ곡배μ [Go][Python3] (0) | 2020.12.08 |
---|---|
BOJ / 7785λ² / νμ¬μ μλ μ¬λ [Go][Python3] (0) | 2020.12.07 |
BOJ / 1260λ² / DFSμ BFS [Go][Python3] (0) | 2020.12.05 |
BOJ / 7596λ² / MP3 Songs [Go][Python3] (0) | 2020.12.04 |
BOJ / 2204λ² / λλΉμ λλ μ¦ ν μ€νΈ [Go][Python3] (0) | 2020.12.03 |
λκΈ
κΈ λ³΄κ΄ν¨
μ΅κ·Όμ λ¬λ¦° λκΈ
TAG
- dp
- baekjoon
- μ€ν
- ν΄μ맡
- λΈλ£¨νΈν¬μ€
- λ§₯λΆ
- μ΄λΆνμ
- Golang
- λ§₯λΆνλ‘
- Algorithm
- Macbook pro 2012 mid 13
- go
- λ°±μ€
- AWS
- μλ°
- μκ³ λ¦¬μ¦
- λΆν μ 볡
- ν
- python3
- μκ°κ΅μ²΄
- νλ‘μ΄λμμ¬
- MongoDB
- java
- dfs
- λ°λ
- λͺ½κ³ λλΉ
- ballet
- λ§₯λΆ μ κ·Έλ μ΄λ
- BFS
- BOJ
- Total
- Today
- Yesterday