From b6e2f3357bfe05b5c59986a8280b79927666855e Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 7 Jun 2022 16:54:08 +0800 Subject: [PATCH] =?UTF-8?q?P5022=20[NOIP2018=20=E6=8F=90=E9=AB=98=E7=BB=84?= =?UTF-8?q?]=20=E6=97=85=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/77062666 --- Luogu/P5022/P5022.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Luogu/P5022/P5022.cpp diff --git a/Luogu/P5022/P5022.cpp b/Luogu/P5022/P5022.cpp new file mode 100644 index 00000000..79993037 --- /dev/null +++ b/Luogu/P5022/P5022.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 5005; + +int n, m, s, t, in[N]; +bool vis[N]; +std::vector c, g[N]; +std::unordered_map map[N]; + +void cycle() { + std::queue q; + + for (int i = 1; i <= n; i++) { + if (in[i] == 1) q.push(i); + } + + while (!q.empty()) { + int u = q.front(); + q.pop(); + + for (int v : g[u]) { + if (in[v] > 1) { + if (--in[v] == 1) q.push(v); + } + } + } +} + +void dfs(int u, int fa, std::vector &res) { + if (vis[u]) return; + vis[u] = true; + + res.push_back(u); + + for (int v : g[u]) { + if (v == fa || u == s && v == t || v == s && u == t) continue; + + dfs(v, u, res); + } + + vis[u] = false; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1, u, v; i <= m; i++) { + cin >> u >> v; + + g[u].push_back(v); + g[v].push_back(u); + in[u]++, in[v]++; + + map[u][v] = map[v][u] = true; + } + + cycle(); + + for (int i = 1; i <= n; i++) { + std::sort(g[i].begin(), g[i].end()); + if (in[i] >= 2) c.push_back(i); + } + + std::vector ans; + + if (n == m) { + ans = std::vector(n, n); + + for (int i = 0; i < c.size(); i++) { + for (int j = i + 1; j < c.size(); j++) { + s = c[i], t = c[j]; + + if (!map[s][t]) continue; + + std::vector res; + + dfs(1, 0, res); + + ans = std::min(ans, res); + } + } + } else { + dfs(1, 0, ans); + } + + for (int &ans : ans) { + cout << ans << ' '; + } + cout << endl; + + return 0; +}