AcWing 1538. 链表排序
原题链接
简单
作者:
og_
,
2020-06-04 18:57:39
,
所有人可见
,
阅读 598
C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
struct node1{
int data;
int next;
int add;
int flag=0;
}temp[N],node[N];
bool cmp(node1 a,node1 b){
if(a.flag == 0 || b.flag ==0) return a.flag>b.flag;
return a.data<b.data;
}
int main(){
int n,add;
cin>>n>>add;
int data,a1,a2;
for(int i=0;i<n;i++){
cin>>a1>>data>>a2;
temp[a1].data = data;
temp[a1].next = a2;
temp[a1].add = a1;
}
int sum=0;
for(int i = add ; i!=-1;i=temp[i].next){
sum++;
temp[i].flag = 1;
}
sort(temp,temp+N,cmp);
if(sum==0) cout<<"0 -1";
else{
printf("%d %05d\n",sum,temp[0].add);
for(int i =0 ; i<sum;i++){
if(i!=sum-1){
printf("%05d %d %05d\n",temp[i].add,temp[i].data,temp[i+1].add);
}
else printf("%05d %d -1\n",temp[i].add,temp[i].data,temp[i+1].add);
}
}
}