#include<stdio.h>
typedef struct NODE
{
int address;
int data;
int next;
}Node;
Node orign[100001], sort[100001], result[100001];
int main()
{
int addr, N, K;
scanf("%d %d %d", &addr, &N, &K);
int i, j;
for(i=0; i<N; i++)
{
Node temp;
scanf("%d %d %d", &temp.address, &temp.data, &temp.next);
orign[temp.address] = temp;
}
for(i=0; i<N; i++)
{
sort[i] = orign[addr];
addr = sort[i].next;
if(addr == -1)
{
N = i+1;
break;
}
}
for(i=0; i<N/K; i++)
{
for(j=0; j<K/2; j++)
{
Node temp;
temp = sort[j+i*K];
sort[j+i*K] = sort[K+i*K-j-1];
sort[K+i*K-j-1] = temp;
}
}
for (i = 0; i < N; i++) {
if(i != N - 1) {
sort[i].next = sort[i+1].address;
printf("%05d %d %05d\n", sort[i].address, sort[i].data, sort[i].next);
} else {
sort[i].next = -1;
printf("%05d %d %d\n", sort[i].address, sort[i].data, sort[i].next);
}
}
}
网友评论