| 12345678910111213141516171819202122232425262728293031323334 |
- #include <algorithm>
- #include <iostream>
- #include <vector>
- using namespace std;
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int n, m;
- cin >> n >> m;
- vector<vector<int>> adj(n + 1);
- for (int i = 0; i < m; ++i) {
- int u, v;
- cin >> u >> v;
- adj[u].push_back(v);
- adj[v].push_back(u); // 独立的反向弧,不与上一条共享 used 标记
- }
- vector<size_t> cursor(n + 1, 0);
- vector<int> st{1}, path;
- path.reserve(2 * m + 1);
- while (!st.empty()) {
- int v = st.back();
- if (cursor[v] == adj[v].size()) {
- path.push_back(v);
- st.pop_back();
- } else {
- st.push_back(adj[v][cursor[v]++]);
- }
- }
- reverse(path.begin(), path.end());
- for (int v : path) cout << v << '\n';
- }
|