#include<iostream>
#include<vector>
using namespace std;
#define maxn 100000
struct node {
int address;
int next;
int data;
};
node info[maxn];
int main() {
int root, N, K;
cin >> root >> N >> K;
for (int i = 1; i <= N; i++) {
int a1, a2, d;
scanf("%05d %d %05d", &a1, &d, &a2);
info[a1].next = a2;
info[a1].data = d;
info[a1].address = a1;
}
vector<node>temp;
vector<node>result;
int count = 0;
while (root != -1) {
temp.push_back(info[root]);
count++;
root = info[root].next;
if (count == K) {
count = 0;
for (int i = 0; i < K; i++)
result.push_back(temp[K - 1 - i]);
temp.clear();
}
}
if (temp.size() != 0) {
result.insert(result.end(), temp.begin(), temp.end());
}
for (int i = 0; i < result.size(); i++) {
printf("%05d %d ", result[i].address, result[i].data);
if (i != result.size() - 1)
printf("%05d\n", result[i + 1].address);
else
printf("%d\n", -1);
}
return 0;
}
PAT甲级 1074(C++)
最新推荐文章于 2024-06-07 07:00:00 发布