P2731 騎馬修柵欄
當個板子咯。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
/**
考慮多邊,以及字典序最小,multiset可以自帶排序,而且刪除元素方便,
這裏注意,std :: multiset :: erase (這裏接受是個迭代器)
*/
multiset<int> e[maxn];
/// 模擬的棧,相對於stack快點
int st[maxn];
int tot;
void dfs(int x) {
// 這裏注意每次都要 it = e[x].beign(),下,還以爲可以自己到下一個--
for (auto it = e[x].begin(); it != e[x].end();it = e[x].begin()) {
int to = *it;
e[x].erase(it);
e[to].erase(e[to].find(x)); // 這裏注意,迭代器
dfs(to);
}
st[++tot] = x;
}
int main(){
int n; cin >> n;
for (int i = 0; i < n; i++) {
int x, y; cin >> x >> y;
e[x].insert(y);
e[y].insert(x);
}
bool flg = 0;
for (int i = 0; i <= maxn; i++) {
if (e[i].size() & 1) {
dfs(i);
flg = 1;
break;
}
}
/// 考慮沒有奇點的情況
if (!flg) {
dfs(1);
}
for (int i = tot; i >= 1; i--) {
cout << st[i] << endl;
}
return 0;
}