C. Mail Stamps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city «A» to city «B», they stamp it with «A B», or «B A». Unfortunately, often it is impossible to send a letter directly from the city of the sender to the city of the receiver, that's why the letter is sent via some intermediate cities. Post officers never send a letter in such a way that the route of this letter contains some city more than once. Bob is sure that the post officers stamp the letters accurately.
There are n stamps on the envelope of Bob's letter. He understands that the possible routes of this letter are only two. But the stamps are numerous, and Bob can't determine himself none of these routes. That's why he asks you to help him. Find one of the possible routes of the letter.
Input
The first line contains integer n (1 ≤ n ≤ 105) — amount of mail stamps on the envelope. Then there follow n lines with two integers each — description of the stamps. Each stamp is described with indexes of the cities between which a letter is sent. The indexes of cities are integers from 1 to 109. Indexes of all the cities are different. Every time the letter is sent from one city to another, exactly one stamp is put on the envelope. It is guaranteed that the given stamps correspond to some valid route from some city to some other city.
Output
Output n + 1 numbers — indexes of cities in one of the two possible routes of the letter.
Examples
input
Copy
2
1 100
100 2
output
Copy
2 100 1
input
Copy
3
3 1
100 2
3 2
output
Copy
100 2 3 1
给出点和点之间的到达关系,求一个路径覆盖所有的点。
由于点很大,离散化它到1-n标号。
然后对于度数为1的点,从此开始dfs,直接dfs遍历就行,不用回溯。
#include <algorithm> //STL通用算法
#include <bitset> //STL位集容器
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque> //STL双端队列容器
#include <exception> //异常处理类
#include <fstream>
#include <functional> //STL定义运算函数(代替运算符)
#include <limits>
#include <list> //STL线性列表容器
#include <map> //STL 映射容器
#include <iomanip>
#include <ios> //基本输入/输出支持
#include<iosfwd> //输入/输出系统使用的前置声明
#include <iostream>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <queue> //STL队列容器
#include <set> //STL 集合容器
#include <sstream> //基于字符串的流
#include <stack> //STL堆栈容器
#include <string> //字符串类
#include <vector> //STL动态数组容器
#define ll long long
using namespace std;
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 100000+66;
const int maxm=400000+66;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const int INF=99999999;
map<int,int>mmp;
int arr[maxn];
int cnt;
struct Edge
{
int u, v;
int next;
Edge() {}
Edge(int u, int v, int next): u(u), v(v), next(next) {}
};
struct Graph
{
Edge edge[maxm];
int head[maxn], etop;
void init(int nn)
{
etop = 0;
rep(i,0,nn+6)
{
head[i]=-1;
}
}
void Add_Edge(int u, int v)
{
edge[++etop] = Edge(u, v, head[u]);
head[u] = etop;
edge[++etop] = Edge(v, u, head[v]);
head[v] = etop;
}
} G;
bool vis[maxn];
int n;
int du[maxn];
vector<int>g[maxn];
void dfs(int u,int num)
{
if(num==cnt-1)
{
printf("%d",arr[u]);
}
for(int i = G.head[u]; i != -1; i = G.edge[i].next)
{
int v=G.edge[i].v;
if(!vis[v])
{
vis[v]=1;
dfs(v,num+1);
printf(" %d ",arr[u]);
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
mmp.clear();
int u,v;
cnt=1;
G.init(n);
rep(i,1,n)
{
g[i].clear();
du[i]=0;
}
rep(i,1,n)
{
vis[i]=0;
scanf("%d %d",&u,&v);
if(!mmp[u])
{
mmp[u]=cnt;
arr[cnt]=u;
cnt++;
}
if(!mmp[v])
{
mmp[v]=cnt;
arr[cnt]=v;
cnt++;
}
du[mmp[u]]++;
du[mmp[v]]++;
G.Add_Edge(mmp[u],mmp[v]);
G.Add_Edge(mmp[v],mmp[u]);
}
rep(i,1,cnt)
{
if(du[i]==1)
{
//cout<<"---";
vis[i]=1;
dfs(i,1);
printf("\n");
break;
}
}
}
}