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;
}
时间: 2025-01-07 22:29:15

PAT (Advanced Level) 1052. Linked List Sorting (25)的相关文章

【PAT甲级】1052 Linked List Sorting (25分)

1052 Linked List Sorting (25分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, yo

【PAT甲级】1052 Linked List Sorting (25 分)

题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组成.以头结点地址开始的这条链表以值排序后得到的链表的长度和头结点,接着以升序按行输出每个结点的地址和值以及指向下一个结点的地址. trick: 题干说的postive N可是数据点4出现了N==0的数据,有些不解如果N==0应该包含的话不应该用nonnegative N吗... 数据点1包含有些结点并非在

PAT 1052. Linked List Sorting (25)

1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, yo

PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, y

PAT Advanced 1052 Linked List Sorting (25) [链表]

题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the s

PAT甲题题解-1052. Linked List Sorting (25)-排序

三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include <algorithm> #include <cstdio> #include <string.h> using namespace std; const int maxn=100000+5; struct Node{ int addr; int val; int to; bo

PAT:1052. Linked List Sorting (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int address; int data; int next; bool tag; }Node[100066]; bool cmp(node a,node b) { if(a.tag!=b.tag) return a.tag>b.tag; else return a.data<

1052. Linked List Sorting (25)

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the stru

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any