Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

时间:2021-12-11 10:27:04

C. Mail Stamps

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/problemset/problem/29/C

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

2
1 100
100 2

Sample Output

2 100 1

HINT

题意

给你一条链,让你从头输出到尾

题解:

离散化一下,然后在跑一发拓扑排序就好了

代码

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 1000000007
#define eps 1e-9
int Num;
char CH[];
const int inf=0x3f3f3f3f;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} //************************************************************************************** int n;
vector<int> q;
map<int,int> H;
map<int,int> h;
int a[maxn];
int b[maxn];
vector<int> e[maxn];
int d[maxn];
int vis[maxn];
int main()
{
n=read();
for(int i=;i<n;i++)
{
a[i]=read();
b[i]=read();
q.push_back(a[i]);
q.push_back(b[i]);
}
sort(q.begin(),q.end());
q.erase(unique(q.begin(),q.end()),q.end());
for(int i=;i<q.size();i++)
H[q[i]]=i,h[i]=q[i];
for(int i=;i<n;i++)
{
e[H[a[i]]].push_back(H[b[i]]);
e[H[b[i]]].push_back(H[a[i]]);
d[H[a[i]]]++;
d[H[b[i]]]++;
}
int flag=;
queue<int> qq;
for(int i=;i<q.size();i++)
{
if(d[H[q[i]]]==)
{
qq.push(H[q[i]]);
vis[H[q[i]]]=;
break;
}
}
while(!qq.empty())
{
int v=qq.front();
printf("%d ",h[v]);
vis[v]=;
qq.pop();
for(int i=;i<e[v].size();i++)
{
if(vis[e[v][i]])
continue;
d[e[v][i]]--;
if(d[e[v][i]]<=)
qq.push(e[v][i]);
}
}
}

Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序的更多相关文章

  1. Codeforces Beta Round &num;29 &lpar;Div&period; 2&comma; Codeforces format&rpar;

    Codeforces Beta Round #29 (Div. 2, Codeforces format) http://codeforces.com/contest/29 A #include&lt ...

  2. Codeforces Beta Round &num;29 &lpar;Div&period; 2&comma; Codeforces format&rpar; C&period; Mail Stamps 拓扑排序

    C. Mail Stamps     One day Bob got a letter in an envelope. Bob knows that when Berland's post offic ...

  3. Codeforces Beta Round &num;32 &lpar;Div&period; 2&comma; Codeforces format&rpar;

    Codeforces Beta Round #32 (Div. 2, Codeforces format) http://codeforces.com/contest/32 A #include&lt ...

  4. Codeforces Beta Round &num;31 &lpar;Div&period; 2&comma; Codeforces format&rpar;

    Codeforces Beta Round #31 (Div. 2, Codeforces format) http://codeforces.com/contest/31 A #include&lt ...

  5. Codeforces Beta Round &num;80 &lpar;Div&period; 2 Only&rpar;【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  6. Codeforces Beta Round &num;83 &lpar;Div&period; 1 Only&rpar;题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  7. Codeforces Beta Round &num;79 &lpar;Div&period; 2 Only&rpar;

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  8. Codeforces Beta Round &num;77 &lpar;Div&period; 2 Only&rpar;

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round &num;76 &lpar;Div&period; 2 Only&rpar;

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

随机推荐

  1. Servlet-Cookie源码分析 源码环境:Tomcat8

    最近在学习servlet的一些实现细节,阅读了Cookie的源码. Cookie本质上是服务器发送给客户端(主要是浏览器)的一个会话临时数据. 其源码注释文档的说明: Creates a cookie ...

  2. MyEclipse:各种提示图标的含义

    1.新建的项目,项目名称前有黄色小警示号的原因, 2.项目名称上有红色叹号,说明缺少jar包,仔细找一下,看看缺了什么,添加进去一般就好了. 3.出现红色叉号,那说明代码错误,需要修改代码.

  3. python1

    leetcode上面的很简单的题目 Given an integer (signed 32 bits), write a function to check whether it is a power ...

  4. jquery中attr&lpar;&rpar;与prop&lpar;&rpar;区别

    我们知道jquery中获取元素属性有两种常见的方法,一个是attr()方法,这个是用的比较多的,也是我们第一个想到的.另外一个就是prop()方法了,这个方法之前很少用到,它是jquery1.6之后新 ...

  5. redis 列出所有的键

    > KEYS * (empty list or set)

  6. Failure &lbrack;INSTALL&lowbar;FAILED&lowbar;SHARED&lowbar;USER&lowbar;INCOMPATIBLE&rsqb;

    在Android studio中想要运行程序,点击运行后程序安装失败,报出如下异常: 原因:在manifest中设置了 .android:sharedUserId="android.uid. ...

  7. &lbrack;jQuery&rsqb;on和bind事件绑定的区别

    on和bind事件绑定的区别 一个demo展示 <!DOCTYPE html> <html lang="zh"> <head> <titl ...

  8. BloomFilter&lpar;布隆过滤器&rpar;

    原文链接:http://blog.csdn.net/qq_38646470/article/details/79431659 1.概念: 如果想判断一个元素是不是在一个集合里,一般想到的是将所有元素保 ...

  9. Shell从入门到精通进阶之三&colon;表达式与运算符

    3.1 条件表达式 表达式 示例 [ expression ] [ 1 -eq 1 ]  ` expression ` ` 1 -eq 1 ` test expression test 1 -eq 1 ...

  10. Maven项目强制更新,解决Failed to read artifact descriptor for xxx&period;jar问题

    导入的maven项目pom.xml现红叉 分析原因:在maven本地仓库中找不到相应的jar包. 解决方案:让maven强制更新依赖. 项目右击菜单,Maven -> Update Projec ...