PAT 1052

题目 : PLinked List Sorting

分值 : 25
难度 : 中等题
思路 : 自定义结构体题目
坑点 : 首先它不是全部读入的节点都有用的 其次他要你输出的是有效的链表的长度和头
评语 : 别读进来整体排序,这样是不对的,读进来,它以s开头然后到-1你自己挖一个链表出来

具体代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct Node{
int self ;
int value ;
int next ;
}Nodes;
Nodes data[100001];
Nodes result[100001];
int cur = 0 ;
bool cmp(Node a , Node b )
{
return a.value < b.value ;
}
int main() {
int N ;
int s ;
string temp ;
cin >> N >> s ;
if(s==-1)
{
cout <<"0 -1"<<endl ;
return 0 ;
}
int v1 ,v2 ,v3 ;
for(int i = 0 ; i< N ; i++ )
{
cin>> v1 >> v2 >>v3 ;
data[v1].self = v1 ;
data[v1].value =v2 ;
data[v1].next = v3 ;
}
while(s!=-1)
{
result[cur++]= data[s];
s =data[s].next ;
}
sort(result, result+cur ,cmp) ;
printf("%d %05d\n",cur , result[0].self);
for(int i = 0 ; i< cur ; i++)
{
if(i)
printf("%05d\n", result[i].self) ;
printf("%05d %d ",result[i].self,result[i].value);
}
cout <<"-1"<<endl ;
return 0;
}