Domain: Algorithms & Data Structures

A miner is searching a rooted underground shaft system that forms a **tree** of `n` chambers (nodes) connected by one-way-less tunnels (undirected edges). A single phantom hides in exactly one chamber; initially it may be in any chamber. The miner can perform two kinds of actions. After each action, the phantom may (or may not) move to one adjacent chamber, except it is forbidden to move into the chamber that the miner just inspected in that same action.

Available actions (each targets a single chamber `x`):

- `1 x` — **Inspect** chamber `x`. If the phantom is currently in `x`, the miner catches it and the process ends. Otherwise the phantom may move to any adjacent chamber except `x`.
- `2 x` — **Seal** chamber `x`: destroy every tunnel incident to `x`. After sealing, the phantom cannot use those destroyed tunnels. Then the phantom may move to one adjacent chamber (along any remaining tunnels).

Design a single strategy — i.e., an explicit sequence of actions — that guarantees the miner will catch the phantom regardless of its initial position and its choices of moves. Your sequence must contain at most `⌊5n/4⌋` actions.

## Input

The input contains multiple test cases. The first line contains a single integer `t` (`1 ≤ t ≤ 10^4`) — the number of test cases. The sum of `n` over all test cases does not exceed `2·10^5`.

Each test case has the following format:

- A line with an integer `n` (`2 ≤ n ≤ 2·10^5`) — the number of chambers.
- Then `n-1` lines follow, each with two integers `u` and `v` (`1 ≤ u, v ≤ n`), indicating an undirected tunnel between chambers `u` and `v`. The tunnels form a tree.

## Output

For each test case, output a sequence of actions that will surely catch the phantom.

First print a single integer `k` (`1 ≤ k ≤ ⌊5n/4⌋`) — the number of actions in your strategy.

Then print `k` lines; the `i`-th such line should contain two integers `ti` and `xi`:

- `ti` is either `1` or `2`, specifying the action type (inspect or seal).
- `xi` is the target chamber index (`1 ≤ xi ≤ n`).

If the miner inspects a chamber and the phantom happens to be there, the process stops immediately (no further actions are performed). Your printed sequence should therefore be a valid plan that would succeed in every possible adversarial scenario (the grader will verify your action list satisfies the bound and would lead to capture irrespective of phantom moves).

## Constraints summary

- `1 ≤ t ≤ 10^4`
- `2 ≤ n ≤ 2·10^5`
- Sum of `n` across all test cases ≤ `2·10^5`
- Output sequence length per test case must satisfy `k ≤ ⌊5n/4⌋`

## Notes

- You do **not** need to minimize `k`, only ensure `k ≤ ⌊5n/4⌋` and that the strategy guarantees capture.
- The judge will simulate the worst-case adversarial phantom behavior to check your plan.

Grading rubric:
criterion: Does the solution include a postorder coloring procedure that specifies how to label each node as 'green', 'yellow', or 'black' using only information from its children?
weight: 10
criterion: Does the solution assert that after removing every node labeled 'black', every connected component of the remaining graph is a simple path (every node has residual degree ≤ 2)?
weight: 10
criterion: Does the solution state that groups associated with different black nodes are pairwise disjoint?
weight: 10
criterion: Does the solution derive the inequality n ≥ 4·b where b denotes the number of black nodes?
weight: 10
criterion: Does the solution state that when a node is colored black because it has three or more green children, it is grouped with three distinct green-subtree endpoint vertices (i.e., the solution explicitly assigns three distinct green vertices to that black node)?
weight: 10
criterion: Does the solution state that the worst-case total number of operations equals n + b and then conclude this value is ≤ ⌊5n/4⌋?
weight: 10
criterion: Does the solution state the algorithmic resource bounds as O(n) time and O(n) memory for producing the operation sequence?
weight: 10
criterion: Does the solution explicitly state that all type-2 (cut) operations are performed before any type-1 (inspect) operations?
weight: 10
criterion: Does the solution describe the sweep procedure for a path as performing exactly one type-1 operation per node while checking nodes in sequence from an endpoint to the other endpoint?
weight: 10
criterion: Does the solution explicitly justify that moving all type-2 (seal) operations earlier cannot reduce correctness because sealing only restricts the phantom’s movement options?
weight: 10

Answer:
**Idea in one line.**  
Cut a carefully chosen small set of nodes so the remaining connected pieces are simple paths, then “sweep” each path by checking its nodes from one end to the other. Do all cuts first (they only restrict the phantom), then do the sweeps. The number of cuts is at most `⌊n/4⌋`, so the total number of actions (cuts + checks) is `≤ n + ⌊n/4⌋ = ⌊5n/4⌋`.

