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 50 51 52 53 54 55 56 57 58
| #include <cstdio> #include <algorithm> using namespace std; const int N=1e5+10; struct Node{ int address,data,next,order; }node[N]; bool cmp(Node& a,Node& b){ return a.order<b.order; }
int main(){ for(int i=0;i<N;i++){ node[i].order=N; } int head,n,k,addr; scanf("%d%d%d",&head,&n,&k); for(int i=0;i<n;i++){ scanf("%d",&addr); scanf("%d%d",&node[addr].data,&node[addr].next); node[addr].address=addr; } int p=head,cnt=0; while(p!=-1){ node[p].order=cnt++; p=node[p].next; } sort(node,node+N,cmp); n=cnt; for(int i=0;i<n/k;i++){ for(int 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 i=n/k*k;i<n;i++){ if(i<n-1) printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address); else printf("%05d %d -1\n",node[i].address,node[i].data); } } } }
return 0; }
|