Qn: Let u and v be two distinct vertices in a di...
Erstellt am: 23. Juni 2025
Beantwortet mit O4 Mini High von Chat01
Erstellt am: 23. Juni 2025
Beantwortet mit O4 Mini High von Chat01
Qn:
Let u
and v
be two distinct vertices in a directed graph. Let's call the ordered pair (u,v)
good if there exists a path from vertex u
to vertex v
along the edges of the graph.
You are given an undirected tree with n
vertices and n−1
edges. Determine whether it is possible to assign a direction to each edge of this tree so that the number of good pairs in it is exactly n
. If it is possible, print any way to direct the edges resulting in exactly n
good pairs.
Input
The first line contains one integer t
(1≤t≤104
) — the number of test cases.
The first line of each test case contains one integer n
(2≤n≤2⋅105
) — the number of vertices in the tree.
The next n−1
lines describe the edges. The i
-th line contains two integers ui
and vi
(1≤ui,vi≤n
; ui≠vi
) — the vertices connected by the i
-th edge.
It is guaranteed that the edges in each test case form an undirected tree and that the sum of n
over all test cases does not exceed 2⋅105
.
Output
For each test case, print "NO" (case-insensitive) if it is impossible to direct all edges of the tree and obtain exactly n
good pairs of vertices.
Otherwise, print "YES" (case-insensitive) and then print n−1
pairs of integers ui
and vi
separated by spaces — the edges directed from ui
to vi
.
The edges can be printed in any order. If there are multiple answers, output any.
hint:
Hint 1: Prevent extra 2-step reachability by alternating edge directions
When you DFS from each root (u or w) and skip the spine, flip the orientation at every depth parity. This stops any two consecutive edges from lining up in the same direction outside the main spine, so no unintended length-2 paths appear.
Because it’s a tree and you never revisit the parent, this pattern guarantees each non-spine edge contributes exactly one reachable pair and no more.
Hint 2: Use the unique degree-2 “center” to isolate the single long path
Selecting a vertex of degree exactly 2 splits the tree into two subtrees plus a 3-node spine. Directing u→center→w gives you exactly one transitive pair (u→w) in addition to the n–1 edge pairs.
If no vertex has degree 2 (e.g. a star or all degrees ≠2), you cannot restrict yourself to just one length-2 reachable path, so the answer must be NO.
code in java
Here’s a Java solution that runs in per test and uses two rooted DFS’s from the degree-2 “center” to orient all non-spine edges in an alternating fashion so that no extra length-2 (or longer) reachability appears outside the single spine path:
javaimport java.io.*; import java.util.*; public class Main { static class Edge { int u, v; Edge(int u, int v){ this.u = u; this.v = v; } } static int n; static List<Integer>[] adj; static List<Edge> ans; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(in.readLine().trim()); while (t-- > 0) { n = Integer.parseInt(in.readLine().trim()); adj = new ArrayList[n+1]; for (int i = 1; i <= n; i++) adj[i] = new ArrayList<>(); for (int i = 0; i < n-1; i++) { String[] uv = in.readLine().split(" "); int u = Integer.parseInt(uv[0]), v = Integer.parseInt(uv[1]); adj[u].add(v); adj[v].add(u); } // find a node of degree 2 int center = -1; for (int i = 1; i <= n; i++) { if (adj[i].size() == 2) { center = i; break; } } if (center == -1) { out.println("NO"); continue; } // the two neighbors on the spine int a = adj[center].get(0); int b = adj[center].get(1); ans = new ArrayList<>(n-1); // orient the spine: a -> center -> b ans.add(new Edge(a, center)); ans.add(new Edge(center, b)); // DFS from a, skipping center dfsOrient(a, center); // DFS from b, skipping center dfsOrient(b, center); out.println("YES"); StringBuilder sb = new StringBuilder(); for (Edge e : ans) { sb.append(e.u).append(' ').append(e.v).append('\n'); } out.print(sb); } out.flush(); } // iterative DFS from root, skipping the given 'block' parent on first step, // orienting edges in alternating fashion: at odd depth, child->parent; even depth, parent->child static void dfsOrient(int root, int block) { Deque<int[]> stack = new ArrayDeque<>(); // push all true children of root (i.e. neighbors ≠ block) at depth=1 for (int nei : adj[root]) { if (nei == block) continue; stack.push(new int[]{nei, root, 1}); } while (!stack.isEmpty()) { int[] cur = stack.pop(); int u = cur[0], p = cur[1], d = cur[2]; // orient edge (p,u) if ((d & 1) == 1) { // odd depth → child->parent ans.add(new Edge(u, p)); } else { // even depth → parent->child ans.add(new Edge(p, u)); } // recurse for (int v : adj[u]) { if (v == p) continue; stack.push(new int[]{v, u, d+1}); } } } }
Explanation of the key steps:
Find a “center” vertex of degree 2.
If no vertex has degree exactly 2, you can’t create exactly one extra length-2 reachable pair beyond the direct edges, so the answer is NO.
Spine orientation.
Let the two neighbors of the center be and . Orient the unique 3-node spine
This yields exactly one extra reachable pair .
Orient each non-spine subtree by alternating edge‐directions.
Together, you get exactly from the edges themselves plus the single through the spine, for a total of good pairs.