#include <iostream>
using namespace std;
#include <list>
int main()
{
    int num;
    cin >> num;
    list<int> lst;
    int head;
    cin >> head;
    lst.push_front(head);
    int p; // 前节点
    int n; // 后节点
    for (int i = 1; i < num; i++)
    {
        cin >> n; // 第一个参数为后节点
        cin >> p; // 第二个参数为前节点
        for (list<int>::iterator it = lst.begin(); it != lst.end(); it++)
        {
            if (*it == p)
            {
                lst.insert(it, n);
            }
        }
    }
    int d;
    cin >> d;
    lst.remove(d);
    lst.reverse();
    for (list<int>::iterator it = lst.begin(); it != lst.end(); it++)
    {
        cout << *it << " ";
    }
}

C++ STL list的应用,这里插反了问题不大用个翻转完事()()()