From c2d037d85ae49d43ef7eed0defd111654b7c838d Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Thu, 5 Nov 2020 10:17:13 +0800 Subject: [PATCH] P5318 [COCI2019] Quiz R41281754 --- problem/P5318/P5318.cpp | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 problem/P5318/P5318.cpp diff --git a/problem/P5318/P5318.cpp b/problem/P5318/P5318.cpp new file mode 100644 index 00000000..4456a5ab --- /dev/null +++ b/problem/P5318/P5318.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +struct edge { + int u, v; +}; + +vector e[100001]; +vector s; +bool vis_dfs[100001] = {0}, vis_bfs[100001] = {0}; +vector ans_dfs, ans_bfs; + +bool cmp(edge x, edge y) { + if (x.v == y.v) { + return x.u < y.u; + } + return x.v < y.v; +} + +void dfs(int x) { + vis_dfs[x] = 1; + ans_dfs.push_back(x); + for (int i = 0; i < e[x].size(); i++) { + int point = s[e[x][i]].v; + if (!vis_dfs[point]) { + dfs(point); + } + } +} + +void bfs(int x) { + int front, point; + queue q; + vis_bfs[x] = true; + q.push(x); + ans_bfs.push_back(x); + while (!q.empty()) { + front = q.front(); + for (int i = 0; i < e[front].size(); i++) { + point = s[e[front][i]].v; + if (!vis_bfs[point]) { + q.push(point); + ans_bfs.push_back(point); + vis_bfs[point] = true; + } + } + q.pop(); + } +} + +int main() { + memset(vis_dfs, 0x00, sizeof(vis_dfs)); + memset(vis_bfs, 0x00, sizeof(vis_bfs)); + int n, m, uu, vv; + cin >> n >> m; + for (int i = 0; i < m; i++) { + cin >> uu >> vv; + s.push_back({uu, vv}); + } + sort(s.begin(), s.end(), cmp); + for (int i = 0; i < m; i++) { + e[s[i].u].push_back(i); + } + dfs(1); + for (int i = 0; i < ans_dfs.size(); i++) { + cout << ans_dfs[i] << ' '; + } + cout << endl; + bfs(1); + for (int i = 0; i < ans_bfs.size(); i++) { + cout << ans_bfs[i] << ' '; + } + cout << endl; + return 0; +}