#P11762. [IAMOI R1] 走亲访友

[IAMOI R1] 走亲访友

Description

Xiao C has nn relatives connected by mm bidirectional roads, ensuring all relatives are mutually reachable.

Xiao C will personally visit them starting from the ss-th relative's home. Each time she can move to a relative's home connected by a road. However, whenever she traverses a road, admirers swarm in causing congestion. She can choose to hide her charm (marking pi=1p_i=1) to prevent congestion, otherwise (marking pi=0p_i=0) the road becomes permanently blocked after this traversal.

Xiao L is bad with directions, so Xiao C wants to leave exactly n1n-1 unblocked roads forming a connected tree, while traversing at most kk roads. Construct such a traversal plan.

Formal Problem Statement

Given a simple undirected connected graph with nn nodes and mm edges, construct a path satisfying:

  1. Starts at node ss (end node unrestricted).
  2. For each traversed edge (ui,vi)(u_i, v_i), assign pi{0,1}p_i \in \{0,1\} where:
    • pi=0p_i=0: Delete this edge (cannot be reused).
    • pi=1p_i=1: Keep this edge.
    • Path continuity: ui=vi1u_i = v_{i-1} for i>1i>1 (even if edge is deleted).
  3. Path length ≤ kk.
  4. Remaining edges form a spanning tree.

Multiple traversals allowed before deletion. Solution always exists under given constraints.

Input Format

  • First line: four integers nn, mm, kk, ss.
  • Next mm lines: two integers uu and vv per line, representing a bidirectional road.

Output Format

  • First line: integer tt (number of traversed edges).
  • Next tt lines: two integers per line - edge index (1-based input order) and pip_i (0/1).

Any valid solution is accepted.

5 5 10 4
1 3
2 5
2 3
4 5
1 5
2
4 1
2 0

Hint

After traversing edge 4 (keep) to node 5, then edge 2 (block), remaining edges form a tree. Alternative solutions like

2
4 1
5 0

or

3
4 1
2 1
3 0

are also valid.

Constraints

Subtask scoring

Subtask nn \le mm k=k= Score
1 10 ≤10 100 20
2 100 n(n1)2\frac{n(n-1)}{2} 10610^6 10
3 10310^3 nn n+mn+m
4 n(n1)2\frac{n(n-1)}{2} n2n^2 20
5 n+mn+m 40

For 100% data:

  • n1mn(n1)2n-1 \le m \le \frac{n(n-1)}{2}
  • No self-loops or duplicate edges

Postscript: This is a modified version. The original problem remains unsolved. Contact Down_syndrome for ideas.

Translation by DeepSeek R1