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
- 유니온 파인드
- 구현
- 백트래킹
- VR
- 자료구조
- 다이나믹 프로그래밍
- 재귀
- 유니티
- 스택
- 백준
- 우선순위 큐
- ue5
- XR Interaction Toolkit
- 수학
- 정렬
- 다익스트라
- 문자열
- DFS
- 누적 합
- 투 포인터
- 그래프
- Team Fortress 2
- 트리
- 시뮬레이션
- Unreal Engine 5
- 브루트포스
- c++
- 그리디 알고리즘
- 알고리즘
- BFS
Archives
- Today
- Total
1일1알
백준 24445번 알고리즘 수업 - 너비 우선 탐색 2 C++ 본문
https://www.acmicpc.net/problem/24445
24445번: 알고리즘 수업 - 너비 우선 탐색 2
첫째 줄에 정점의 수 N (5 ≤ N ≤ 100,000), 간선의 수 M (1 ≤ M ≤ 200,000), 시작 정점 R (1 ≤ R ≤ N)이 주어진다. 다음 M개 줄에 간선 정보 u v가 주어지며 정점 u와 정점 v의 가중치 1인 양
www.acmicpc.net
bfs, 내림차순으로 방문
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n, m, r;
int cnt = 1;
vector<int> ans;
vector<bool> found;
vector<vector<int>> board;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> r;
ans = vector<int>(n + 1, 0);
found = vector<bool>(n + 1, false);
board = vector<vector<int>>(n + 1, vector<int>());
for (int i = 0; i < m; i++) {
int s, e;
cin >> s >> e;
board[s].push_back(e);
board[e].push_back(s);
}
for (int i = 1; i <= n; i++) {
sort(board[i].begin(), board[i].end(), greater<>());
}
queue<int> q;
q.push(r);
found[r] = true;
while (!q.empty()) {
int curr = q.front();
ans[curr] = cnt++;
q.pop();
for (auto next : board[curr]) {
if (found[next]) continue;
q.push(next);
found[next] = true;
}
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << "\n";
}
}
'알고리즘' 카테고리의 다른 글
백준 13703번 물벼룩의 생존확률 C++ (0) | 2023.02.02 |
---|---|
백준 11758번 CCW C++ (0) | 2023.02.01 |
백준 17952번 과제는 끝나지 않아! C++ (0) | 2023.01.30 |
백준 1240번 노드사이의 거리 C++ (0) | 2023.01.29 |
백준 13414번 수강신청 C++ (0) | 2023.01.28 |