题目链接:Codeforces 29C Mail Stamps
题意:给你n条无向边,保证只存在两条路径可以遍历完所有的点。让你输出任意一条路径。
AC 代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5+100;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void getmax(int &a, int b) {a = max(a, b); }
void getmin(int &a, int b) {a = min(a, b); }
void add(LL &x, LL y) { x += y; x %= MOD; }
int u[MAXN], v[MAXN];
map<int, int> fp;
vector<int> G[MAXN];
bool vis[MAXN];
int rec[MAXN];
void DFS(int u)
{
for(int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if(vis[v]) continue;
cout << " " << rec[v];
vis[v] = true;
DFS(v);
}
}
int main()
{
int n; cin >> n; int top = 0;
fp.clear();
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &u[i], &v[i]);
rec[top++] = u[i];
rec[top++] = v[i];
fp[u[i]]++; fp[v[i]]++;
}
sort(rec, rec+top);
int T = unique(rec, rec+top) - rec;
//cout << T << endl;
for(int i = 0; i < T; i++) {
G[i].clear(); vis[i] = false;
}
for(int i = 1; i <= n; i++) {
int sx = lower_bound(rec, rec+T, u[i]) - rec;
int ex = lower_bound(rec, rec+T, v[i]) - rec;
G[sx].push_back(ex);
G[ex].push_back(sx);
}
int s;
for(int i = 0; i < T; i++) {
if(fp[rec[i]] == 1) {
s = i;
break;
}
}
cout << rec[s]; vis[s] = true; DFS(s);
return 0;
}