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. nginx 限制ip/限制访问路径

    一.多站点统一限IP vim nginx.conf allow 127.0.0.1; deny all; # 以上代码解释: # deny all; 限制所有的ip # allow ip; 除了 这个 ...

  2. TYVJ 1427 线段树的基本操作

    题意: 单点修改,区间最值 思路: 线段树 原题请戳这里 //By SiriusRen #include <cstdio> #include <cstring> #includ ...

  3. Npgsql使用入门(二)【实用助手类】

    数据库映射的实体类: public class Test { public long TestID { get; set; } public string Name { get; set; } pub ...

  4. python爬虫:爬取凤凰指数

    在知乎上看到的这个问题,讲讲我爬取过程中遇到的问题: 1.循环爬取其他页面,在其他项目中用循环一般可以搞定,可是这个,第一页和第二第三页的表格是不同的,所以要重新写规则,我懒,写了第一页后,就不想在写 ...

  5. j2ee学习笔记

    Servlet: ①字符集问题:②身份验证:③统一的输入输出:类似母版,重写ServletResponeceWraper的getWriter,使用缓存流. 然后输出时,分析stringWriter , ...

  6. JAVA(TOMCAT)远程调试

    当我们的项目部署到远程机器(开发环境和部署环境在不同的jvm下)中,而远程机器的数据和本地有可能不一样,这个时候我们可能需要连接到远程机器进行调试.为了解决这一问题,JAVA为我们提供了Java平台调 ...

  7. dll文件:关于MFC程序不能定位输入点

    问题:无法定位程序输入点到动态链接库上...... 过程:找完整个工程文件夹: APS为资源文件: resource.h为定义文件: 完全复制 除主文件以外的所有文件,程序可用: 在主程序框Cpp文件 ...

  8. Kinect+OpenNI+OpenCV使用

    关于OpenNI,已经可以使用2.0,可以不再使用PrimeSense: 这里的是转载其他人的 OpenCV系列: 原文:http://blog.csdn.net/chenxin_130/articl ...

  9. 页面footer在底部

    页脚动态贴在底部需要满足以下两个条件: 当主体的内容高度不超过可视区域高度的时候,页脚贴在页面底部. 当主体的内容高度超过可视区域高度的时候,页脚将按正常布局. 方法一:footer高度固定+绝对定位 ...

  10. C# model代码生成器

    using System.Collections.Generic; using System.Text; public class Class1 { //传递 1.表名 2.列名 3.类型 publi ...