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 #29 (Div. 2, Codeforces format)

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

  2. Codeforces Beta Round #29 (Div. 2, Codeforces format) C. 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 #32 (Div. 2, Codeforces format)

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

  4. Codeforces Beta Round #31 (Div. 2, Codeforces format)

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

  5. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

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

  6. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

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

  7. Codeforces Beta Round #79 (Div. 2 Only)

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

  8. Codeforces Beta Round #77 (Div. 2 Only)

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

  9. Codeforces Beta Round #76 (Div. 2 Only)

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

随机推荐

  1. Java 设计模式学习总结(下)

    (八)模板方法 模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情况下,重新定义算法的某些步骤. templateMethod()会依次调用 ...

  2. Selenium启动本地firefox的profile

    ProfilesIni pi = new ProfilesIni();FirefoxProfile profile = pi.getProfile("default");WebDr ...

  3. Raspberry Pi3 ~ Eclipse中添加wiringPi 库函数

    这篇是在博客园原创 转载注明出处啊 以前用单片机.STM32之类的时候都是在一个集成的开发环境下进行的 比如Keil.IAR等 那么linux下编程,eclipse是个不错的选择 关于树莓派的GPIO ...

  4. C语言实现strcpy

    strcpy.h: #ifndef STRCPY_H #define STRCPY_H #include <stdio.h> char *cat_strcpy(char *dst, con ...

  5. 【2013微软面试题】输出节点数为n的二叉树的所有形态

    转自:http://blog.csdn.net/monsterxd/article/details/8449005 /* *  题意,求节点数为n的二叉树的所有形态,先要想个方式来唯一标示一棵二叉树 ...

  6. <javascript高级程序设计>笔记

    1.要讲一个值转换成其对应的Boolean类型 ,可以调用转型函数Boolean(). var message=“hello world!”; var messageAsBoolean=Boolean ...

  7. effective c++:virtual函数在构造函数和析构函数中的注意事项

    如不使用自动生成函数要明确拒绝 对于一个类,如果你没有声明,c++会自动生成一个构造函数,一个析构函数,一个copy构造函数和一个copy assignment操作符. class Empty { p ...

  8. AutoCompleteTextView使用 监听

    AutoCompleteTextView使用 An editable text view that shows completion suggestions automatically while t ...

  9. 《Java数据结构与算法》笔记-CH4-5不带计数字段的循环队列

    第四章涉及三种数据存储类型:栈,队列,优先级队列 1.概括:他们比数组和其他数据存储结构更为抽象,主要通过接口对栈,队列和优先级队列进行定义.这些 接口表明通过他们可以完成的操作,而他们的主要实现机制 ...

  10. SCAU 13校赛 17115 ooxx numbers

    17115 ooxx numbers 时间限制:1000MS  内存限制:65535K 题型: 编程题   语言: 无限制 Description a number A called oo numbe ...