input:hotel.in output:hotel.out
Time limit:
1000 MS space constraints: 262144 KB specific restrictions
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 }