Codeforces Round #279 (Div. 2) £°b ;â £©

Source: Internet
Author: User

Codeforces Round #279 (Div. 2) £°b ;â £

B. Queuetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

During the lunch break allNBerland 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.

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 ).

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.

Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.

Input

The first line contains integerN(2? ¡U?N? ¡U? 2 rows passed 105) beyond the number of students in the queue.

ThenNLines follow,I-Th line contains the pair of integersAI,?BI(0? ¡U?AI,?BI? ¡U? 106), whereAIIs the ID number of a person in front of a student andBIIs 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.

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.

Output

Print a sequenceNIntegersX1 ,?X2 ,?...,?XNPlease specify 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.

Sample test (s) input
492 310 731 07 141
Output
92 7 31 141 
Note

The picture into strates the queue for the first sample.


<Strong · Strong "http://www.bkjia.com/kf/ware/vc/" target = "_ blank" class = "keylink"> vcD4KPHA + PGJyPgo8L3A + CjxwPjxzdHJvbmc + 1eK8uMzs0LS0 + strong/strong + strong + xuTKtdXiy7zCt8rHvei8 + NGns6S1xKOsuNC + release + PC9wPgo8cD48cHJlIGNsYXNzPQ = "brush: java; "> # include # Include # Include using namespace std; int data1 [1000010], data2 [1000010], vis [1000010], ans [200010]; int main () {int n; scanf ("% d", & n); memset (data1,-1, sizeof (data1); memset (data2,-1, sizeof (data2 )); memset (vis, 0, sizeof (vis); for (int I = 1; I <= n; I ++) {int a, B; scanf ("% d", & a, & B); data1 [a] = B; data2 [B] = a; vis [a] = 1; vis [B] = 1;} int tmp = 0, k = 2; while (1) {ans [k] = data1 [tmp]; vis [ans [k] = 0; k + = 2; tmp = data1 [tmp]; if (tmp <= 0) break;} int found; for (int I = 1; I <= 1000000; I ++) {if (vis [I]) {found = I; break ;}} tmp = found; while (1) {if (data2 [tmp] <0) break; tmp = data2 [tmp];} k = 1; while (1) {ans [k] = tmp; tmp = data1 [tmp]; k + = 2; if (tmp <= 0) break;} int I; for (I = 1; I


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.