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.

Examples
input
2
1 100
100 2
output
2 100 1 
input
3
3 1
100 2
3 2
output
100 2 3 1 

题意:

  给你n张邮票

  每张邮票上面写有两个城市a,b

  有可能是a到b

  也有可能是b到a

现在问你 一条可行的路线

题解:

  很明显的拓扑

  每次选择度小于等于1 的点加入答案就好了

  注意数据范围

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<vector>
#include<map>
#include<queue>
using namespace std;
const int N = 1e5+, M = , mod = , inf = 0x3f3f3f3f;
typedef long long ll; int n,a[N],b[N],vis[N],cnt = ;
vector<int > G[N];
map<int,int > mp,hash,fhash;
int main() {
scanf("%d",&n);
for(int i=;i<=n;i++) {
scanf("%d%d",&a[i],&b[i]);
if(!hash.count(a[i])) hash[a[i]] = cnt ,fhash[cnt++] = a[i];
if(!hash.count(b[i])) hash[b[i]] = cnt, fhash[cnt++] = b[i];
a[i] = hash[a[i]];
b[i] = hash[b[i]];
}
for(int i=;i<=n;i++) {
int aa = a[i], bb = b[i];
G[aa].push_back(bb);
G[bb].push_back(aa);
mp.count(aa)?mp[aa]++:mp[aa]=;
mp.count(bb)?mp[bb]++:mp[bb]=;
}
int u = ;
for(map<int,int >::iterator it=mp.begin();it!=mp.end();it++) {
int now = it->first;
if(mp[now]==) u = now;
}
queue<int> q;
vector<int >ans;
q.push(u);
// cout<<fhash[u]<<endl;
memset(vis,,sizeof(vis));
while(!q.empty()) {
int k = q.front();
q.pop();
vis[k] = ;
ans.push_back(k);
for(int i=;i<G[k].size();i++) {
//if(vis[G[k][i]]) continue;
mp[G[k][i]] --;
if(mp[G[k][i]]<=&&!vis[G[k][i]]) q.push(G[k][i]);
}
}
for(int i=;i<ans.size();i++) {
//cout<<ans[i]<<endl;
printf("%d ",fhash[ans[i]]) ;
}
return ;
}

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 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...

  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. JDK1.7源码阅读tools包之------ArrayList,LinkedList,HashMap,TreeMap

    1.HashMap 特点:基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Has ...

  2. 模拟试题C

    模拟试题C 一.单项选择题(2′*14 =28′) 1.双线性法向插值法(Phong Shading)的优点是( ) A)法向计算精确 B)高光域准确 C)对光源和视点没有限制 D)速度较快 2.用编 ...

  3. Java中数组遍历

    就是将数组中的每个元素分别获取出来,就是遍历.遍历也是数组操作中的基石. 数组的索引是 0 到 lenght-1 ,可以作为循环的条件出现 public class ArrayDemo4 { publ ...

  4. Chernobyl’ Eagle on a Roof(鹰蛋坚固度)

    链接 Chernobyl’ Eagle on a Roof 题意 引用论文题意:有一堆共 M 个鹰蛋,一位教授想研究这些鹰蛋的坚硬度 E.他是通过不断从一幢 N 层的楼上向下扔鹰蛋来确定 E 的.当鹰 ...

  5. 关于MySQL日期操作函数 date_formate 的使用

    基本语法:DATE_FORMAT(date,format)说明:date 参数是合法的日期.format 规定日期/时间的输出格式.可以用的格式主要有格式 描述%a 缩写星期名%b 缩写月名%c 月, ...

  6. bzoj 1296: [SCOI2009]粉刷匠 动态规划

    Description windy有 N 条木板需要被粉刷. 每条木板被分为 M 个格子. 每个格子要被刷成红色或蓝色. windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色. 每个 ...

  7. Python数据结构1-----基本数据结构和collections系列

    1.基本数据结构 整型.浮点型.字符串.元祖.列表.字典.集合 2.高级数据结构 (collections模块) (1)计数器(counter):对字典的补充,用于追踪值的出现次数. [具备字典所有的 ...

  8. 自己对WEBGL坐标系的转换过程的理解【如图】

  9. Tab 切换效果

    今天写的两个小效果都是为了测试我写的单参函数,结果还是有点成功的~~此处是不是想发表情包! Tab效果很简单,这里我就不赘述了,直接上代码: html代码: <div class="c ...

  10. 2019-03-28 SQL inner left full

    在使用 join 时,on 和 where 条件的区别如下: 1. on 条件是在生成临时表时使用的条件,它不管 on 中的条件是否为真,都会返回左边表中的记录. 2.where 条件是在临时表生成好 ...