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/29/C
Description
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 离散化拓扑排序的更多相关文章
- Codeforces Beta Round #29 (Div. 2, Codeforces format)
Codeforces Beta Round #29 (Div. 2, Codeforces format) http://codeforces.com/contest/29 A #include< ...
- 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 ...
- Codeforces Beta Round #32 (Div. 2, Codeforces format)
Codeforces Beta Round #32 (Div. 2, Codeforces format) http://codeforces.com/contest/32 A #include< ...
- Codeforces Beta Round #31 (Div. 2, Codeforces format)
Codeforces Beta Round #31 (Div. 2, Codeforces format) http://codeforces.com/contest/31 A #include< ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
随机推荐
- nginx修改内核参数
1.修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件 数量的限制(这是因为系统为 ...
- windows中安装python
windows中安装python 在windows中安装python的步骤如下. 1.下载python的安装包 python的安装包地址为: https://www.python.org/ftp/py ...
- Python的OO思想
想当年大二的时候,在学校学习Java, 最牛逼的OO思想,用了3页纸就讲完了,还是清华大学出版社的呢. 后来全凭自己啃视频,啃代码才搞懂什么叫做OO. 现在学习Python,就用自己的方式,好好学习一 ...
- php连接数据库
<?php header('Content-Type:text/html; charset=utf-8'); define('DB_HOST', 'localhost'); define('DB ...
- Ubuntu 上安装R
1. 编辑 /etc/apt/sources.listsudo cp /etc/apt/sources.list /etc/apt/sources.list.backupsudo gedit sour ...
- linux中hosts文件的修改
转载自http://hi.baidu.com/dillisbest/item/5e0b612d011b4cd40e37f9a6 1. 关于/etc/host,主机名和IP配置文件 Hosts - Th ...
- CodeIgniter 3.0+ 部署linux环境 session报错
codeigniter Message: mkdir(): Invalid path Filename: drivers/Session_files_driver.php 看起来像权限问题,在默认情况 ...
- 如何创建Asp.net MVC ViewModel
ASP.NET MVC View Model Patterns Since MVC has been released I have observed much confusion about how ...
- Eclipse或Myeclipse常用快捷键组合详解
Eclipse 是一个开放源代码的.基于Java的可扩展开发平台,就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境.. Eclipse(Myeclipse)中有很多便于开发的快捷键 ...
- RxCache 的代码分析,含缓存时间duration的在代码中改变的自己实现的机制
当应用进程创建 RxCache 的实例后,会给应用进程返回一个 rxcache实例及一个 ProxyProvider,代码如下: CacheProviders providers = new RxCa ...