#include<iostream> #include<list> usingnamespace std; int num, first, x, y, z; list<int>::iterator loc[100003]; //使用编号作为数组地需要,在相应的数组中存入结点的位置!高啊!
intmain() { list<int> L; //链表 cin >> num >> first; L.push_back(first); //把第一辆车放到链表里 loc[first] = L.begin(); //把第一辆车的位置,存到以车编号为序号的数字里 list<int>::iterator temp; //遍历用的迭代器 for (int i = 1; i < num; i++) { cin >> x >> y >> z; temp = loc[y]; //将这个y需要的车的位置拿出来给了temp if (z > 0) //放右边的时候 { L.insert(++temp, x); //放到temp后一个位置的前一个,也就是temp后面 loc[x] = --temp; //把刚放入的车子位置以车编号为数组下标存入数组 } else { L.insert(temp, x); //同理 loc[x] = --temp; } } for (temp = L.begin(); temp != L.end(); temp++) //遍历序列输出 cout << *temp << " "; return0; }
Python
1 2 3 4 5 6 7 8 9
n=int(input()) res=[input()] for i inrange(n-1): x,y,z=input().split() if z=='0': res.insert(res.index(y),x) else: res.insert(res.index(y)+1,x) print(' '.join(res))