#include<stdio.h>
#include<algorithm>
using namespace std;
struct pnode{
int address;
int data;
int next;
int order;
}node[100010];
bool cmp(pnode a,pnode b){
return a.order<b.order;
}
int main(){
int i, j, head1, n, k, tp1, tp2, c;
for(i=0;i<100010;i++)node[i].order=100010;
scanf("%d%d%d",&head1,&n,&k);
for(i=0;i<n;i++){ // 输入
scanf("%d%d%d",&tp1,&c,&tp2);
node[tp1].next=tp2; node[tp1].address=tp1;
node[tp1].data=c;
} // 输入
int p=head1, count=0;
while(p!=-1){
node[p].order=count++;
p=node[p].next;
}
n=count;
sort(node,node+100010,cmp);
// 输出
for(i=0;i<n/k;i++){
for(j=(i+1)*k-1;j>i*k;j--)
printf("%05d %d %05d\n",node[j].address,node[j].data,node[j-1].address);
printf("%05d %d ",node[i*k].address,node[i*k].data);
if(i<n/k-1)printf("%05d\n",node[(i+2)*k-1].address);
else {
if(n%k==0)printf("-1\n");
else{
printf("%05d\n",node[(i+1)*k].address);
for(int ii=n/k*k;ii<n;ii++){
printf("%05d %d ",node[ii].address,node[ii].data);
if(ii<n-1)printf("%05d\n",node[ii+1].address);
else printf("-1\n");
}
}
}
}
return 0;
}
PAT-B1025
最新推荐文章于 2024-07-18 12:01:28 发布