#include #define MAXN 100 using namespace std; int main() { // front[a] is the branch just below branch a, or -1 if a is a root int front[MAXN+1]; int cat; cin >> cat; int branch; /* initialize all branches to point to the ground so the branches that don't appear on the right side of some input are roots */ for (int i = 1; i <= MAXN; ++i) { front[i] = -1; } // read in the tree description while ((cin >> branch) && branch != -1) { // peek ahead, if we see a newline then the line is done while (cin.peek() != '\n') { int higher; cin >> higher; // the branch "higher" lies directly above "branch" front[higher] = branch; } } cout << cat; while (front[cat] != -1) { // hop down to the next branch cat = front[cat]; cout << ' ' << cat; } cout << endl; return 0; }