codeforces 29C Mail Stamps(dfs)

 Mail Stamps
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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.

Sample Input

Input
2
1 100
100 2
Output
2 100 1 
Input
3
3 1
100 2
3 2
Output
100 2 3 1 

思路:因为这些数字大到可以达到10的九次方,但个数却不多,因此可以用map容器存储各个数字对应的位置,然后再用数组f[i]储存每个位置对应的数,用vector容器储存图的邻接表,方便访问。注意深搜时找到一条能够遍历到所有点的路径再输出,要用到回溯。

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
map<int,int>m;//起到去重作用,并且解决了数组无法开大的问题,m[a]=pos。储存每个数在数组中的位置 
vector<int>graph[100005];//相当于无向图的邻接表 
int f[100005];//第i个数的值 
bool vis[100005];
int n;
int dfs(int cur,int dep){
	if(dep==n+1){//如果这条路走得通,能够通过这条路访问到每一个数,就开始回溯输出 
		cout<<f[cur];
		return 1;
	}
	for(int i=0;i<graph[cur].size();i++){//尝试访问该点的每一个未被访问的邻接点 
		if(vis[graph[cur][i]]==0){
			vis[graph[cur][i]]=1;
			if(dfs(graph[cur][i],dep+1)){
			cout<<" "<<f[cur];
			return 1;
		   }
		}
		vis[graph[cur][i]]=0;//回溯 
	}
	
}
int main(){
	int i,j,a,b,pos;
	memset(vis,0,sizeof(vis));
	cin>>n;
	pos=0;
	for(i=0;i<n;i++){
		cin>>a>>b;
		if(m[a]==0){m[a]=++pos;f[pos]=a;}
		if(m[b]==0){m[b]=++pos;f[pos]=b;}
		graph[m[a]].push_back(m[b]);
		graph[m[b]].push_back(m[a]);
	}
	int start=1;
	for(i=1;i<=n+1;i++){
		if(graph[i].size()==1){
			start=i;
			break;
		}
	}//找到一个起点 
	vis[start]=1;
	dfs(start,1);//深搜 
	cout<<endl;
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值