During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break
and it temporarily stopped working.

Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands
before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated
from 1).

After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.

Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.

Input

The first line contains integer n (2 ≤ n ≤ 2·105)
— the number of students in the queue.

Then n lines follow, i-th line contains the pair
of integers ai, bi (0 ≤ ai, bi ≤ 106),
where ai is
the ID number of a person in front of a student and bi is
the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't
exist.

The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.

Output

Print a sequence of n integers x1, x2, ..., xn —
the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.

Sample test(s)
input
4
92 31
0 7
31 0
7 141
output
92 7 31 141
这道题错了很久,不是wa,re就是te,最后发现是数组开小了。。。这题先记录每个数的前面和后面分别是谁,分两种情况讨论,一种总数是偶数,那么只要找到0,然后从0向前循环一遍,向后循环一遍就行了,如果是奇数,那么除了0向后循环一遍外,还要找到除0外的最后一个数,然后再向前循环一遍。
#include<stdio.h>

#include<string.h>

#define count 1000005

int next[count],pre[count],c[count],vis[count],a[count],b[count];

int main()

{
int n,m,i,j,t,u;
while(scanf("%d",&n)!=EOF)
{
memset(next,-1,sizeof(next));memset(pre,-1,sizeof(pre));memset(c,0,sizeof(c));
memset(vis,0,sizeof(vis));memset(a,0,sizeof(a));memset(b,0,sizeof(b));
if(n==2){
for(i=1;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
if(a[i]==0){
c[1]=b[i];
}
else if(b[i]==0){
c[0]=a[i];
}
}
   printf("%d %d\n",c[0],c[1]);
continue;
}
for(i=1;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
next[a[i]]=b[i];
pre[b[i]]=a[i];
}
if(n%2==0){
 t=1;
 for(i=next[0];i!=-1 && t<n;i=next[i]){
c[t]=i;t=t+2;
 }
 t=n-2;
 for(i=pre[0];i!=-1 && t>=0;i=pre[i]){
c[t]=i;t=t-2;
 }
}

if(n%2!=0){
 t=1;
 for(i=next[0];i!=-1 && t<n;i=next[i]){
c[t]=i;t=t+2;vis[i]=1;
 }
 u=-1;
 for(i=1;i<=n;i++){

  if(vis[a[i]]==0 && a[i]!=0){
  u=a[i];break;
}

   }

   while(next[u]!=-1){

    u=next[u];

   }

   t=n-1;

   for(i=u;i!=-1 && t>=0;i=pre[i]){

  c[t]=i;t=t-2;

  }
 
}

for(i=0;i<n;i++){
if(i!=n-1){
printf("%d ",c[i]);
}
else printf("%d\n",c[i]);
}
}
return 0;

}

B. Queue的更多相关文章

  1. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  2. Azure Queue Storage 基本用法 -- Azure Storage 之 Queue

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...

  3. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  4. 初识Message Queue之--基础篇

    之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...

  5. 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接

    我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...

  6. PriorityQueue和Queue的一种变体的实现

    队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...

  7. C#基础---Queue(队列)的应用

       Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...

  8. [LeetCode] Queue Reconstruction by Height 根据高度重建队列

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  9. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  10. 源码之Queue

    看源码可以把python看得更透,更懂,想必也是开发人员的必经之路. 现在有个任务,写个线程池.使用Queue就能写一个最简单的,下面就来学学Queue源码. 源码之Queue: class Queu ...

随机推荐

  1. Java虚拟机常用的性能监控工具

    基础故障处理工具 jps: 虚拟机进程状况工具 功能:来处正在运行的虚拟机进程,并显示虚拟机执行主类名称,以及本地虚拟机唯一ID. 它是使用频率最高的命令行工具,因为其他JDK工具大多需要输入他查询到 ...

  2. Linux tar压缩和解压

    经常会忘记 tar 压缩和解压命令的使用,故记下来. 1. 打包压缩 tar -zcvf pack.tar.gz pack/ #打包压缩为一个.gz格式的压缩包 tar -jcvf pack.tar. ...

  3. 无限重置IDE过期时间插件 亲测可以使用

    相信破解过IDEA的小伙伴,都知道jetbrains-agent这个工具,没错,就是那个直接拖入到开发工具界面,一键搞定,so easy的破解工具!这个工具目前已经停止更新了,尽管还有很多小伙伴在使用 ...

  4. ClickHouse安装使用(单机、集群、高可用)

    Clickhouse版本:20.3.6.40-2 安装包地址:https://repo.yandex.ru/clickhouse/rpm/stable/x86_64/ 一.单机版 1.安装依赖 yum ...

  5. web测试误区:浏览器后退键退出系统会话失效

    通过最近测试的项目,认识到实际:浏览器后退键退出系统,会话仍旧有效.打破了之前认为浏览器后退键就会退出系统登录的认知. 一,了解Cookie和Session的作用,具体来说cookie机制采用的是在客 ...

  6. gstack pstack strace

    gstack pstack strace 通过进程号 查看 进程的工作目录 Linux神器strace的使用方法及实践 - 知乎 https://zhuanlan.zhihu.com/p/180053 ...

  7. 在Ubuntu安装kubernetes

    一.安装Docker 1. 配置Docker docker安装完成后需要配置cgroup驱动为systemd来增强稳定性 sudo vim /etc/docker/daemon.json { &quo ...

  8. (万字好文)Dubbo服务熔断与降级的深入讲解&代码实战

    原文链接:(万字好文)Dubbo服务熔断与降级的深入讲解&代码实战 一.Dubbo服务降级实战 1 mock 机制 谈到服务降级,Dubbo 本身就提供了服务降级的机制:而 Dubbo 的服务 ...

  9. CF492B

    题意 一条长为L的路,在n个不同的位置都放置了路灯,灯光半径相同,问半径至少为多少时灯光可以覆盖整条路. 那我们就先排序,使灯的位置是从路的一边依次排到另一边的 ,然后求出两两挨着的灯之间距离的最大值 ...

  10. js异步、事件循环(EventLoop)小结

    单线程 众所周知,JS是单线程的语言,之所以是单线程,用一句烂大街的话就是,如果两个线程同时操作一个DOM节点,那么该以哪个为准呢,虽然多线程也有办法解决,但是js毕竟是浏览器脚本语言,不需要那么复杂 ...