#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
using namespace std;
const int maxn=100010;
struct Node
{
int address;
int key;
int next;
int flag;
}node[maxn];
bool cmp(Node a,Node b)
{
if(a.flag == 0 || b.flag == 0) //只要有一个不在链表上
return a.flag > b.flag; //不再链表的排在后面
else
return a.key < b.key; //按照值的升序排列
}
int main()
{
freopen("1.txt","r",stdin);
int n,head;
scanf("%d %d",&n,&head);
for(int i = 0;i<n;i++)
{
int add,key,next;
scanf("%d%d%d",&add,&key,&next);
node[add].address = add;
node[add].key = key;
node[add].next = next;
}
for(int i = 0;i<=maxn;i++) //初始化链表
node[i].flag = 0;
int countn = 0;
while(head!=-1)
{
node[head].flag = 1; //在链表中
countn++; //链表节点数
head = node[head].next;
}
if(countn == 0) //如果全部是不在链表上的节点
printf("0 -1");
else
{
sort(node,node+maxn,cmp);
printf("%d %05d\n",countn,node[0].address);
for(int i = 0;i<countn;i++)
{
if(i!=countn -1)
printf("%05d %d %05d\n",node[i].address,node[i].key,node[i+1].address);
else
printf("%05d %d -1\n",node[i].address,node[i].key);
}
}
}
1052 Linked List Sorting (25分)
最新推荐文章于 2023-11-29 13:31:35 发布