16940번: BFS 스페셜 저지
올바른 순서는 1, 2, 3, 4와 1, 3, 2, 4가 있다.
www.acmicpc.net
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> a [100000];
int parent[100000];
int order[100000];
bool check[100000];
int main() {
int n;
cin >> n;
for (int i=0; i<n-1; i++) {
int u, v;
cin >> u >> v;
u -= 1; v -= 1;
a[u].push_back(v);
a[v].push_back(u);
}
for (int i=0; i<n; i++) {
cin >> order[i];
order[i] -= 1;
}
queue<int> q;
q.push(0);
check[0] = true;
int m = 1;
for (int i=0; i<n; i++) {
if (q.empty()) {
cout << 0 << '\n';
return 0;
}
int x = q.front(); q.pop();
if (x != order[i]) {
cout << 0 << '\n';
return 0;
}
int cnt = 0;
for (int y : a[x]) {
if (check[y] == false) {
parent[y] = x;
cnt += 1;
}
}
for (int j=0; j<cnt; j++) {
if (m+j >= n || parent[order[m+j]] != x) {
cout << 0 << '\n';
return 0;
}
q.push(order[m+j]);
check[order[m+j]] = true;
}
m += cnt;
}
cout << 1 << '\n';
return 0;
}
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> a[100000];
bool check[100000];
int main() {
int n;
cin >> n;
for (int i=0; i<n-1; i++) {
int u, v;
cin >> u >> v;
u -= 1; v -= 1;
a[u].push_back(v);
a[v].push_back(u);
}
vector<int> b(n);
vector<int> order(n);
for (int i=0; i<n; i++) {
cin >> b[i];
b[i] -= 1;
order[b[i]] = i;
}
for (int i=0; i<n; i++) {
sort(a[i].begin(), a[i].end(), [&](const int &u, const int &v) {
return order[u] < order[v];
});
}
vector<int> bfs_order;
queue<int> q;
q.push(0);
check[0] = true;
while (!q.empty()) {
int x = q.front(); q.pop();
bfs_order.push_back(x);
for (int y : a[x]) {
if (check[y] == false) {
check[y] = true;
q.push(y);
}
}
}
if (bfs_order == b) {
cout << 1 << '\n';
} else {
cout << 0 << '\n';
}
return 0;
}
'Problem set' 카테고리의 다른 글
[백준] 2146 다리 만들기 (0) | 2021.02.20 |
---|---|
[백준] 16964 DFS 스페셜 저지 (0) | 2021.02.20 |
[백준] 7562 나이트의 이동 (0) | 2021.02.20 |
[백준] 7576 토마토 (0) | 2021.02.20 |
[백준] 2178 미로 탐색 (0) | 2021.02.20 |