题目链接
代码
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
int
end[100010][2],
input[100010][2] ,
first,
end_first,
pri,
N,K,count,
temp_num;
char temp[2][10];
for(int i = 0;i<100010;i++){
end[i][0] = end[i][1] = input[i][0] = input[i][1] = -1;
}
scanf("%s %d %d",temp[0],&N,&K);
first = atoi(temp[0]);
for (int i = 0; i < N;i++){
scanf("%s %d %s", temp[0], &temp_num, temp[1]);
input[atoi(temp[0])][0] = temp_num;
input[atoi(temp[0])][1] = atoi(temp[1]);
}
int now, end_first_flag = 0;
now = 100008;
pri = -1;
input[100008][1] = first;
count = 0;
do{
now = input[now][1];
if (input[now][0] < 0){
count++;
end[now][0] = input[now][0];
end[now][1] = -1;
if (pri != -1)
end[pri][1] = now;
pri = now;
if (!end_first_flag){
end_first_flag = 1;
end_first = now;
}
}
} while (input[now][1] != -1 );
now = 100008;
do{
now = input[now][1];
if (input[now][0] >= 0 && input[now][0] <= K){
count++;
end[now][0] = input[now][0];
end[now][1] = -1;
if (pri != -1)
end[pri][1] = now;
pri = now;
if (!end_first_flag){
end_first_flag = 1;
end_first = now;
}
}
} while (input[now][1] != -1);
now = 100008;
do{
now = input[now][1];
if (input[now][0] > K){
count++;
end[now][0] = input[now][0];
end[now][1] = -1;
if (pri != -1)
end[pri][1] = now;
pri = now;
if (!end_first_flag){
end_first_flag = 1;
end_first = now;
}
}
} while (input[now][1] != -1);
now = 100008;
end[100008][1] = end_first;
do{
now = end[now][1];
printf("%05d %d ",now,end[now][0]);
if (end[now][1] == -1)
printf("-1");
else
printf("%05d\n",end[now][1]);
} while (end[now][1] != -1);
return 0;
}
上面是我的渣渣实现,下面给出一个跟我思路大体相同的一个比较好看的实现。