

- #include <cstdio>
- const int maxn = 100010;
- struct Node{
- int address, data, next;
- }nodes[maxn];
- int main() {
- int head,n,k;
- scanf("%d%d%d", &head, &n, &k);
- int add;
- for(int i = 0; i < n; i++){
- scanf("%d", &add);
- scanf("%d%d", &nodes[add].data, &nodes[add].next);
- nodes[add].address = add;
- }
- //有效节点数目
- int v = head;
- int num = 1;
- while(nodes[v].next != -1){
- num++;
- v = nodes[v].next;
- }
-
- int heads[maxn];
- int tails[maxn];
-
- int next;
- int pre = head;
- int cur = nodes[head].next;
- int curHead = head;
- int times = num / k;
- for(int i = 0; i < times; i++){
- tails[i] = curHead;
- int s = 1;
- while(s < k){
- next = nodes[cur].next;
- nodes[cur].next = pre;
- pre = cur;
- cur = next;
- s++;
- }
- heads[i] = pre;
- curHead = cur;
- pre = cur;
- cur = nodes[cur].next;
- }
- nodes[tails[times-1]].next = curHead;
- for(int i = 0; i < times-1; i++){
- nodes[tails[i]].next = nodes[heads[i+1]].address;
- }
- int ans = heads[0];
- while(nodes[ans].next != -1){
- printf("%05d %d %05d\n", nodes[ans].address, nodes[ans].data, nodes[ans].next);
- ans = nodes[ans].next;
- }
- printf("%05d %d -1\n", nodes[ans].address, nodes[ans].data);
- return 0;
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |