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 | 29 | 30 |
Tags
- 컴퓨터공학과
- vector
- 북리뷰
- coding
- 코딩
- cs
- Computer science
- 그래프
- 컴공과
- 알고리즘
- Operating System
- 오에스
- 구현
- bfs
- 문제풀이
- 너비우선탐색
- 오퍼레이팅시스템
- 코테
- 정석
- OS
- c++
- 스택
- 개발
- 정석학술정보관
- DP
- 컴공
- 브루트포스
- Stack
- 자료구조
- 백준
Archives
- Today
- Total
Little Jay
[C++] 백준 16964번 - DFS 스페셜 저지 본문
전형적인 DFS문제이다.
그런데 이제 Sorting을 곁들인.....
순서대로 DFS를 수행해주면 되지만 인접리스트로 구현한 그래프를 정렬을 하고 DFS를 수행해줘야 한다.
문제해결기법 13주차 A번 문제와 매우 유사한 문제였던 것 같다.
#include <bits/stdc++.h>
#define endl '\n'
#define pii pair<int, int>
using namespace std;
int t;
vector<int> v[100001];
bool visited[100001];
int order[100001];
vector<int> dfs_order;
bool cmp(int& a, int& b) {
return order[a] < order[b];
}
void dfs(int start) {
if (visited[start]) return;
visited[start] = true;
dfs_order.push_back(start);
for (auto i : v[start]) if (!visited[i]) dfs(i);
}
void solve() {
int n; cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b; cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
vector<int> temp(n + 1);
for (int i = 1; i <= n; i++) {
cin >> temp[i];
order[temp[i]] = i;
}
for (int i = 1; i <= n; i++) {
sort(v[i].begin(), v[i].end(), cmp);
}
dfs_order.push_back(0);
if (temp[1] == 1) dfs(1);
if (dfs_order == temp) cout << 1 << endl;
else cout << 0 << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
t = 1; while (t--) solve();
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 1922번 - 네트워크 연결 (0) | 2022.12.21 |
---|---|
[C++] 백준 2407 - 조합 (0) | 2022.12.01 |
[C++] 백준 11660번 - 구간 합 구하기5 (0) | 2022.11.29 |
[C++] 백준 2670 - 연속부분최대곱(with setprecision) (0) | 2022.11.28 |
[C++] 백준 20291번 - 파일 정리 (0) | 2022.09.27 |
Comments