#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
using namespace std;
struct node
{
int data;
int now;
int next;
}A[100010];
int N,st,K;
int main()
{
scanf("%d%d%d",&st,&N,&K);
for(int i=0;i<N;i++)
{
int now;
scanf("%d",&now);
scanf("%d %d",&A[now].data,&A[now].next);
A[now].now=now;
}
int head=100001;
A[head].next=-1;
int p=st,pre=head,step=0;
int num=0;
while(p!=-1)
{
num++;
p=A[p].next;
}
int rest=num;
p=st;
while(p!=-1&&rest>=K)
{
int r=A[p].next;
A[p].next=A[pre].next;
A[pre].next=p;
p=r;
step++;
if(step==K)
{
rest-=K;
while(A[pre].next!=-1)
{
pre=A[pre].next;
}
step=0;
}
}
if(rest>0)
{
A[pre].next=p;
}
p=A[head].next;
while(p!=-1)
{
if(A[p].next!=-1)
{
printf("%05d %d %05d\n",p,A[p].data,A[p].next);
}
else
{
printf("%05d %d -1\n",p,A[p].data);
}
p=A[p].next;
}
return 0;
}
PAT甲1074 Reversing Linked List(25 分)
最新推荐文章于 2024-07-28 03:11:06 发布