把num!=k+1写成了,num不等于1,导致只有测试点1错误,好几次这样,所以大部分都错误的时候,未必是思路不对,可能就是一个小细节没对
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3ffffff;
struct node1{
int address,data,next,order=inf;
}node[100000];
bool cmp(node1 a,node1 b){
return a.order<b.order;
}
int main(){
int st,n,k;
cin>>st>>n>>k;
for(int i=1;i<=n;i++){
int a;
cin>>a;
node[a].address=a;
cin>>node[a].data>>node[a].next;
}
int count=1;
for(int i=0;i<100000;i++)
node[i].order=inf;
for(int i=st;i!=-1;i=node[i].next){
node[i].order=count++;
}
sort(node,node+100000,cmp);
int num=count-1,temp=k;
int turn=1;
for(int i=0;i<count-1;i++){
if(num>k&&temp>0&&turn==1){
printf("%05d %d %05d\n",node[temp-1].address,node[temp-1].data,node[num-1].address);
temp--;
turn=0;
}
else if(num>k&&temp>0&&turn==0){
printf("%05d %d %05d\n",node[num-1].address,node[num-1].data,node[temp-1].address);
num--;
turn=1;
}
else if(num==k&&temp!=0){
if(temp!=1)
printf("%05d %d %05d\n",node[temp-1].address,node[temp-1].data,node[temp-2].address);
else
printf("%05d %d -1\n",node[temp-1].address,node[temp-1].data);
temp--;
}
else{
if(num!=k+1)
printf("%05d %d %05d\n",node[num-1].address,node[num-1].data,node[num-2].address);
else
printf("%05d %d -1\n",node[num-1].address,node[num-1].data);
num--;
}
}
}