PAT (Advanced Level) 1052. Linked List Sorting (25)

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1052. Linked List Sorting (25)相关的知识,希望对你有一定的参考价值。

简单题。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<stack>
#include<vector>
using namespace std;

const int maxn=100000+10;
int n,h;
struct X
{
    int a,b,c;
} node[maxn],p[maxn];
int f[maxn];

bool cmp(const X&a,const X&b)
{
    return a.b<b.b;
}

int main()
{
    memset(f,0,sizeof f);
    scanf("%d%d",&n,&h);

    for(int i=1; i<=n; i++)
    {
        scanf("%d%d%d",&node[i].a,&node[i].b,&node[i].c);
        f[node[i].a]=i;
    }

    if(h==-1)printf("0 -1\n");
    else
    {
        int now=f[h];
        int x=0;
        while(1)
        {
            p[x++]=node[now];
            if(node[now].c==-1) break;
            now=f[node[now].c];
        }

        if(x==0)
        {
            if(h==-1) printf("0 -1\n");
            else printf("0 %05d\n",h);
        }

        else
        {
            sort(p,p+x,cmp);
            h=p[0].a;
            printf("%d %05d\n",x,h);
            for(int i=0; i<x; i++)
            {
                if(i<x-1) printf("%05d %d %05d\n",p[i].a,p[i].b,p[i+1].a);
                else printf("%05d %d -1\n",p[i].a,p[i].b);
            }
        }
    }
    return 0;
}

以上是关于PAT (Advanced Level) 1052. Linked List Sorting (25)的主要内容,如果未能解决你的问题,请参考以下文章

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095

PAT Advanced Level 1038