Reverse in Groups of K

easy linked-list 1000 ms · 256 MB

Reverse in Groups of K

You are given a singly linked list with n nodes. Each node has a distinct id, an integer value, and the id of the next node in the list (or -1 if it is the last node). The nodes are given in arbitrary order, not necessarily the order they appear in the list, so you first have to follow the next pointers from the head to recover the actual sequence.

Walk the list from the head and split it into consecutive groups of k nodes: the first k nodes form the first group, the next k nodes form the second group, and so on. Reverse the order of the nodes inside every group that contains exactly k nodes. If the list length is not a multiple of k, the last group has fewer than k nodes — leave that trailing group exactly as it is, and do not reverse it. The groups keep their original positions along the list; only the order of nodes within each full group changes.

Print the values of the nodes in the resulting list, from head to tail.

Input

The first line contains two integers n and k (1 ≤ n ≤ 2·10^5, 1 ≤ k ≤ n).

Each of the next n lines contains three integers id, value, next describing one node:

  • id (1 ≤ id ≤ n) is the node's id. Every id from 1 to n appears exactly once across these lines.
  • value (-10^9 ≤ value ≤ 10^9) is the node's value.
  • next is the id of the node that follows this one in the list, or -1 if this node is the last one.

These n lines may appear in any order, independent of the list order.

The last line contains one integer head (1 ≤ head ≤ n), the id of the first node of the list.

The given next pointers, starting from head, form a single chain that visits all n nodes exactly once and ends at the node whose next is -1. There are no cycles and no branches.

Output

Print n integers — the values of the list after reversing every full group of k consecutive nodes — separated by single spaces, on one line.

Notes

In the first example the list, in order, has values 10 20 30 40 50 and k = 2. The groups are [10, 20], [30, 40] and [50]. The first two groups are full and get reversed to [20, 10] and [40, 30]; the last group has only one node, so it is left alone. The result is 20 10 40 30 50.

In the second example k equals n, so the whole list is a single full group and gets reversed.

Example 1

Input

5 2
4 50 -1
2 40 4
5 30 2
1 20 5
3 10 1
3

Output

20 10 40 30 50

Example 2

Input

4 4
1 3 3
3 4 -1
4 2 1
2 1 4
2

Output

4 3 2 1

Hints · reveal one at a time if you're stuck

Hint 1

The input is not in list order, so first work out the order. Since the ids are exactly 1 to n, you can store each node's value and next pointer in arrays indexed by id.

Hint 2

Once you have the values in list order, the problem is just about an array: split it into blocks of k and reverse only the blocks that are full. Be careful with the leftover block at the end, since it must stay untouched.

Hint 3

Reverse each full block in place by walking from the block's start to its end, and then move on to the next block boundary. Each element is moved only a constant number of times, so the total work is linear even when k is 1.

report an issue Prefer your own editor? sooks prep 83 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.