codeforces C. Mail Stamps

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.

大致题意:给你n条路要你连成一条路

结题思路:

这边由于顶点的编号太大要经过离散化的处理,处理后这边用两个map映射来一一对应变化的值。然后接下来就是直接打印欧拉道路了

AC代码:

 

# include <stdio.h>
# include <iostream>
# include <string>
# include <algorithm>
# include <ctype.h>
# include <string.h>
# include <map>
# include <queue>
# include <limits.h>
# include <vector>
using namespace std;
struct node{//输入数据
	int u;
	int v;
};
map<int, int> m1;//离散化后的对应关系
map<int, int> m2;//与m1相反
node s[400010];
int degree[400010];//存储各点的度数
int a[400020];
vector<int> g[400010];//存储图
int path[400010];//存储最后的路径
int c=0;
void euler_path(int u){//打印欧拉道路
	for(int i=0; i<g[u].size(); i++){
		if(degree[g[u][i]]){
			path[c++]=m2[g[u][i]];
			degree[g[u][i]]--;
			degree[u]--;
			euler_path(g[u][i]);
		}
	}
}
int main(){ 
	int n, u, v, i, j, k, cnt=0, b;
	scanf("%d", &n);
	for(i=1; i<=n; i++){
		scanf("%d%d", &u, &v);
		s[i].u=u;s[i].v=v;
		a[cnt++]=u;
		a[cnt++]=v;
	}
	sort(a, a+2*n);
	int Size=unique(a, a+2*n)-a;
	for(i=0; i<Size; i++){//离散化操作
		int num=lower_bound(a, a+Size, a[i])-a+1;
		m1[a[i]]=num;
		m2[num]=a[i];
	}
	for(i=1; i<=n; i++){
		u=s[i].u;v=s[i].v;
		g[m1[u]].push_back(m1[v]);
		g[m1[v]].push_back(m1[u]);
		degree[m1[u]]++;degree[m1[v]]++;
	}
	int flage=0;
	for(i=1; i<=100009; i++){//寻找度数为奇数的点为起点
		if(degree[i]%2==1){
			b=i;
			flage=1;
			break;
		}
	}
	if(!flage){//所有的点度数均为偶数,随便找个起点
		for(i=1; i<=100009; i++){
			if(degree[i]){
				b=i;
			}
		}
	}
	path[c++]=m2[b];
	euler_path(b);
	for(i=0; i<c; i++){//打印path数组
		if(i!=c-1){
			printf("%d ", path[i]);
		}
		else{
			printf("%d", path[i]);
		}
	}
	return 0;
}
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。Gym是Codeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值