1052. Linked List Sorting (25)

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct node
{
	int address, key, next;
}nod[100000];

int cmp(node n1, node n2)
{
	return n1.key < n2.key;
}

int main()
{
	int n, begin;
	scanf("%d%d", &n, &begin);

	int i;
	node cur;

	for(i = 1; i <= n; i++)
	{
		scanf("%d%d%d", &cur.address, &cur.key, &cur.next);
		nod[cur.address] = cur;
	}

	vector<node> v;
	while(begin != -1)
	{
		cur = nod[begin];
		v.push_back(cur);
		begin = cur.next;
	}

	int size = v.size();
	if(size == 0)
	{
		printf("%d -1\n", size);
		return 0;
	}

	sort(v.begin(), v.end(), cmp);

	printf("%d %05d\n", size, v[0].address);

	for(i = 0; i < size; i++)
	{
		printf("%05d %d ", v[i].address, v[i].key);

		if(i + 1 < size)
		{
			printf("%05d\n", v[i + 1].address);
		}
		else
		{
			printf("-1\n");
		}
	}

	system("pause");
	return 0;
}

 

posted on 2025-11-23 17:09  王景迁  阅读(0)  评论(0)    收藏  举报

导航