样例(可复制)
00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854
00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1
注意点
- 如果出现段错误一般是数组越界,本人的Map[10100]一开始大小设置时少输入了个0就报这种错
#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
int Map[10100]={0};
struct Node{
int add,data,next,order;
bool flag1,flag2;
Node(){flag1=false;flag2=true;};
}node[maxn];
bool cmp(Node a,Node b){
if(a.flag1&&b.flag1){
if(a.flag2!=b.flag2)return a.flag2<b.flag2;
return a.order<b.order;
}
return a.flag1>b.flag1;
}
int main(){
int start,n,add,data,next,order=0;
cin>>start>>n;
while(n--){
scanf("%d %d %d",&add,&data,&next);
node[add].add=add;
node[add].data=data;
node[add].next=next;
}
for(int p=start;p!=-1;p=node[p].next){
node[p].flag1=true;
node[p].order=order++;
if(Map[abs(node[p].data)]==0){
Map[abs(node[p].data)]=1;
node[p].flag2=false;
}else{
node[p].flag2=true;
}
}
sort(node,node+maxn,cmp);
for(int i=0;i<order;i++){
if(i==order-1){
printf("%05d %d -1\n",node[i].add,node[i].data);
break;
}
if(!node[i].flag2&&node[i+1].flag2)printf("%05d %d -1\n",node[i].add,node[i].data);
else printf("%05d %d %05d\n",node[i].add,node[i].data,node[i+1].add);
}
return 0;
}