-
Notifications
You must be signed in to change notification settings - Fork 0
/
1025.cpp
64 lines (58 loc) · 1.12 KB
/
1025.cpp
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
59
60
61
62
63
64
#include <iostream>
#include <vector>
using namespace std;
struct node
{
int add;
int data;
int nadd;
};
int main()
{
int fadd;
int n,k;
vector<node>a(100000);
vector<node> b;
vector<node> c;
node temp;
cin >> fadd >> n >> k;
for (int i=0; i < n; i++)
{
cin >> temp.add >> temp.data >> temp.nadd;
a[temp.add] = temp;
}
if (fadd == -1)
{
cout << "-1" << endl;
}
else
{
int nextadd = fadd;
while (nextadd != -1)
{
b.push_back(a[nextadd]);
nextadd = a[nextadd].nadd;
}
int N2 = b.size();
int rdx = k - 1;
while (rdx < N2)
{
for (int i = rdx; i > rdx - k; i--)
{
c.push_back(b[i]);
}
rdx += k;
}
for (int i = rdx - k + 1; i < N2; i++)
{
c.push_back(b[i]);
}
for (int i = 0; i < N2 - 1; i++)
{
c[i].nadd = c[i+1].add;
printf("%05d %d %05d\n", c[i].add, c[i].data, c[i].nadd);
}
printf("%05d %d %d", c[N2 - 1].add, c[N2 - 1].data, -1);
}
return 0;
}