思路和1097类似。。。加个index有奇效,所以当天考试的时候秒过。。。就是要注意可能会有废节点。。。
#include#include #include #include using namespace std;const int inf = 99999999;struct node { int val; int add; int next; int index=inf;};bool cmp(node n1, node n2) { return n1.index < n2.index;}int main() { int r, num, k; cin >> r >> num >> k; node *arr = new node[100010]; for (int i = 0; i < num; i++) { int add, val, next; cin >> add >> val >> next; arr[add].add = add; arr[add].val = val; arr[add].next = next; } int p = 0; int negative = 0, low_k = 0, high_k = 0; for (p; p < num&&r != -1; p++) { if (arr[r].val < 0) { arr[r].index = negative; negative++; } else if (arr[r].val >= 0 && arr[r].val <= k) { arr[r].index = 100000 + low_k; low_k++; } else { arr[r].index = 200000 + high_k; high_k++; } r = arr[r].next; } sort(arr, arr + 100010, cmp); for (int i = 0; i < p - 1; i++) { printf("%05d %d %05d\n", arr[i].add, arr[i].val, arr[i + 1].add); } printf("%05d %d %d\n", arr[p-1].add, arr[p-1].val, -1); system("pause");}