알고리즘/BaekJoon
[c++] 1260번 : DFS와 BFS
__jhp_+
2022. 5. 31. 14:26
https://www.acmicpc.net/problem/1260
1260번: DFS와 BFS
첫째 줄에 정점의 개수 N(1 ≤ N ≤ 1,000), 간선의 개수 M(1 ≤ M ≤ 10,000), 탐색을 시작할 정점의 번호 V가 주어진다. 다음 M개의 줄에는 간선이 연결하는 두 정점의 번호가 주어진다. 어떤 두 정점 사
www.acmicpc.net
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
#define MAX_VERTEX 1000
void DFS(bool[][MAX_VERTEX + 1], int, int);
void BFS(bool[][MAX_VERTEX + 1], int, int);
void print(vector<int>&);
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
bool edges[MAX_VERTEX + 1][MAX_VERTEX + 1] = { false, };
int N = 0, M = 0, V = 0;
cin >> N >> M >> V;
int from = 0, to = 0;
for (int i = 0; i < M; ++i)
{
cin >> from >> to;
edges[from][to] = true;
edges[to][from] = true;
}
DFS(edges, N, V);
BFS(edges, N, V);
return 0;
}
void DFS(bool edges[][MAX_VERTEX + 1], int vCnt, int start)
{
bool vertexs[MAX_VERTEX + 1] = { false, };
stack<int> st;
vector<int> v;
v.reserve(vCnt);
vertexs[start] = true;
st.push(start);
v.push_back(start);
while (st.empty() == false)
{
int cur = st.top();
st.pop();
for (int i = 1; i <= vCnt; ++i)
{
if (vertexs[i] == false && edges[cur][i] == true)
{
vertexs[i] = true;
st.push(cur);
st.push(i);
v.push_back(i);
break;
}
}
}
print(v);
return;
}
void BFS(bool edges[][MAX_VERTEX + 1], int vCnt, int start)
{
bool vertexs[MAX_VERTEX + 1] = { false, };
queue<int> q;
vector<int> v;
v.reserve(vCnt);
vertexs[start] = true;
q.push(start);
v.push_back(start);
while (q.empty() == false)
{
int cur = q.front();
q.pop();
for (int i = 1; i <= vCnt; ++i)
{
if (vertexs[i] == false && edges[cur][i] == true)
{
vertexs[i] = true;
q.push(i);
v.push_back(i);
}
}
}
print(v);
return;
}
void print(vector<int>& v)
{
for (size_t i = 0; i < v.size(); ++i)
{
cout << v[i] << ' ';
}
cout << '\n';
return;
}