- 그래프에서 한 정점으로부터 시작하여 차례대로 모든 정점들을 한 번씩 방문하는 알고리즘
임의의 노드에서 시작해서 다음 가지로 넘어가기 전에 해당 가지를 완벽하게 탐색하는 방법
vector<int> graph[maxn];
bool visited[maxn];
void dfs(int x){
if(visited[x]) return;
visited[x] = true;
for(int i: graph[x]){
dfs(i);
}
}
임의의 노드에서부터 인접한 노드를 먼저 탐색하는 방법
vector<int> graph[maxn];
bool visited[maxn];
void bfs(int start){
queue<int> q;
q.push(start);
while(!q.empty()){
int x = q.front();
q.pop();
for(int i : graph[x])
if(!visited[i]){
q.push(i);
visited[i]=true;
}
}
}