AcWing 1538. 链表排序
原题链接
简单
作者:
sherlook
,
2020-08-28 10:49:46
,
所有人可见
,
阅读 662
#include <iostream>
#include <map>
using namespace std;
const int N = 100000;
struct node{
int key;
int next;
}nodes[N];
int n,st;
// key n,next
map<int,pair<int,int>> M;
int main(){
scanf("%d%d",&n,&st);
for(int i=0; i<n; i++){
int p,k,np;
scanf("%d%d%d",&p,&k,&np);
nodes[p] = {k,np};
}
for(int i=st; i!=-1; i=nodes[i].next){
pair<int, int> pii = {i,nodes[i].next};
M[nodes[i].key] = pii;
}
cout <<M.size()<<" ";
auto i = M.begin();
for(auto i = M.begin(); i!=M.end(); i++){
//换一种奇怪的输出方式
printf("%05d\n%05d %d ",i->second.first,(i)->second.first,i->first);
}
cout <<"-1";
}