[PAT] Class 1052 Linked List Sorting (25 points)

Meaning of the questions:

Enter a positive integer N (<= 100000), and a node address of the head of the list. Then N input lines, each comprising a value (-1e5 ~ 1e5) address of a node, the node is stored, the address pointing to the next node. Address by five leading zeros contains a positive integer components. A head node address list start linked list of this length and the first node after a value obtained by sorting, in ascending order followed by the address of the next output row address of each node and values ​​and points to a node.

trick:

Casual working say postive N 4 data points but there have been data N == 0, some puzzled if N == 0 should be included, then it should not be used nonnegative N. . .

1 comprises a data point is not on some node in the first node where this list. (Said to a casual working Unicom's list, there may be some practical points are not on the list)

Some of the problems seen by some trick is not obvious given the casual working in the stem, only to ensure that some cases will not happen, there are some boundary conditions and problems that may arise on their own need to give more consideration. . . . .

Code:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
pair<int,int>pr[100007];
pair<int,int>ans[100007];
int main(){
int n;
int add;
scanf("%d%d",&n,&add);
for(int i=1;i<=n;++i){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
pr[x]={y,z};
}
int cnt=0;
while(1){
if(pr[add].second){
ans[++cnt].first=pr[add].first;
ans[cnt].second=add;
add=pr[add].second;
}
else
break;
}
if(!cnt)
printf("0 -1");
else{
sort(ans+1,ans+1+cnt);
printf("% D% 05d \ n ", cnt, years [1] .SECOND);
for(int i=1;i<=cnt;++i){
printf("%05d %d ",ans[i].second,ans[i].first);
if(i!=cnt)
printf("%05d\n",ans[i+1].second);
else
printf("-1");
}
}
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11619280.html