---

## Full algorithm (constructive)

1. **Root the tree** at node `1`. We will do a single postorder DFS to compute one of three colors for every node: **green**, **yellow**, or **black**.

2. **Coloring rule (postorder):** for each node `v`, let `G` = number of children of `v` colored **green**, and let `Y` = whether `v` has any child colored **yellow**.
   - If `G ≥ 3` **or** `Y = true`, color `v` **black**.
   - Else if `G = 2` (and `Y = false`), color `v` **yellow**.
   - Else (i.e., `G ≤ 1` and `Y = false`), color `v` **green**.

   Compute these colors by visiting children first (postorder), so each child’s color is known when we decide the parent’s color.

3. **Perform all cuts.** For every node `v` colored **black**, output operation `2 v` (destroy all edges incident to `v`). Do these `2`-operations first (in any order).

   After cutting every black node, every connected component of the remaining graph is a simple path (this follows from how colors were assigned — nodes with degree ≥3 in the leftover graph were prevented by the coloring).

4. **Sweep each path.** For each connected component (which is now a path of length `k` nodes), choose one endpoint and perform `1 x` operations along the path from that endpoint to the other endpoint (i.e., check first node, then next, …, last), listing `k` operations for that path. Do these sweeps after finishing all `2`-operations.

   If any `1 x` finds the phantom, the process stops early (the plan is still valid since we listed a complete schedule for the adversary-free guarantee).

---

## Why this works

- **Doing all `2` first is safe.** Cutting edges only reduces the phantom’s possible moves. Sealing a node only deletes tunnels, so it can only reduce the phantom’s set of legal moves and never creates new moves; therefore performing type-2 operations earlier cannot make capture harder. Hence we may assume all `2`-operations come before any `1`-operation.

- **After the cuts every component is a path.** The coloring rule forbids any node (in the uncut graph) from having three or more “green” child-subpaths survive in its subtree without itself being black. When we cut out every black node (remove all incident edges), every remaining component has no branching — it must be a path (degree ≤ 2 everywhere). Informally: yellow nodes serve only to join two green child-paths into one longer path; black nodes break branches.

- **Sweeping a path of length `k` with `k` checks works.** If you check nodes along the path from one endpoint to the other using `1`-operations, after `i` checks the phantom cannot be located at any of the first `i` nodes (because if it were, it would be caught at that check; otherwise it could not cross back past the last checked node because edges behind black nodes are removed). By induction this eventually forces capture.

---

## Operation count (bound)

- Let `b` be the number of black nodes produced by the coloring. We perform exactly `b` operations of type `2` (one for each black node).

- After removing edges incident to black nodes, the graph splits into disjoint paths covering the `n` nodes. Sweeping every path requires exactly `n` operations of type `1` in total (each node is checked exactly once in the worst-case adversarial schedule). Thus worst-case total operations = `n + b`.

- **Key combinatorial bound:** The coloring guarantees `b ≤ ⌊n/4⌋`. Sketch of charging argument:
  - Every time a node becomes black because it had `≥ 3` green children, choose exactly three of those green children and charge one distinct green-subtree endpoint from each of the three to that black node.
  - Every time a node becomes black because it had a yellow child, charge the yellow child and its (up to) two green children — again three distinct nodes charged to this black node.
  - The groups associated with different black nodes are pairwise disjoint (no vertex is charged to two different black nodes).
  - Hence each black node accounts for at least 4 distinct vertices (the black node itself + at least 3 charged vertices), so n ≥ 4·b.

- Therefore total operations `n + b ≤ n + ⌊n/4⌋ = ⌊5n/4⌋`, meeting the required bound.

---

## Complexity and implementation notes

- The coloring (postorder DFS) runs in `O(n)` time and uses `O(n)` memory (recursion or an explicit stack).
- Producing the list of cut operations is `O(b)`. After removing black nodes conceptually, extracting the path components and listing sweep orders is `O(n)`.
- Overall time complexity `O(n)` per test case; total across all tests `O(sum n)`.

---

## Summary (what to output)

- First, output `k = n + b` operations (or fewer — the schedule may stop early if the phantom is caught).
- Then print:
  1. `2 v` for every black node `v` (order arbitrary).
  2. For each remaining path component, a sequence `1 x1`, `1 x2`, …, `1 xk` checking nodes along the path from one endpoint to the other endpoint.

This produces a valid plan that always catches the phantom and uses at most `⌊5n/4⌋` operations.