96pts求卡常
查看原帖
96pts求卡常
700558
williamwei楼主2024/10/17 14:00
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
using namespace std;
const int maxn = 5000 + 10;
int n, m, U, V, tot, cur[maxn], res[maxn];
vector<int> e[maxn];
bitset<maxn> vis;
void dfs(int u, int pa) {
    cout << u << ' ';
    for (int v : e[u]) if (v != pa) dfs(v, u);
}
void dfs2(int u) {
    if (vis[u]) return; vis[u] = true; cur[++tot] = u; 
    for (int v : e[u]) if (!((u == U && v == V) 
        || (u == V && v == U))) dfs2(v);
}
int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        e[u].push_back(v); e[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) sort(e[i].begin(), e[i].end());
    if (n == m) {
        for (U = 1; U <= n; U++)
            for (int v : e[U]) {
                V = v; vis.reset(); tot = 0; dfs2(1); 
                if (tot < n) continue;
                for (int i = 1; i <= n; i++) {
                    if (!res[i] || cur[i] < res[i]) {
                        for (int j = 1; j <= n; j++) res[j] = cur[j];
                        break;
                    }
                    if (cur[i] > res[i]) break;
                }
            }
        for (int i = 1; i <= n; i++) cout << res[i] << ' ';
    } else dfs(1, 0);
    return 0;
}
2024/10/17 14:00
加载中...