程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Codeforces Round #279 (Div. 2) £¨BÌ⣩

Codeforces Round #279 (Div. 2) £¨BÌ⣩

編輯:C++入門知識

Codeforces Round #279 (Div. 2) £¨BÌ⣩


B. Queue time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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.

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 integer n (2?¡Ü?n?¡Ü?2¡¤105) ¡ª the number of students in the queue.

Then n lines follow, i-th line contains the pair of integers ai,?bi (0?¡Ü?ai,?bi?¡Ü?106), where ai is the ID number of a person in front of a student and bi 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.

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 sequence of n integers x1,?x2,?...,?xn ¡ª 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
4
92 31
0 7
31 0
7 141
output
92 7 31 141 
Note

The picture illustrates the queue for the first sample.

\

<†·Ÿ"http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPjxzdHJvbmc+1eK8uMzs0LS0+sLr0LTjwsHLLi4uLjwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz7LvMK3o7q9qMGi0ru49s/yuvO1xMG0us3P8sewtcTBtKGj08PP8rrztcTBtKOstNMwv6rKvMmo0rux6aOsv8nS1LXDtb3FxdTaxbzK/c67tcSho8i7uvPL5rHj1dLSu7j2w7uz9s/Wuf21xLrFo6zTw8/yx7C1xMG0uPrX2bW9zbejrL7NysfFxdTatdrSu867tcSjrNTZ08PP8rrztcTBtMmo0rux6aOstcO1vcXF1NrG5sr9zru1xKGjPGJyPgo8L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+xuTKtdXiy7zCt8rHvei8+NGns6S1xKOsuNC+9dPQtePP8daux7DX9rn9tcSyorLpvK8uLi4uLsH0uPjS1Lrz1NnLtbDJPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPjxicj4KPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPkFDtPrC66O6PC9zdHJvbmc+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 %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


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved