您的位置:首页 > 大数据 > 人工智能

codeforces C. Mail Stamps

2016-09-16 21:31 357 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: