Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fi='baib.inp';
- fo='baib.out';
- var a,b,c,ans:array[1..1000000] of longint;
- n,i,j,next:longint;
- procedure init;
- var i:longint;
- begin
- assign(input,fi);reset(input);
- readln(n);
- for i:=1 to n do
- begin
- readln(a[i],b[i]);
- c[i]:=b[i];
- end;
- close(input);
- end;
- procedure qsort(l,r:longint);
- var tm,i,j,p:longint;
- begin
- if l>=r then exit;
- i:=l;
- j:=r;
- p:=c[(l+r) div 2];
- while i<=j do
- begin
- while c[i]<p do inc(i);
- while c[j]>p do dec(j);
- if i<=j then
- begin
- tm:=c[i];
- c[i]:=c[j];
- c[j]:=tm;
- inc(i);
- dec(j);
- end;
- end;
- qsort(i,r);qsort(l,j);
- end;
- function check(i:longint):boolean;
- var l,r,m:longint;
- begin
- l:=1;
- r:=n;
- while l<=r do
- begin
- m:=(l+r) div 2;
- if i<c[m] then r:=m-1;
- if i>c[m] then l:=m+1;
- if i=c[m] then exit(true);
- end;
- exit(false);
- end;
- procedure solve;
- var i:longint;
- begin
- for i:=1 to n do if not check(a[i]) then
- begin
- ans[1]:=a[i];
- break;
- end;
- end;
- procedure qsort2(l,r:longint);
- var tm,i,j,p:longint;
- begin
- if l>=r then exit;
- i:=l;
- j:=r;
- p:=a[(l+r) div 2];
- while i<=j do
- begin
- while a[i]<p do inc(i);
- while a[j]>p do dec(j);
- if i<=j then
- begin
- tm:=a[i];
- a[i]:=a[j];
- a[j]:=tm;
- tm:=b[i];
- b[i]:=b[j];
- b[j]:=tm;
- inc(i);
- dec(j);
- end;
- end;
- qsort2(i,r);qsort2(l,j);
- end;
- function nhiphan(i:longint):longint;
- var l,r,m,cur:longint;
- begin
- l:=1;
- r:=n;
- cur:=0;
- while l<= r do
- begin
- m:=(l+r) div 2;
- if a[m]>i then r:=m-1;
- if a[m]<i then l:=m+1;
- if a[m]=i then exit(b[m]);
- end;
- end;
- begin
- assign(output,fo);rewrite(output);
- init;
- qsort(1,n);
- solve;
- qsort2(1,n);
- ans[2]:=b[1];
- j:=3;
- i:=1;
- next:=b[1];
- while next<>0 do
- begin
- next:=nhiphan(ans[i]);
- ans[j]:=next;
- inc(i);
- inc(j);
- end;
- for i:=1 to j-2 do write(ans[i],' ');
- close(output);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement