1052. Linked List Sorting

小心一个item和没有item的case

#include<deque>
#include<algorithm>
using namespace std;
const int N=100005;
struct node
{
    int addr,data,next;
    node(int a,int b,int c):addr(a),data(b),next(c){}
    bool operator<(const node&x)const{return data<x.data;}
};
node *so[N];
deque<node>sr;
int main()
{
    int n,start;
    scanf("%d%d",&n,&start);
    for(int a,b,c,i=0;i<n;++i)
    {
        scanf("%d%d%d",&a,&b,&c);
        so[a]=new node(a,b,c);
    }
    if(start!=-1)
        do sr.push_back(*so[start]);
             while((start=so[start]->next)!=-1);
    if(sr.empty())
        printf("0 -1");
    else
    {
        sort(sr.begin(),sr.end());
        printf("%d %05d\n",sr.size(),sr[0].addr);
        int i;
        for(i=0;i<sr.size();++i)
        {
            printf("%05d %d",sr[i].addr,sr[i].data);
            if(i==(sr.size()-1))  printf(" -1\n");
            else printf(" %05d\n",sr[i+1].addr);
        }
    }
    return 0;
}


你可能感兴趣的:(1052. Linked List Sorting)