知识点:静态链表
坑点,可能链表为空,就是最后一个测试点
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) x.size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
const int N = 1e5 + 5;
struct node {
int address, key, nxt, flag;
node(int a = 0, int b = 0, int c = 0, int d = N): address(a), key(b), nxt(c), flag(d) {}
} List[N];
bool cmp(node a, node b) {
if (a.flag != b.flag) return a.flag < b.flag;
else return a.key < b.key;
}
int main() {
int n, s; cin >> n >> s;
for (int i = 0; i < n; i++) {
int a, b, c; cin >> a >> b >> c;
List[a] = node(a, b, c, N);
}
int p = s, cnt = 0;
while (p != -1) {
List[p].flag = 1; cnt++; p = List[p].nxt;
}
sort(List, List + N, cmp);
if (!cnt) { cout << 0 << " " << -1; return 0; }
printf("%d %05d\n", cnt, List[0].address);
for (int i = 0; i < cnt - 1; i++) printf("%05d %d %05d\n", List[i].address, List[i].key, List[i + 1].address);
printf("%05d %d -1", List[cnt - 1].address, List[cnt - 1].key);
return 0;
}