JZ junior OJ 2293. [noip universal group 1] Sham Inn

Title Description

A total of N people in the queue, they have their own number, to ensure that a number greater than 0 and less than 2000001,
everyone has their own front and back numbers of people, if nobody in front of him or behind, it's used instead of 0,
a total of only Bring a team.
 

Entry

The first row represents an integer N number of people
next line N represents the number of notes on each of the
different reference numerals to ensure that everyone

Export

Line number N, separated by a space intermediate, this number represents.
 

Sample input

Sample input [1] 
. 4 
92 31 is 
0. 7 
31 is 0 
. 7 141 is 
[2] Sample input 
. 5 
0 1 
1. 4 
. 4 0 
. 3 2 
. 5. 3

Sample Output

[1] Sample Output 
92731141 
[2] Sample Output 
51342
 

Data range limit

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=2100000;
 4 bool sf[maxn];
 5 int n,x,y,a[maxn],all,next[maxn],last[maxn],first1,first2;
 6 int main()
 7 {
 8     freopen("hotel.in","r",stdin);
 9     freopen("hotel.out","w",stdout);
10     sf[0]=1;
11     memset(last,-1,sizeof(last));
12     cin>>n;
13     for(int i=1;i<=n;i++)
14     {
15         scanf("%d%d",&x,&y);
16         next[x]=y;
17         last[y]=x;
18         if(x==0)
19             first2=y;
20         if(sf[x]==0)
21         {
22             a[++all]=x;
23             sf[x]=1;
24         }
25         if(sf[y]==0)
26         {
27             a[++all]=y;
28             sf[y]=1;
29         }
30     }
31     for(int i=1;i<=all;i++)
32         if(last[a[i]]==-1)
33             first1=a[i];
34     for(int i=1;i<=n/2;i++)
35     {
36         cout<<first1<<" "<<first2<<" ";
37         first1=next[first1];
38         first2=next[first2];
39     }
40     if(n%2==1) cout<<first1;
41     return 0;
42 }

Guess you like

Origin www.cnblogs.com/anbujingying/p/11317271.html