PAT甲级1052

#include 

using namespace std;
const int maxn = 1e5+2;

struct node{
    int addr;
    int key;
    int next;
    friend bool operator < (node a, node b){
        return a.key > b.key;
    }
};

node data[maxn];

int main()
{
    int N, firAddr;
    scanf("%d%d", &N, &firAddr);
    for(int i = 0; i < N; ++i){
        int d;
        scanf("%d", &d);
        data[d].addr = d;
        scanf("%d%d", &data[d].key, &data[d].next);
    }
    if(firAddr == -1){
        printf("0 -1\n");
        return 0;
    }
    priority_queue pq;
    int i;
    for(i = firAddr; data[i].next!=-1; i = data[i].next){
        pq.push(data[i]);
    }
    pq.push(data[i]);
    printf("%d %05d\n", pq.size(), pq.top().addr);
    while(!pq.empty()){
        printf("%05d %d", pq.top().addr, pq.top().key);
        pq.pop();
        if(pq.empty()) printf(" -1\n");
        else printf(" %05d\n", pq.top().addr);
    }
    return 0;
}

/*
特例1:所给起始地址并非真正链表起始地址
5 22222
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

特例2:所给起始地址是NULL,即-1
5 -1
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345
*/

你可能感兴趣的:(PAT甲级题目)