import sys
def organize_queue(left_dict, right_dict, first, last, n):
queue = [0] * n
queue[0] = first
left = first
right = 0
idx = 2
while idx < n:
right = right_dict[left]
queue[idx] = right
left = right
idx += 2
right = right_dict[0]
queue[1] = right
left = right
idx = 3
while idx < n:
right = right_dict[left]
queue[idx] = right
left = right
idx += 2
return queue
def main():
input = sys.stdin.read
data = input().strip().split('\n')
n = int(data[0])
students = [tuple(map(int, line.split())) for line in data[1:]]
left_dict = {}
right_dict = {}
for a, b in students:
left_dict[b] = a
right_dict[a] = b
left_set = set(left_dict.values())
right_set = set(right_dict.values())
first = (left_set - right_set).pop()
last = (right_set - left_set).pop()
queue = organize_queue(left_dict, right_dict, first, last, n)
print(' '.join(map(str, queue)))
if __name__ == "__main__":
main()
#include <bits/stdc++.h>
using namespace std;
#define int long long
//dsu?我只会分类讨论,感觉有点像链式前向星?
signed main() {
int test = 1;
//cin >> test;
while (test--) {
int n;
cin >> n;
if (n % 2 == 0) {
int a[n + 5];
map<int, int>mp1, mp2;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
mp1[x] = y; //后
mp2[y] = x; //前
}
a[2] = mp1[0];
a[n - 1] = mp2[0];
int t = mp1[0];
for (int i = 4; i <= n; i += 2) {
a[i] = mp1[t];
t = mp1[t];
}
t = mp2[0];
for (int i = n - 3; i >= 1; i -= 2) {
a[i] = mp2[t];
t = mp2[t];
}
for (int i = 1; i <= n; i++) {
cout << a[i] << " ";
}
cout << endl;
} else {
map<int, int>mp;
map<int, int>mp1; //标记后面的就行
int a[n + 5];
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
mp[x]--;
mp[y]++;
mp1[x] = y;
}
int jishukaitou;
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
if (it->second == -1) {
jishukaitou = it->first;
}
}
a[1] = jishukaitou;
int t = jishukaitou;
for (int i = 3; i <= n; i += 2) {
a[i] = mp1[t];
t = mp1[t];
}
a[2] = mp1[0];
t = mp1[0];
for (int i = 4; i <= n; i += 2) {
a[i] = mp1[t];
t = mp1[t];
}
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
cout << endl;
}
}
}
/*
1 2 3 4 5
0 2
1 3
2 4
3 5
4 0
*/
152. Maximum Product Subarray | 337. House Robber III |
869. Reordered Power of 2 | 1593C - Save More Mice |
1217. Minimum Cost to Move Chips to The Same Position | 347. Top K Frequent Elements |
1503. Last Moment Before All Ants Fall Out of a Plank | 430. Flatten a Multilevel Doubly Linked List |
1290. Convert Binary Number in a Linked List to Integer | 1525. Number of Good Ways to Split a String |
72. Edit Distance | 563. Binary Tree Tilt |
1306. Jump Game III | 236. Lowest Common Ancestor of a Binary Tree |
790. Domino and Tromino Tiling | 878. Nth Magical Number |
2099. Find Subsequence of Length K With the Largest Sum | 1608A - Find Array |
416. Partition Equal Subset Sum | 1446. Consecutive Characters |
1618A - Polycarp and Sums of Subsequences | 1618B - Missing Bigram |
938. Range Sum of BST | 147. Insertion Sort List |
310. Minimum Height Trees | 2110. Number of Smooth Descent Periods of a Stock |
2109. Adding Spaces to a String | 2108. Find First Palindromic String in the Array |
394. Decode String | 902. Numbers At Most N Given Digit Set |