CF490B.Queue

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

午餐休息期间,所有 nn 名贝尔兰国立大学的学生在食堂排起了队。然而,他们发现食堂本身也有午休时间,此时暂时停止了服务。

站在一条不提供服务的队伍中是如此无聊!因此,每位学生都写下了排在他正前方的学生的学号,以及排在他正后方的学生的学号。如果某位学生前面或后面没有人(即他是第一位或最后一位),那么他就写下数字 00(在贝尔兰国立大学,学生学号从 11 开始编号)。

之后,所有学生都去忙自己的事情了。当他们回来时,才发现根据各自记录的前后邻居学号来还原队伍顺序并非一件容易的事。

请帮助学生们根据他们所记录的队列中前后邻居的学号,还原出队伍的原始状态。

输入格式

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

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.

第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5)—— 队列中学生的数量。

接下来是 nn 行,其中第 ii 行包含一对整数 ai, bia_i,\, b_i(0≤ai, bi≤1060 \leq a_i,\, b_i \leq 10^6),其中 aia_i 表示排在该学生前面的学生的编号,bib_i 表示排在该学生后面的学生的编号。这些行以任意顺序给出。若某学生没有前(或后)邻,则对应位置用 00 表示。

所有学生的编号互不相同。保证这些记录对应一个所有学生按某种顺序排成的队列。

输出格式

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.

输出一个包含 n 个整数 _x_₁, _x_₂, ..., x__n 的序列——即所有学生在队列中的 ID 编号序列,顺序为从队首学生到队尾学生。

输入输出样例

  • 输入#1

    4
    92 31
    0 7
    31 0
    7 141

    输出#1

    92 7 31 141

说明/提示

The picture illustrates the queue for the first sample.

该图展示了第一个样例的队列。

输入解题思路,AI测评打分。不知道怎么写?

首页