B. Queue
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.
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.
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.
4
92 31
0 7
31 0
7 141
92 7 31 141
#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的更多相关文章
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Azure Queue Storage 基本用法 -- Azure Storage 之 Queue
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- 初识Message Queue之--基础篇
之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...
- 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接
我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...
- PriorityQueue和Queue的一种变体的实现
队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...
- C#基础---Queue(队列)的应用
Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...
- [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 ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 源码之Queue
看源码可以把python看得更透,更懂,想必也是开发人员的必经之路. 现在有个任务,写个线程池.使用Queue就能写一个最简单的,下面就来学学Queue源码. 源码之Queue: class Queu ...
随机推荐
- 【Oracle】delete表后commit后怎么找回,方法
有些时候,不小心删除了一些需要的表,而且数据库不能停止,只能一直运行下去,这样的话很麻烦 下面介绍的方法就是删除表后通过时间戳后者scn找回删除的数据 模拟实验环境: 创建一个新表 SQL> c ...
- C#数组的 Length 和 Count()
C#数组的 Length 和 Count() C# 数组中 Length 表示数组项的个数,是个属性.而 Count() 也是表示项的个数,是个方法,它的值和 Length 一样.但实际上严格地说, ...
- kubernets之DaemonSet
一 k8s资源之DaemonSet 1.1 介绍认识DaemonSet DaemonSet可以理解为一种比较特殊的RS,DaemonSet的作用是永远保持被指定的节点只运行一个pod的副本,可用作集 ...
- LeetCode965. 单值二叉树
题目 1 class Solution { 2 public: 3 int flag = 0; 4 bool isUnivalTree(TreeNode* root){ 5 isUnivalTree1 ...
- go语言循环变量
阅读go语言圣经第五章第六节介绍到了捕获迭代变量 package main import ( "fmt" ) func main() { var lis []func() for ...
- Jquery实现对Array数组实现类似Linq的Lambda表达式的Where方法筛选
平时使用Linq,习惯了Lambda表达式,用着非常顺手,奈何在Jquery里面不能这样用,只能循环一个个判断.趁空闲时间找了找,自己写了这样的扩展方法.目前写出了三种方案,没有比较性能,觉得都可以用 ...
- 一句话木马拿下webshell
1.我们先建立一个简单的一句话木马文件,我们这里就命名为shell2吧. 2.因为提交的文件可能是有过滤的,我们这个靶场的这个题目就是禁止上传危险的文件类型,如jsp jar war等,所以就需要绕过 ...
- [Usaco2008 Mar]牛跑步
题目描述 BESSIE准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘, 然后走回牛棚. BESSIE也不想跑得太远,所以她想走最短的路经. 农场上一共有M (1 < ...
- STL_deque容器
一.deque简介 deque是"double-ended queue"的缩写,和vector一样都是STL的容器,deque是双端数组,而vector是单端的. deque在接口 ...
- 面试官:你说说ReentrantLock和Synchronized区别
大家好!又和大家见面了.为了避免面试尴尬,今天同比较通俗语言和大家聊下ReentrantLock和Synchronized区别! 使用方式 Synchronized可以修饰实例方法,静态方法,代码块. ...