zju pat 1052

//1052 有些节点并不属于链表,因此单纯排序,会有一个case过不去
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#define MAX_SIZE 100010
using namespace std;
typedef struct Node
{
    int _address;
    int key;
    int next;
    
};

bool cmp(Node n1,Node n2)
{
    return n1.key < n2.key;
}
int main()
{
    int N,i;
    int head_first;
    int _address;
    int next;
    int key;
    while(cin>>N>>head_first)
    {
        vector<Node>vec_s1(MAX_SIZE);
        vector<Node>vec_s2(MAX_SIZE);
        for(i = 0 ;i < N;i++)
        {
            cin>>_address >>key>>next;
            vec_s1[_address]._address = _address; //地址作为索引,方便查找,哈希
            vec_s1[_address].key = key;
            vec_s1[_address].next = next;
        }
        int temp = head_first;
        int top = 0;
        while(temp != -1)  //遍历链表,保存成数组
        {
            vec_s2[top]._address = vec_s1[temp]._address;
            vec_s2[top].key = vec_s1[temp].key;
            vec_s2[top].next = vec_s1[temp].next;
            temp = vec_s1[temp].next;
            top++;
        }
        sort(vec_s2.begin(),vec_s2.begin() + top,cmp);
        if(top == 0)
        {
            printf("0 -1
");
        }
        else{
            printf("%d %05d
",top,vec_s2[0]._address);
            for(i = 0;i < top-1;i++) //按照排好序,连接起来
            {
                printf("%05d %d %05d
",vec_s2[i]._address,vec_s2[i].key,vec_s2[i+1]._address);
            }
            printf("%05d %d -1
",vec_s2[top-1]._address,vec_s2[top-1].key);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/cheng07045406/p/3536350.html