然鹅我的方法很愚蠢
#include<bits/stdc++.h>
using namespace std;
int data_[100000]= {0},next_[100000]= {0};
set<pair<int,int>> s1,s2,s3; //负数 //小于等于k的正数,0 //大于k的数
int main() {
int N,K,add1,a,d,n,cnt1=0,cnt2=0;
cin>>add1>>N>>K;
for(int i=0; i<N; i++) {
cin>>a>>d>>n;
data_[a]=d,next_[a]=n;
}
int *p=&add1,i=0;
while(*p!=-1) { //遍历链表
if(data_[*p]<0) s1.insert({i,*p});
else if(data_[*p]>=0&&data_[*p]<=K) s2.insert({i,*p});
else s3.insert({i,*p});
p=&next_[*p]; //下一个位置
i++;
}
for(auto it=s1.begin(); it!=s1.end(); it++) {
if(it!=s1.begin()) printf(" %05d\n",it->second);
printf("%05d %d",it->second,data_[it->second]);
}
if(s1.size()&&s2.size())
printf(" %05d\n",s2.begin()->second);
for(auto it=s2.begin(); it!=s2.end(); it++) {
if(it!=s2.begin()) printf(" %05d\n",it->second);
printf("%05d %d",it->second,data_[it->second]);
}
if(s2.size()&&s3.size()||s1.size()&&s3.size())
printf(" %05d\n",s3.begin()->second);
for(auto it=s3.begin(); it!=s3.end(); it++) {
if(it!=s3.begin()) printf(" %05d\n",it->second);
printf("%05d %d",it->second,data_[it->second]);
}
cout<<" -1";
return 0;
}