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
The picture illustrates the queue for the first sample.
![](https://i-blog.csdnimg.cn/blog_migrate/54ad6217a7cb84b94bbf7202319e482c.png)
题意:给你每个人前后的编号,如果前或者后没人则编号为0。
做法:我是用p1容器记录每个人后一个编号,p2容器记录每个人前一个编号,当n为偶数时则根据如果0 a,则a是第二位,b 0,b就是n-1位,然后根据p1,p2来推出其他位置的编号,如果n为奇数,能求出第二位和第n-1位的编号,但都是偶数位,所以先把偶数的编号标记一下,然后找出任一一个奇数位置的编号,然后根据p1容器来求出第一位置的编号(即p2[1].size()==0),然后推出其他位置即可。
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL long long
#define inf 0x0f0f0f0f
using namespace std;
vector<int>pp[1000005];
vector<int>pp2[1000005];
int a[200005],b[200005];
int num[200005];
int vis[1000005];
int main()
{
int n;
int i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<=1000000;i++)
{
pp[i].clear();
pp2[i].clear();
}
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i],&b[i]);
pp[a[i]].push_back(b[i]);
pp2[b[i]].push_back(a[i]);
if(a[i]==0)
num[2]=b[i];
if(b[i]==0)
num[n-1]=a[i];
}
int kk,js;
if(n%2==0)
{
kk=2;
while(kk<n)
{
if(kk+2<=n)
num[kk+2]=pp[num[kk]][0];
kk=kk+2;
}
kk=n-1;
while(kk>1)
{
if(kk-2>0)
num[kk-2]=pp2[num[kk]][0];
kk-=2;
}
}
else
{
kk=2;
memset(vis,0,sizeof(vis));
vis[num[2]]=1;
while(kk<n)
{
if(kk+2<=n)
{
num[kk+2]=pp[num[kk]][0];
vis[num[kk+2]]=1;
}
kk=kk+2;
}
int cc;
for(i=0;i<n;i++)
if(vis[a[i]]==0&&a[i]!=0)
{
cc=a[i];
break;
}
int st;
while(1)
{
if(pp2[cc].size()==0)
{
st=cc;
break;
}
else
{
cc=pp2[cc][0];
}
}
num[1]=st;
kk=1;
while(kk<n)
{
if(kk+2<=n)
num[kk+2]=pp[num[kk]][0];
kk+=2;
}
}
for(i=1;i<=n;i++)
{
printf("%d",num[i]);
if(i!=n)
printf(" ");
}
printf("\n");
}
}