测试点4考察的是开始地址为-1的情况,直接添加语句输出0 -1就可以了,不然下面输出chain[0].location的时候会报段错误。PS:一开始没考虑到:(
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 100001
struct node {
int location;
int next;
int key;
};
struct node info[maxn];
vector<struct node>chain;
bool compare(struct node n1, struct node n2) {
return n1.key < n2.key;
}
int main() {
int N,start; cin >> N>>start;
if (start == -1) {
cout << 0 << " " << -1;
return 0;
}
for (int i = 1; i <= N; i++) {
int address, k, n;
scanf("%05d %d %05d", &address, &k, &n);
info[address].location = address;
info[address].key = k;
info[address].next = n;
}
int current = start;
while (current != -1) {
chain.push_back(info[current]);
current = info[current].next;
}
sort(chain.begin(), chain.end(), compare);
printf("%d %05d\n", chain.size(), chain[0].location);
for (int i = 0; i < chain.size(); i++) {
if (i != chain.size() - 1)
printf("%05d %d %05d\n", chain[i].location, chain[i].key, chain[i + 1].location);
else
printf("%05d %d %d\n", chain[i].location, chain[i].key, -1);
}
return 0;
}