codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

490B.md (2129B)


      1 Source: https://codeforces.com/problemset/problem/490/B
      2 
      3 B. Queue
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 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.
     22 
     23 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).
     24 
     25 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.
     26 
     27 Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
     28 
     29 Input
     30 
     31 The first line contains integer *n* (2 ≤ *n* ≤ 2·105) — the number of students in the queue.
     32 
     33 Then *n* lines follow, *i*-th line contains the pair of integers *a**i*, *b**i* (0 ≤ *a**i*, *b**i* ≤ 106), where *a**i* is the ID number of a person in front of a student and *b**i* 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.
     34 
     35 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.
     36 
     37 Output
     38 
     39 Print a sequence of *n* integers *x*1, *x*2, ..., *x**n* — 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.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 4  
     47 92 31  
     48 0 7  
     49 31 0  
     50 7 141
     51 ```
     52 
     53 Output
     54 
     55 ```
     56 92 7 31 141
     57 ```
     58 
     59 Note
     60 
     61 The picture illustrates the queue for the first sample.
     62 
     63  ![](https://espresso.codeforces.com/7b7ddb9301a8862170a52cde1b833386c3882e9a.png)