fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define pb push_back
  5. #define fi first
  6. #define se second
  7. #define all(x) x.begin(),x.end()
  8. const ll MAXN=1e6+5,MOD=998244353,INF=1e9,LG=16,MAX=2e6;
  9. ll i,n,m,j,k,p,t,ans,nex[MAXN],pre[MAXN],b[MAXN];
  10. struct h{
  11. ll pre,suf;
  12. } a[MAXN];
  13. int main(){
  14. ios::sync_with_stdio(0);
  15. cin.tie(0);cout.tie(0);
  16. // freopen("changedir.inp","r",stdin);
  17. // freopen("changedir.out","w",stdout);
  18. cin>>n;
  19. for(i=1;i<=n;i++){
  20. cin>>a[i].pre>>a[i].suf;
  21. nex[a[i].pre]=a[i].suf;
  22. pre[a[i].suf]=a[i].pre;
  23. }
  24. ll cur=nex[0],id=2;
  25. while(cur!=0){
  26. b[id]=cur;
  27. ll luu=cur;
  28. cur=nex[cur];
  29. nex[luu]=0;
  30. pre[luu]=0;
  31. id+=2;
  32. }
  33. for(i=1;i<=MAX;i++){
  34. if(nex[i]&&!pre[i]){
  35. cur=i;
  36. break;
  37. }
  38. }
  39. id=1;
  40. while(cur!=0){
  41. b[id]=cur;
  42. ll luu=cur;
  43. cur=nex[cur];
  44. nex[luu]=0;
  45. pre[luu]=0;
  46. id+=2;
  47. }
  48. for(i=1;i<=n;i++)
  49. cout<<b[i]<<" ";
  50. }
Success #stdin #stdout 0.01s 7596KB
stdin
Standard input is empty
stdout
Standard output is empty