ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/11724
package main
import (
"bufio"
"fmt"
"os"
)
var (
graph [][]int
visited []bool
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n, m int
fmt.Fscanln(reader, &n, &m)
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 < m; i++ {
var u, v int
fmt.Fscanln(reader, &u, &v)
graph[u][v] = 1
graph[v][u] = 1
}
var numOfConnected int
for i := 0; i < n+1; i++ {
if !visited[i] {
numOfConnected++
dfs(i)
}
}
fmt.Fprintln(writer, numOfConnected-1)
}
func dfs(start int) {
visited[start] = true
for i := 0; i < len(graph); i++ {
if !visited[i] && graph[start][i] == 1 {
dfs(i)
}
}
}
๐จ Python3
# https://www.acmicpc.net/problem/11724
import sys
sys.setrecursionlimit(10000)
graph = []
visited = []
def dfs(start):
global graph
global visited
visited[start] = True
for i in range(len(graph)):
if not visited[i] and graph[start][i] == 1:
dfs(i)
if __name__ == "__main__":
n, m = list(map(int, sys.stdin.readline().split()))
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(m):
u, v = list(map(int, sys.stdin.readline().split()))
graph[u][v] = 1
graph[v][u] = 1
num_of_connected = 0
for i in range(n+1):
if not visited[i]:
num_of_connected += 1
dfs(i)
print(num_of_connected-1)
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 2468๋ฒ / ์์ ์์ญ [Go][Python3] (0) | 2020.12.16 |
---|---|
BOJ / 2667๋ฒ / ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ [Go][Python3] (0) | 2020.12.15 |
BOJ / 1012๋ฒ / ์ ๊ธฐ๋ ๋ฐฐ์ถ [Go][Python3] (0) | 2020.12.13 |
BOJ / 4963๋ฒ / ์ฌ์ ๊ฐ์ [Go][Python3] (0) | 2020.12.12 |
BOJ / 2644๋ฒ / ์ด์๊ณ์ฐ [Go][Python3] (0) | 2020.12.10 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- MongoDB
- ์๊ณ ๋ฆฌ์ฆ
- Algorithm
- dp
- ๋ถํ ์ ๋ณต
- AWS
- ์ด๋ถํ์
- ์๋ฐ
- ํ
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ํด์๋งต
- baekjoon
- ๋ธ๋ฃจํธํฌ์ค
- ์คํ
- ๋งฅ๋ถ
- go
- ๋งฅ๋ถํ๋ก
- ํ๋ก์ด๋์์ฌ
- Macbook pro 2012 mid 13
- ballet
- Golang
- dfs
- python3
- java
- ๋ชฝ๊ณ ๋๋น
- BFS
- ๋ฐฑ์ค
- ์๊ฐ๊ต์ฒด
- BOJ
- ๋ฐ๋
- Total
- Today
- Yesterday