Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 조세퍼스 순열
- 버킷 정렬
- heap
- 백준 5397
- 백준 1406
- 해시 구현
- qorwns
- 게리멘더링2
- 스택의 특징
- 백준 17779
- ㅣ풀이
- c#
- 백준 1158
- 백준 17822
- Stack 이란
- dfs
- 해시구현
- 원판 돌리기
- 별 찍기 10
- 자료구조
- 백준 17471
- C/C++ 구현
- 시간 복잡도
- AVL 시간 복잡도
- 백준
- 백준 2447
- 5397
- 1764
- 풀이
- 구현
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 1260] dfs와 bfs 본문
https://www.acmicpc.net/problem/1260
1260번: DFS와 BFS
첫째 줄에 정점의 개수 N(1 ≤ N ≤ 1,000), 간선의 개수 M(1 ≤ M ≤ 10,000), 탐색을 시작할 정점의 번호 V가 주어진다. 다음 M개의 줄에는 간선이 연결하는 두 정점의 번호가 주어진다. 어떤 두 정점 사이에 여러 개의 간선이 있을 수 있다. 입력으로 주어지는 간선은 양방향이다.
www.acmicpc.net
요구사항
1. dfs로 방문 순서대로 출력
2. bfs로 방문 순서대로 출력
풀이
1. dfs로 노드 방문
2. bfs로 노드 방문
문제는 양방향 그래프이다..
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;
int map[1001][1001];
int visit[10001];
int n, m, s;
struct go {
int x;
int y;
};
void bfs() {
memset(visit, 0, sizeof(visit));
queue<int> q;
q.push(s);
visit[s] = 1;
while (!q.empty()) {
int now = q.front();
printf("%d ", now);
q.pop();
for (int i = 1; i <= n; i++) {
if (map[now][i] == 1) {
if (visit[i] == 0) {
visit[i] = 1;
q.push(i);
}
}
}
}
}
void dfs(int now) {
printf("%d ", now);
for (int i = 1; i <= n; i++) {
if (map[now][i] == 1) {
if (visit[i] == 0) {
visit[i] = 1;
dfs(i);
}
}
}
}
int main() {
cin >> n >> m >> s;
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
map[x][y] = 1;
map[y][x] = 1;
}
memset(visit, 0, sizeof(visit));
visit[s] = 1;
dfs(s);
printf("\n");
bfs();
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 14502] 연구소 (0) | 2020.01.11 |
---|---|
[백준 11403] 경로 찾기 (0) | 2020.01.11 |
[백준 1944] 복제 로봇 (0) | 2020.01.11 |
[백준 1194] 달이 차오른다, 가자 (0) | 2020.01.09 |
[백준 1916] 최소비용 구하기 (0) | 2020.01.09 |
Comments