We need to read problem: partition edges of a simple connected undirected graph into pairs, each pair must be two edges that share a common vertex (adjacent edges), and each edge appears exactly once. So we are covering all edges with "paths of length 2", which are basically unordered pairs of edges that meet at a vertex. Each such path can be described by three vertices (x,y,z) where edges are (x,y) and (y,z). So essentially we need a perfect matching on the set of edges, where matched edges share a common vertex. Additionally, each edge belongs to exactly one pair. So it's like decomposing the edge set into disjoint "V-shaped" structures (two edges sharing a middle vertex). Each such structure uses two edges and one vertex as the center; the edges incident to that vertex may be used multiple times, but each edge appears only once. The problem: given graph, find such a partition or determine impossible.
We need to output for each pair the triple (x,y,z) where the middle vertex is the common vertex. But note: in the triple, order of x and z maybe matters? The output seems to list three vertices. We can output any order? Usually they want the two edges: (x,y) and (y,z). So y is the middle vertex. So we need to assign each edge to be paired with another edge that shares a vertex.
Let's think about necessary conditions: Since each edge appears exactly once, total number of edges m must be even. That's given? Not necessarily, but if m odd, impossible. So m even is necessary.
Now, we need to find a perfect matching on edges with adjacency defined by sharing a vertex. This is a hypergraph matching? Actually edges are vertices of a line graph? In the line graph L(G), vertices correspond to edges of G, and two vertices are adjacent if the corresponding edges share a common endpoint. Then a perfect matching in L(G) is a set of pairs of adjacent edges covering all edges. So we need a perfect matching in the line graph. The line graph can be huge (m up to 1e5), but we need to find matching efficiently.
This is reminiscent of edge partition into paths of length 2. There might be a known result: such a partition exists iff the number of edges is even and every connected component of the line graph (or something) satisfies some parity? But line graph of connected graph (except maybe a star?) is connected? Actually if G is connected and not a single edge, L(G) is connected (if G is not a star? Wait: for a tree, the line graph of a star is complete graph? Actually star K_{1,n} has line graph which is a complete graph on n vertices (since all edges share the center). That is connected. For a path of length >=2, line graph is connected. So L(G) is connected if G is connected and has at least one vertex of degree >=2? Actually if G is a single edge, L(G) is a single vertex, but m=1 odd anyway. So L(G) is connected for connected graphs with at least 2 edges? There is a known fact: L(G) is connected iff G is connected and is not a tree with exactly one vertex of degree >1? Hmm, let's recall: line graph of a connected graph is connected if and only if the graph is not a path of length 0 or 1? Actually consider a graph that is two cycles connected by a single vertex? That's still connected. I think for connected G with more than 1 edge, L(G) is connected except when G is a star? Wait, star K_{1,n}: edges all share the center, so in line graph, any two edges are adjacent because they share the center. So line graph of a star is complete graph, hence connected. So all connected graphs with at least 2 edges have connected line graph? Actually consider a graph consisting of a single edge: line graph has 1 vertex, trivial. For two edges that don't share a vertex? But graph is connected, so if there are two edges, they must share a vertex? In a connected graph, any two edges are connected via a path but they might not be directly adjacent. For line graph connectivity, need that for any two edges e and f, there is a sequence of edges where consecutive edges share a vertex. This is true for any connected graph with at least 2 edges? Let's test: Suppose G is a path of three vertices: edges e1 (1-2), e2 (2-3). L(G) has vertices e1 and e2 adjacent, so connected. Suppose G is a triangle: three edges each adjacent to two others, connected. Suppose G is a graph with a vertex of degree 1 and a long path? Actually any connected graph with more than 1 edge: take any two edges. Since G is connected, there is a path connecting an endpoint of one edge to an endpoint of the other. The edges along that path give a sequence connecting them in L(G). But careful: need edges to share endpoints? The sequence can be the edges along the path: each consecutive edges share a vertex. So indeed L(G) is connected for any connected graph with more than one edge. Except maybe when graph has a bridge? But still sequence exists. So line graph is connected. So we have a connected graph L(G) on m vertices. We need to find a perfect matching in it (i.e., a 1-factor). Does a connected graph always have a perfect matching? No, not necessarily. For example, a tree (graph) with odd number of vertices may not have perfect matching. But m is even needed. However, even with even number of vertices, a connected graph may not have a perfect matching (e.g., a star with 3 leaves? That's K_{1,3} has 3 edges? Actually star K_{1,3} has 3 edges, m=3 odd, can't. For even m: consider a path graph on 4 vertices? That's 3 edges? Actually path on 4 vertices has 3 edges, odd. Path on 5 vertices has 4 edges? Let's think: L(G) is not arbitrary; it's a line graph. So maybe line graphs have special properties. There is a known theorem: Every connected line graph of a graph with an even number of edges has a perfect matching? Not sure.
Alternatively, consider the problem directly: We need to pair up edges such that each pair shares a vertex. This is like orienting each edge to a vertex? Another viewpoint: For each vertex, we can pair up edges incident to it, but edges incident to a vertex may be paired with edges incident to different vertices. The pair is determined by the common vertex, which is the vertex where the two edges meet. So each pair has exactly one middle vertex. Therefore, we can think of assigning each edge to be "used" at exactly one of its two endpoints as the middle vertex. For an edge (u,v), we need to decide whether it will be paired with some other edge at u, or at v. Then at each vertex, the edges assigned to that vertex must be paired up among themselves (since two edges that share that vertex must both be assigned to that vertex). So if we choose for each edge an endpoint (direction) indicating where it will be paired, then at each vertex, the number of edges assigned to that vertex must be even (so they can be paired). Moreover, the pairing at each vertex can be arbitrary among those edges (just any perfect matching of the incident edges assigned there). This reduces to: find an orientation of each edge to one of its ends such that for each vertex v, the number of edges oriented towards v (i.e., assigned to be paired at v) is even. Because then we can pair those edges arbitrarily at v. But careful: The pairing must be between two edges that both are assigned to the same vertex as the middle. So each edge must be assigned to exactly one of its endpoints. Then at each vertex, we need the count of assigned edges to be even (so they can be paired). This is exactly a parity condition. Additionally, we must also ensure that the pairing at each vertex is possible given the edges? But just even count is enough because we can arbitrarily match them; there is no further restriction (like we need to output the actual pairs). So the existence reduces to: find a labeling of each edge with one of its two endpoints such that for each vertex v, the number of edges labeled with v is even.
This is reminiscent of finding a spanning Eulerian subgraph? Actually it's like we have a graph, each edge chooses an endpoint, and we want each vertex to have even indegree in this directed sense (if we view the chosen endpoint as head). This is similar to finding a parity subgraph: given a graph, we want to orient each edge such that each vertex has even indegree (or outdegree). Since the sum of indegrees = number of edges, which is even if m even. So parity condition sum works. However, there may be constraints. Is such an orientation always possible when m is even? Not necessarily; consider a triangle with 3 edges (odd m). But m even? Let's test on graph with 2 edges sharing a vertex: e1: 1-2, e2:2-3. m=2 even. Can we assign endpoints? edges: (1,2) assign either 1 or 2; (2,3) assign either 2 or 3. We need each vertex's assigned count even. Vertex 2 appears in both edges. We can assign both edges to vertex 2: then vertex 2 gets count 2 (even), vertices 1 and 3 get 0 (even). That works. So triangle? Actually triangle has 3 edges (odd). So not considered. But consider a square (cycle of 4 vertices): edges: 1-2,2-3,3-4,4-1. m=4 even. Can we assign? Let's try: assign edges to vertices such that each vertex gets even count. For vertex 1: edges (1-2) and (4-1) can be assigned to 1 or not. Let's try assign (1-2) to 1, (4-1) to 1 -> vertex1 count 2 (even). Vertex2: edges (1-2) and (2-3). If (1-2) already assigned to 1, then (1-2) not to 2. So we need (2-3) assigned to 2 to give vertex2 count even? That gives 1? Actually if (1-2) assigned to 1, vertex2 gets 0 from that edge. So to get even (0 or 2), we need (2-3) assigned to 2? That gives vertex2 count 1 (odd). Not ok. Could assign (2-3) to 3? Then vertex2 count 0 (even). But then vertex3 gets from (2-3) assigned to 3, and also from (3-4). We need vertex3 even. If (3-4) assigned to 4, then vertex3 gets 1 (odd). If assigned to 3, then vertex3 gets 2 (even). So we could try: assign (1-2) to 1, (2-3) to 3, (3-4) to 3, (4-1) to 1. Then vertex1: edges (1-2) and (4-1) both assigned to 1 -> count 2 (even). Vertex2: edges (1-2) to 1, (2-3) to 3 -> count 0 (even). Vertex3: edges (2-3) to 3, (3-4) to 3 -> count 2 (even). Vertex4: edges (3-4) to 3, (4-1) to 1 -> count 0 (even). Works. So square works.
Consider a graph that is a path of 3 vertices (2 edges) we already did works.
Consider a graph that is two disjoint edges? But graph is connected so not.
Consider a star with 4 leaves (K_{1,4}): edges: center c and leaves a,b,d,e. m=4 even. We need assign each edge to either leaf or center. For each leaf vertex, only one edge incident, if assigned to leaf, leaf gets count 1 (odd). To be even, leaf must get 0 or 2. Since leaf has only one incident edge, the only way is to assign it to the center. So all four edges must be assigned to center. Then center gets count 4 (even). That works. So star works.
Consider a graph where a vertex has odd degree. Could that cause trouble? Let's test a graph with a vertex of degree 3 (odd) and total edges even? Example: consider a "Y" shape: center vertex 1 connected to 2,3,4. And also edge 2-3 to make total even? Actually with 3 edges (1-2,1-3,1-4) m=3 odd, can't. Need even. Add another edge somewhere to make even, say add edge 2-5 (a leaf). Now vertices: 1 degree 3, 2 degree 2 (edges 1-2 and 2-5), 3 degree 1,4 degree1,5 degree1. m=4 even. Can we assign? Let's see. For leaves 3,4,5 each have one edge. To have even count at leaves, they must assign their edge to the other endpoint (i.e., 1 for 3 and 4, 2 for 5). So edges (1-3)-> assign to 1, (1-4)-> assign to 1, (2-5)-> assign to 2. Now remaining edge (1-2) can be assigned either to 1 or 2. Counts: vertex1 currently has assigned edges from (1-3) and (1-4) =2 (even). If we assign (1-2) to 1, then vertex1 count becomes 3 (odd) -> not allowed. If assign (1-2) to 2, then vertex2 currently has (2-5) assigned to 2, so count becomes 1 (odd) -> not allowed. So impossible. So this graph cannot be partitioned? Let's test manually: Graph: vertices 1,2,3,4,5. Edges: 1-2,1-3,1-4,2-5. Can we pair edges into length-2 paths? We need to pair each edge with another sharing a vertex. Possible pairs: Could pair (1-3) and (1-4) with middle 1, leaving edges (1-2) and (2-5). These two share vertex 2, so can pair (1-2) and (2-5) with middle 2. That seems to work! Wait, check: pairing (1-3) and (1-4) uses edges (1,3) and (1,4) with middle 1. Then edges left: (1,2) and (2,5). They share vertex 2, so pair (1,2) and (2,5) with middle 2. That covers all edges. This is valid. In our assignment model, how does it correspond? For each edge we decide its middle vertex. For (1-3) we chose middle 1? Actually the pair (1-3) and (1-4) share vertex 1, so both edges have middle vertex 1. So assign (1-3) to 1, (1-4) to 1. For (1-2) and (2-5) share vertex 2, so assign (1-2) to 2, (2-5) to 2. So assignments: (1-3)->1, (1-4)->1, (1-2)->2, (2-5)->2. Then counts: vertex1: 2 (even), vertex2: 2 (even), vertex3: 0 (even), vertex4:0, vertex5:0. That's allowed. But earlier we forced leaves to assign to the other endpoint: for leaf 3 only edge (1-3), we assigned it to 1, that's fine. For leaf 4, assigned to 1. For leaf 5, we assigned to 2. That's exactly our assignment. So our earlier reasoning that leaves must assign to the other endpoint is correct. Then we had remaining edge (1-2). We had vertex1 count already 2 (even), vertex2 count 1 (from (2-5)). If we assign (1-2) to 2, then vertex2 becomes 2 (even). So assign to 2 works. But earlier I mistakenly thought vertex2 had count 1? Wait, we assigned (2-5) to 2, so vertex2 count 1. Then assigning (1-2) to 2 gives count 2, even. Vertex1 count remains 2 (since (1-2) not assigned to 1). So that works. I incorrectly said vertex2 count becomes 1 if assign (1-2) to 2? Actually I earlier said vertex2 currently has (2-5) assigned to 2 -> count 1. Then assign (1-2) to 2 -> count becomes 2 (even). Yes that's fine. So my earlier attempt said if assign (1-2) to 2 then vertex2 count becomes 1? That was a mistake. So that graph is possible.
Let's try a graph that might be impossible. For the assignment problem, we need an orientation of each edge to one endpoint such that indegree at each vertex is even. This is exactly the problem of finding a parity subgraph: we need to choose a subset of edges to orient? Actually orientation of each edge can be thought of as assigning a head vertex. The condition indegree even. This is equivalent to finding a subset of edges to orient such that each vertex's indegree parity is 0? Wait, we are orienting every edge, but orientation is just choosing head. So we need a function f: E -> V such that for each edge e = (u,v), f(e) ∈ {u,v}, and for each vertex v, the number of edges e with f(e)=v is even.
This is known as a "nowhere-zero flow" or "even orientation"? Actually it's like a 1-factorization of line graph? Another known concept: "graphical code" or "cut space". This is a linear equation system mod 2. Represent each edge as variable that chooses which endpoint gets the count. Let x_e = 0 if we assign edge to its first endpoint (say u), and 1 if assign to v (or vice versa). Then for each vertex v, the number of edges assigned to v is sum over incident edges of something. But careful: if we fix an ordering of endpoints, we can define for each edge e = (u,v), a contribution to u if we assign to u else to v. So for each vertex v, we have an equation: sum_{e incident to v} [assignment indicator equals v] ≡ 0 mod 2. That is linear equations mod 2. Variables are per edge, taking values in {0,1} (say 0 means assign to the lower-numbered vertex, 1 to the higher). Then for each vertex v, we have equation: sum_{e incident to v} (some coefficient) = 0 mod 2. For each edge e = (u,v), it contributes 1 to u if x_e = 0? Actually we can define: if we pre-assign an orientation, say each edge has a default assignment to its smaller-index vertex when x_e=0, and to the larger when x_e=1. Then the equation for vertex v becomes: sum_{e incident to v, with default assignment to v and x_e=0} 1 + sum_{e incident to v, with default assignment not to v and x_e=1} 1 ≡ 0 mod 2. This can be expressed as: for each vertex v, sum_{e incident to v} a_{v,e} + (some linear terms) etc. But simpler: we can think of it as: we need to choose for each edge a head, such that each vertex has even indegree. That's a classic problem: does there exist an orientation of an undirected graph such that each vertex has even indegree? Since indegree + outdegree = deg(v). Parity: indegree ≡ deg(v) - outdegree. But we can also rephrase mod 2: indegree ≡ deg(v) (mod 2) if we consider outdegree mod 2? Not helpful.
But we can also transform: If we consider the graph, an orientation with even indegree at each vertex is equivalent to saying that the indegree vector is in the cut space? Actually note: For any orientation, the sum of indegrees = m. If m is even, then the sum of parity conditions could be consistent. However, not every graph with even number of edges admits such an orientation. For example, consider a graph consisting of a single edge (m=1 odd, can't). But with even m, maybe always possible? Let's test a graph that might be impossible. Consider a graph with two vertices and multiple parallel edges? But graph is simple. So two vertices can have only one edge, m=1. For m even with two vertices, you'd need at least 2 edges, but simple graph cannot have multiple edges. So minimum 2 vertices can have at most 1 edge. So no case. For three vertices, possible edges: triangle (3 edges) odd; path of 2 edges (2 edges) we saw works. For four vertices: can we construct a graph where no such orientation exists? Let's try systematic.
We need a solution to the linear system over GF(2). Number of variables = m. Number of equations = n (one per vertex). But equations are not independent because sum over all vertices of (number assigned to v) = m (mod 2 = 0 if m even). So one linear dependency, so rank at most n-1. So we need to check consistency.
This system may always be solvable for connected graphs with m even? Not sure. Let's test some candidate impossible graph. Consider a graph that is a tree with an odd number of edges? But m must be even, so tree with even number of edges (i.e., odd number of vertices? Actually tree on n vertices has n-1 edges, so for m even, n must be odd. So tree with odd number of vertices and even edges. Example: n=5, tree: maybe a star with 4 leaves? That's 4 edges (even). We tested star works. What about a path of 5 vertices (4 edges)? Works? Let's test path 1-2-3-4-5. Edges: (1,2),(2,3),(3,4),(4,5). Need assign each edge to an endpoint such that each vertex's assigned count even. Leaves: vertex1 only edge (1-2). To be even, must assign to 2. Similarly vertex5 must assign (4-5) to 4. Vertex2: incident edges (1-2) and (2-3). (1-2) is assigned to 2? Actually we assigned (1-2) to 2? Wait leaf 1 forced assignment to 2, so (1-2) assigned to 2. For vertex2, we have (2-3) also. Vertex2 currently count from (1-2) = 1. To make even, we need (2-3) assigned to 3? That would give vertex2 count 1 (odd). Or assigned to 2 would give 2 (even). So we have a choice. Let's see if we can make it work. Set (1-2) assigned to 2 (forced). (4-5) assigned to 4 (forced). Now we need to assign (2-3) and (3-4). Vertex3: edges (2-3) and (3-4). Let's try: assign (2-3) to 2, (3-4) to 4. Then counts: v2: from (1-2) to2 + (2-3) to2 =2 (even). v3: from (2-3) to2 gives 0, from (3-4) to4 gives 0 -> 0 even. v4: from (3-4) to4 gives 1, plus (4-5) to4 gives 1+? Actually (4-5) assigned to4 gives count 1 for v4. So v4 count =1 odd -> not ok. Try assign (2-3) to2, (3-4) to3. Then v2: 2 (even), v3: (2-3) to2 gives 0, (3-4) to3 gives 1 -> 1 odd. v4: (3-4) to3 gives 0, (4-5) to4 gives 1 ->1 odd. Not ok. Try assign (2-3) to3, (3-4) to3. Then v2: (1-2) to2 gives 1, (2-3) to3 gives 0 ->1 odd. So not ok. Try assign (2-3) to3, (3-4) to4. Then v2:1 (odd). v3: (2-3) to3 gives1, (3-4) to4 gives0 ->1 odd. v4: (3-4) to4 gives1, (4-5) to4 gives1 ->2 even. So v4 ok but others not. So none works? Let's systematically solve. Variables: edges e12, e23, e34, e45. For each vertex condition (even count). We define assignment: for each edge, choose one endpoint as head. We'll denote for each edge (u,v) variable x_e: 0 means head u, 1 means head v (assuming u<v). For path: vertices 1,2,3,4,5. Edges: 1-2,2-3,3-4,4-5. Write equations:
Vertex1: only edge (1-2). If we choose head for e12: if x12=0 (head 1) then count1 gets 1; if x12=1 (head 2) then count1 gets 0. So we require: (if x12=0) then 1 ≡0 mod2 => x12 must be 1 (head 2). So x12=1 forced.
Vertex5: edge (4-5). For e45: if x45=0 (head 4) then count5 gets 0; if x45=1 (head5) then count5 gets 1. So require: must have count5=0 => x45=0 forced (head 4).
Now vertex2: edges e12 and e23. For e12, if x12=1 then it contributes to vertex2? Since e12: 1-2, if x12=0 head 1, contributes to 2? Actually we need to define contributions carefully. Let's define for each edge (u,v) with u<v, variable x_e = 0 means assign to u (head u), 1 means assign to v (head v). Then for vertex v, it gets a contribution of 1 if:
- v is u and x_e = 0, OR
- v is v and x_e = 1.
Otherwise 0.
So for vertex2:
- e12: u=1, v=2. If x12=0 then head=1, contributes to 1 not 2. If x12=1 then head=2, contributes to 2.
- e23: u=2, v=3 (since 2<3). For e23, if x23=0 then head=2 (contribute to 2), if x23=1 then head=3 (contribute to 3).
Thus equation for vertex2: (x12 ==1? contribution) + (x23 ==0? contribution) ≡ 0 mod2.
Let's denote indicator: for e12, contribution to vertex2 is x12 (since if x12=1 then 1, else 0). For e23, contribution to vertex2 is (1 - x23) if we think? Actually if x23=0 then contributes 1; if x23=1 then contributes 0. So it's (1 - x23). But mod2, 1 - x23 ≡ 1 + x23 (since - ≡ + mod2). So contribution = 1 + x23 mod2. But we can also just treat as boolean: we need parity condition.
Write mod2: x12 + (1 - x23) ≡ 0 (mod2) => x12 + 1 + x23 ≡ 0 (since -x23 ≡ x23 mod2? Wait, mod2, 1 - x23 = 1 + x23 because -x23 ≡ x23 (since 2x23 ≡0). Actually careful: in integers mod2, subtraction is same as addition. So 1 - x23 ≡ 1 + x23 (mod2). So condition: x12 + 1 + x23 ≡ 0 → x12 + x23 ≡ 1 (mod2). Because bring 1 to other side: x12 + x23 ≡ 1 (since 1+...=0 => ...= -1 ≡1). So x12 + x23 ≡ 1 (mod2).
We have x12=1 from vertex1. So 1 + x23 ≡ 1 => x23 ≡ 0 (mod2). So x23=0 forced. So e23 must be assigned to vertex2 (since u=2, x23=0 means head 2).
Vertex4: edges e34 and e45. e34: u=3, v=4. e45: u=4, v=5. For vertex4:
- e34: if x34=1 then head=4 contributes to 4 (since v=4), if x34=0 then head=3 contributes to 3 → contribution = x34? Actually if x34=1 then contributes 1; else 0. So contribution = x34.
- e45: u=4,v=5; if x45=0 then head=4 (since u=4) contributes 1; if x45=1 then head=5 contributes 0 → contribution = (1 - x45) ≡ 1 + x45.
Equation: x34 + (1 + x45) ≡ 0 => x34 + x45 ≡ 1 (mod2). Since x45=0 forced from vertex5, we get x34 + 0 ≡ 1 => x34 ≡ 1. So x34=1 forced (head to 4? Actually x34=1 means head to v=4, consistent).
Now vertex3: edges e23 (u=2,v=3) and e34 (u=3,v=4). For vertex3:
- e23: u=2,v=3; contribution to vertex3 if x23=1 (head=v=3) → contribution = x23? Actually if x23=1 contributes 1, else 0. So contribution = x23.
- e34: u=3,v=4; contribution to vertex3 if x34=0 (head=u=3) → contribution = (1 - x34) ≡ 1 + x34.
Equation: x23 + (1 + x34) ≡ 0 => x23 + x34 ≡ 1 (mod2). Plug x23=0, x34=1 → 0+1=1 mod2, ok. So satisfied.
Thus we found a solution: x12=1, x23=0, x34=1, x45=0. So path of 4 edges works. So path of even edges works.
So far all examples we tried are solvable when m even. Could there be a connected graph with even number of edges where no such orientation exists? Let's try to find a counterexample.
Consider a graph with two vertices of odd degree? Actually in assignment problem, we can formulate as follows: For each vertex v, define d(v) degree. If we assign each edge to one endpoint, let a(v) = number of edges assigned to v. Condition: a(v) even for all v. Sum_v a(v) = m (even). Now note that a(v) ≡ d(v) (mod 2) ??? Not necessarily. Because each edge contributes to exactly one endpoint. So a(v) mod 2 can differ from d(v) mod 2. But we can consider that for each edge, we choose one endpoint, essentially picking a direction. This is equivalent to selecting a subset S of vertices such that each edge has exactly one endpoint in S? Not exactly. If we think of each edge as being oriented from the unselected endpoint to the selected endpoint (head). Then each vertex's indegree is a(v). The outdegree is d(v)-a(v). We want a(v) even. No immediate parity constraints.
Another viewpoint: This is like a 2-factorization of line graph? Could be related to existence of a perfect matching in line graph. There is a known theorem: Every connected line graph with an even number of vertices has a perfect matching, except for some small exceptions? I recall a result: "Every connected claw-free graph with an even number of vertices has a perfect matching." Actually line graphs are claw-free (they do not contain an induced K_{1,3}). There is a theorem by Sumner and others: Every connected claw-free graph with an even number of vertices has a perfect matching. I think there is a known result: Every connected claw-free graph of even order has a perfect matching. This is a classical theorem (Sumner's theorem? Actually Sumner's theorem says every connected claw-free graph with an even number of vertices has a perfect matching. I need to confirm: "Every connected claw-free graph of even order has a perfect matching" - I believe this is a theorem by Las Vergnas and Sumner? There is a result: "Every connected claw-free graph with an even number of vertices has a perfect matching." Yes, I recall something like that. Since line graphs are claw-free, this would imply that if L(G) is connected and has even number of vertices (i.e., m even), then L(G) has a perfect matching. That would give a solution always when m is even and G is connected (except maybe when G is a single edge? But m=1 odd, so not relevant). However, is L(G) always connected for connected G? As argued earlier, yes for any connected graph with at least one edge, line graph is connected? Actually need to be careful: For a tree with 2 vertices (single edge), L(G) has 1 vertex (connected). For any connected graph with more than one edge, L(G) is connected. So for m even and m>=2, L(G) is connected and claw-free, thus has a perfect matching. Therefore the problem always has a solution for any connected graph with even number of edges! Wait, is that theorem always true? Let's check known counterexamples: There is a known fact: Not every claw-free graph has a perfect matching; but the theorem says "connected claw-free graph of even order has a perfect matching"? I'm not 100% sure. Actually I recall a result: "Every connected claw-free graph of even order has a perfect matching" is a theorem by Sumner (1974) for graphs with minimum degree at least 2? Hmm. Let's recall: There is a well-known theorem by Las Vergnas and Sumner about matchings in claw-free graphs. The strongest: "Every connected claw-free graph of even order has a perfect matching." I think that is true. But I'm not absolutely certain. Let me think of potential counterexample: The claw is K_{1,3}. A claw-free graph could be odd cycle? C5 is claw-free? C5 has no induced K_{1,3} because it's a cycle, yes. C5 has 5 vertices (odd), so not even. For even, say C4 is claw-free and has perfect matching? C4 has perfect matching? Yes, C4 is a cycle of length 4, it has perfect matchings. C6 also has perfect matchings. What about the Petersen graph? The Petersen graph is claw-free? Actually the Petersen graph is known to be claw-free? I think the Petersen graph is K_{1,3}-free? I think it is claw-free because its girth is 5 and minimum degree 3, but maybe it contains a claw? Not sure. But the Petersen graph has 10 vertices (even) and is connected and claw-free, and it does have a perfect matching? Actually the Petersen graph has perfect matchings? Yes, it is 3-regular and is known to have a perfect matching (it is bridgeless cubic, so by Petersen's theorem it has a perfect matching). So that works.
But is there any connected claw-free graph with even number of vertices that does not have a perfect matching? Possibly some graphs like a triangle with a pendant edge? But triangle with a pendant edge: vertices: triangle a,b,c plus pendant d attached to a. That's 4 vertices? Actually triangle (3 vertices) plus pendant adds 4th vertex, so total 4. Edges: triangle edges: ab, bc, ca, plus ad. This graph is not a line graph? But we are focusing on line graphs, which are a subclass of claw-free graphs. However, the theorem might hold for all claw-free graphs, but I'm not 100% sure. Let's search memory: "Sumner's theorem: Every connected claw-free graph with an even number of vertices has a perfect matching." Yes, I recall that. Actually I think it's a known result: Sumner (1974) proved that every connected claw-free graph of even order has a perfect matching. I've seen it referenced in some combinatorial problems. So if that's true, then the answer to this problem is always "Yes" when m is even, and "No solution" only when m is odd. But the problem statement includes an example where m=3 (odd) outputs No solution. In second sample, graph is triangle (3 edges, odd) -> No solution. Third sample: 3 vertices, 2 edges (even) -> has solution (1 2 3). So far consistent. So perhaps the intended solution is: if m is odd, impossible; else always possible. But is it always possible for any connected graph with even edges? Let's test some tricky cases. Consider a graph that is just two edges sharing a vertex (path of length 2). Works. Consider a graph that is a single edge? m=1 odd, impossible.
Consider a graph that is a cycle of odd length? For example, triangle m=3 odd, impossible. Cycle of length 5: m=5 odd, impossible. Cycle of length 4: m=4 even, works (we saw assignment exists). So parity of cycle length doesn't matter; only total edges count even.
But wait, what about a graph that is a single vertex? No edges, m=0 even? But m>=1? Possibly m can be 0? But constraints: 1 ≤ n, m ≤ 1e5; m can be 0? Actually m>=1? It says m edges, but range 1 to 1e5, so m>=1. So m=0 not considered.
So perhaps the necessary and sufficient condition is simply m even. But let's double-check with a more complex graph that might violate something. I recall that line graphs are not only claw-free but also have some other properties. The existence of a perfect matching in a connected claw-free graph of even order is a theorem indeed. However, I'm cautious: There is a known result: "Every connected claw-free graph of even order has a perfect matching." I think it's true for graphs with at least some conditions? Actually I recall a result by Las Vergnas (1975) and Sumner (1976) about matchings in claw-free graphs. Let me verify by constructing a possible counterexample: Consider a graph that is two disjoint triangles connected by a single vertex? But that would be not claw-free? Let's try to find a connected claw-free graph with even number of vertices that has no perfect matching. I'll think of known families: The line graph of a graph is claw-free. Are there line graphs that are connected, even number of vertices, but without a perfect matching? If the theorem holds for all claw-free graphs, then none. But maybe the theorem has additional condition like minimum degree at least 2? Let's search memory: There is a classical theorem by Sumner: "Every connected claw-free graph with an even number of vertices has a perfect matching." I'm pretty sure I've seen this used in programming contest problems. Actually I recall a Codeforces problem "Little Pony and Harmony Chest" no. Another problem: "Matching in Line Graph" maybe. But yes, I think it's a known result used in some solutions. However, is it always true? Let's test with a small claw-free graph that is not a line graph but still claw-free. For instance, take the graph consisting of a 5-cycle plus an isolated vertex? Not connected. Must be connected. Consider the graph that is the complement of a perfect matching? Not sure.
Alternatively, maybe the theorem holds for "claw-free graphs with minimum degree at least 2"? I'm trying to recall known results: Sumner's theorem: "Every connected claw-free graph of even order has a perfect matching." Actually I think that is correct. Let me confirm by checking small even-order claw-free graphs. For n=2, only possible connected claw-free graph is K2 (claw-free), has perfect matching? n=2, perfect matching would be the edge itself, yes. For n=4, connected claw-free graphs: K4, C4, diamond (K4 minus an edge), paw (triangle with pendant). But paw: vertices: triangle a,b,c, plus d attached to a. This graph has 4 vertices. Is it claw-free? A claw is an induced K1,3. Does paw contain an induced K1,3? The vertices a,b,c,d: subgraph induced by a,b,c,d: edges: ab,ac,bc (triangle) and ad. Consider vertex a, neighbors b,c,d. The induced subgraph on {a,b,c,d} includes edges ab,ac,ad, and bc. The set {b,c,d} are neighbors of a: b and c are adjacent, so not independent. So not a claw. So paw is claw-free. Does it have a perfect matching? Paw: vertices: a,b,c,d. Edges: triangle a-b, a-c, b-c, plus a-d. Can we find a perfect matching covering all 4 vertices? Try match a-b and c-d? But c-d not edge. a-c and b-d? b-d no. a-d and b-c? That's edges a-d and b-c. Yes, that works. So perfect matching exists.
Consider a graph: a cycle of length 4 with a chord? Already fine.
For n=6, can we construct a connected claw-free graph with no perfect matching? The classic example of a graph with no perfect matching is a graph with an odd component? But connected and even number of vertices, by Tutte's theorem there might be a graph with no perfect matching. But does it remain claw-free? For a graph to have no perfect matching, there must be a Tutte set. But could such a graph be claw-free? Possibly there exist claw-free graphs without perfect matching. I recall that "the bull graph" is claw-free? Bull graph: triangle with two pendant leaves from two vertices? Actually bull graph: vertices a,b,c,d,e with edges ab, bc, ca (triangle), plus b-d and c-e. That's 5 vertices odd. Not even. For even, maybe a graph formed by two copies of K4 minus a perfect matching? Not sure.
Let's search memory: There is a known result: "Every connected claw-free graph of even order has a perfect matching." I think it's a theorem by Sumner (1974) and independently by Las Vergnas (1975). Actually I recall reading: "Sumner (1974) proved that every connected claw-free graph of even order has a perfect matching." So it's true. But wait, I also recall that "claw-free graphs are factor-critical?" Not all.
Let me double-check by attempting to construct a counterexample. Consider a graph that is the line graph of a bipartite graph? But line graphs are claw-free. Could the line graph of some graph have no perfect matching? That would be a counterexample to the theorem. But if the theorem is true, then none exist. However, is the line graph of a connected graph always claw-free? Yes, by definition, line graphs are claw-free because a claw in L(G) would correspond to three edges in G all incident to a common vertex but not all pairwise adjacent? Actually a claw K_{1,3} in L(G) would be a vertex (edge e0) adjacent to three other vertices (edges e1, e2, e3) that are not adjacent among themselves. In G, that means e0 shares a vertex with e1, e2, e3, but e1, e2, e3 do not share a vertex among themselves. Could that happen? In G, if e0 connects vertices u and v, then for e1, e2, e3 each to be adjacent to e0, they must share either u or v. If they all share the same endpoint, say u, then e1, e2, e3 are all incident to u, thus any two of them share u, so they are adjacent in L(G). So to have them non-adjacent, they must be incident to different endpoints: some to u, some to v, and no two share an endpoint. For three edges e1,e2,e3 to be incident to e0 but not to each other, we could have e1 incident to u, e2 incident to v, e3 incident to u? Wait, if e1 and e3 both incident to u, they share u, so they'd be adjacent. So at most one edge incident to each endpoint can be among the three if we want them non-adjacent. Since e0 has only two endpoints, at most two edges can be incident to e0 without being adjacent to each other: one incident to u, one incident to v. So a claw K_{1,3} cannot appear. So line graphs are claw-free.
Thus if Sumner's theorem holds, then any connected line graph with even number of vertices (i.e., m even) has a perfect matching. So the problem reduces to checking if m is even; if yes, always possible; if no, impossible.
But let's verify with a more exotic graph: a graph with a vertex of degree 1? That's fine. As we saw, star works. So maybe it's always possible. Let's test with a small graph: n=2, m=1 odd -> no. n=3, m=2 even -> works. n=4, m=4 even -> could be a path of 3 edges? Actually path with 3 edges (m=3 odd) no. So only even m.
But wait, what about a graph that is a single vertex with loops? Not allowed.
I think it's plausible that the intended solution is to simply output something when m even, perhaps constructing the matching via a DFS or greedy algorithm, not relying on the theorem. The problem says "Little Chris is participating... You are given a chance to compete with Chris." So it's a problem from Codeforces? I recall a Codeforces problem "Little Pony and Harmony Chest"? No. Actually this is Codeforces Round #261 (Div. 2) problem E? Might be "Chris and Road"? Not sure. I recall a problem called "Edge coloring" or "Cutting Graph". Actually I think this is Codeforces problem 267C? Let's search memory: There is a problem "Chris and Dancing" no. I think it's from Codeforces 263B? Not sure.
Given constraints up to 1e5 edges, we need to output the pairs. So we must actually construct the pairing, not just say "possible". So we need an algorithm to find the perfect matching in the line graph. Even if existence is guaranteed for even m, we still need to construct the pairing. So we need to produce the triples.
So we need to find a pairing of edges such that each pair shares a common vertex. That's essentially a 2-factorization? We can use the orientation approach: assign each edge to an endpoint such that each vertex gets even number of assigned edges. Then at each vertex, arbitrarily pair up the edges assigned to it. That yields the desired partition. So the problem reduces to finding such an assignment (an orientation where indegree is even) when m even. And we hypothesize it's always possible for connected graphs with even m. But we must prove or at least construct it algorithmically.
Thus we can attempt to construct the assignment via a DFS or similar, maybe using an Eulerian tour? Because condition even indegree resembles an Eulerian orientation? Actually an Eulerian orientation is one where indegree equals outdegree for each vertex, which is stronger (equal, not just even). But we just need parity. We can treat this as a system of linear equations mod 2. Since the graph is connected, we can solve it greedily.
Let's think: we need to assign each edge to one of its endpoints such that at each vertex, the number of assigned edges is even. This is equivalent to choosing a subset of vertices S? Not exactly.
We can view it as: for each edge, we decide which endpoint "owns" the edge for the purpose of pairing. Then at each vertex, we will pair up the edges it owns. So we need the parity condition.
This is like finding a parity subgraph: For each vertex v, we want the number of incident edges assigned to v to have a given parity (0). That's a system of linear equations over GF(2) where each equation is sum over incident edges (x_e) (or 1-x_e) depending on orientation. But we can transform to a simpler system: let’s arbitrarily orient each edge from one endpoint to the other. Then define variable y_e = 0/1 indicating whether we flip the assignment relative to orientation? Actually we can set an arbitrary initial assignment: for each edge, initially assign it to, say, the endpoint with smaller index. Then at each vertex, we have some current parity (maybe odd/even). Then we can flip assignment of some edges (i.e., change which endpoint gets it) to adjust parity. Flipping the assignment of an edge toggles the parity of both its endpoints? Let's analyze.
Let for each edge e = (u,v), we initially assign it to some endpoint, say u. Then for each vertex v, we have count c(v) = number of edges initially assigned to v. This may be odd or even. Now, if we flip the assignment of an edge (i.e., change from u to v), then the count for u decreases by 1, and count for v increases by 1. So parity of both u and v toggles (since subtracting 1 toggles parity, adding 1 toggles parity). So flipping an edge toggles parity at its two endpoints.
Thus we need to choose a subset F of edges to flip such that after flipping, all vertices have even parity. That is, we need to toggle parities at vertices that initially have odd parity. Because we want final parity even (0 mod 2). Initially we have some set O of vertices with odd c(v). Flipping an edge toggles the parity of its endpoints. So we need to select a set of edges such that the symmetric difference of their endpoint sets equals O (i.e., the odd vertices become even, and even vertices remain even). This is exactly the problem of finding a subgraph (set of edges) whose vertices with odd degree are exactly O. That is, we need to find an edge set T (edges to flip) such that in the subgraph induced by T, the vertices of odd degree are precisely O. In other words, T is a collection of edges that forms an "O-join" (a set of edges covering the odd vertices with odd degree). This is a classic problem: In any connected graph, there exists a set of edges such that the odd-degree vertices are exactly a given set of even cardinality. Indeed, given any set of vertices O of even cardinality, there exists a subgraph (forest) where O are exactly the odd-degree vertices. For example, take any spanning tree and then take the unique paths between pairs of odd vertices. More concretely, we can pair up the odd vertices arbitrarily and for each pair, take the unique path in the tree (or any path) and take the symmetric difference of those paths. The resulting set of edges has odd degree exactly at the vertices in O. Since O has even size, we can pair them. So this is always possible.
Thus the assignment exists if and only if the initial set O (vertices with odd initial assignment count) has even cardinality? Because we need to toggle them to even, and flipping edges toggles parity of two vertices, so the number of odd vertices must be even. Is it always even? The sum of parities of all vertices is sum c(v) mod 2 = total number of edges assigned initially = m mod 2. Since m is even, total odd parity count is even (since sum of parities = m mod 2 = 0 mod 2). Actually sum of odd/even? Parity sum: sum (c(v) mod 2) mod 2 = total edges mod 2 (since each edge contributes 1 to exactly one vertex's count). So if m is even, the number of vertices with odd c(v) is even. So O has even size. Therefore, we can always find a set of edges to flip to make all parities even, because the graph is connected. Indeed, we can take any spanning tree, pair up the odd vertices, and for each pair take the unique path in the tree, and take the symmetric difference of all those paths. The resulting set T will have odd degree exactly at those odd vertices. Since we are working mod 2, flipping those edges yields even parity at all vertices.
Thus we have a constructive proof that for any connected graph with even number of edges, such an assignment exists. Therefore, the problem always has a solution when m is even, and no solution when m is odd. That is the key.
Let's verify with our earlier examples: initial assignment could be arbitrary; we then compute odd vertices, find T, flip edges in T, then assign edges accordingly. Then at each vertex, even number of assigned edges, then pair them arbitrarily.
Thus algorithm:
- Input n,m.
- If m is odd: output "No solution".
- Else:
- Build graph adjacency list (for later pairing).
- We'll assign each edge initially to some endpoint, say the smaller-index vertex (or any consistent rule, like to the first endpoint given). Compute for each vertex v, cnt[v] = number of edges initially assigned to v.
- Determine set odd_vertices: those v with cnt[v] % 2 == 1.
- Since m even, the number of odd vertices is even. We need to find a set of edges to flip such that parity toggles at exactly those odd vertices. This is equivalent to finding a T (edges to flip) that forms a subgraph where odd-degree vertices are odd_vertices. This can be done via constructing a spanning tree, then for each odd vertex pair (say pairing consecutive ones after sorting), we can flip edges along the unique path between them in the tree. But flipping along a path toggles parity at endpoints. However, if we do multiple paths, the toggles accumulate via symmetric difference. The standard method: Take any spanning tree (or use DFS tree). Then, do a DFS from an arbitrary root, and while processing, we can propagate parity needs upward, similar to solving a "matching" on the tree to cover odd vertices. A simpler method: Since we only need to produce a set of edges to flip, we can pair up the odd vertices arbitrarily (e.g., using a stack) and then for each pair, flip all edges on the tree path between them. However, flipping many edges multiple times might cause an edge to be flipped an even number of times, cancelling out. To implement efficiently, we can compute for each edge how many times it is toggled modulo 2. Since m up to 1e5, and tree size n-1, and number of odd vertices up to n, we can process paths by incrementing counters on edges (like using LCA with binary lifting and difference arrays). But we can avoid explicit path flipping by using a simpler approach: Do a DFS from root, compute for each vertex whether we need to flip the edge to its parent to fix parity. This is similar to a standard technique for making all vertex parities even by flipping edges along tree. More precisely, we have initial odd set O. We want to choose a set T such that in the tree (or original graph), the degree of each vertex in T (mod 2) equals the initial parity (odd/even). Actually we want T such that for each vertex v, the parity of the number of edges from T incident to v equals the initial odd parity (cnt[v] mod 2). Because flipping those edges toggles parity: final parity = initial parity XOR (deg_T(v) mod 2). We want final parity = 0, so we need deg_T(v) mod 2 = initial parity. So T must be a subgraph where the degree parity at each vertex matches the initial odd parity (which is 1 for odd vertices, 0 for even). That's exactly a "parity subgraph". In a tree, we can compute T by a