PATA1052题解

注意:要去除无效结点

//
//  main.cpp
//  PATA1052
//
//  Created by Phoenix on 2018/2/14.
//  Copyright © 2018年 Phoenix. All rights reserved.
//

#include 
#include 
#include 
using namespace std;
const int maxn = 100010;

struct Node{
    int address;
    int data;
    int next;
    int flag;
}node[maxn];

bool cmp(Node a, Node b) {
    if(a.flag != b.flag) return a.flag > b.flag;
    else return a.data < b.data;
}

int main(int argc, const char * argv[]) {
    int n, begin;
    scanf("%d %d", &n, &begin);
    for(int i = 0; i < maxn; i++) {
        node[i].flag = 0;
    }
    for(int i = 0; i < n; i++) {
        int address;
        scanf("%d", &address);
        node[address].address = address;
        scanf("%d %d", &node[address].data, &node[address].next);
    }
    int num = 0;
    while(begin != -1) {
        node[begin].flag = 1;
        begin = node[begin].next;
        num++;
    }
    if(num == 0) printf("0 -1");
    else{
    sort(node, node + maxn, cmp);
    printf("%d %05d\n", num, node[0].address);
    for(int i = 0; i < num - 1; i++) {
        printf("%05d %d %05d\n", node[i].address, node[i].data, node[i + 1].address);
    }
    printf("%05d %d -1\n", node[num - 1].address, node[num - 1].data);
    }
    return 0;
}


你可能感兴趣的:(PAT)