Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
Output
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
Examples
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
Note
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve: Given an undirected graph G with n vertices, m edges (no loops, no multiple edges). Find two disjoint subsets A and B of V such that both A and B are vertex covers of G. Or output -1 if impossible.
Interpretation: A vertex cover is a set that touches every edge. Typically, minimum vertex cover etc. Here we require two disjoint vertex covers (not necessarily covering all vertices; some vertices can be left out). They must be disjoint; they can be non-empty? The problem statement says: "both sets are vertex cover. Note that because of m ≥ 1, vertex cover cannot be empty." So each vertex cover must be non-empty? Actually they say: "each description must contain two lines ... Note that because of m ≥ 1, vertex cover cannot be empty." So indeed a vertex cover must contain at least one vertex because m >=1 means there is at least one edge, and empty set would not cover any edge. So A and B each must be non-empty vertex covers, and disjoint. The uncovered vertices can be assigned to ourselves.
We need to output any such A and B (size and list). Or -1 if impossible.
Constraints up to 100k vertices and edges.
We need to think about conditions for existence.
Observations: Two disjoint vertex covers exist if and only if the graph is bipartite? Actually consider: If we have two disjoint vertex covers A and B, then every edge must have at least one endpoint in A, and also at least one endpoint in B. That means each edge must have one endpoint in A and the other in B? Not necessarily: an edge could have both endpoints in A (but then B must also cover that edge, so at least one endpoint in B; if both endpoints are in A, then to cover the edge for B, at least one of the endpoints must also be in B, but A and B are disjoint, so impossible). Similarly, an edge cannot have both endpoints in B. So each edge must have exactly one endpoint in A and exactly one endpoint in B? Let's analyze: For an edge uv, A must cover it => u in A or v in A. B must cover it => u in B or v in B. Since A and B are disjoint, u cannot be in both A and B; same for v. So the possibilities:
u in A, v not in A; and v must be in B (since B needs to cover the edge; u cannot be in B because disjoint, so v must be in B). So we get u in A, v in B.
u in B, v in A.
Could both u in A and v in A? Then B would need either u or v in B, but that would conflict with disjointness; so impossible.
Both in B similarly impossible.
What about u not in A and v not in A? Then A fails to cover edge, so impossible.
So each edge must have exactly one endpoint in A and exactly one in B. Yes.
Thus the condition forces that (A, B) form a partition of the set of vertices incident to edges? Actually vertices not incident to any edge (isolated vertices) can be anywhere because they don't affect cover condition. But for every edge, its endpoints are assigned one to A and one to B, and no edge can have both endpoints in the same set. This is exactly a proper 2-coloring of the graph: color vertices in A one color, vertices in B the other color, and every edge connects different colors. That is precisely a bipartite graph. However, note that there could be isolated vertices: they don't constrain coloring. Also, vertices that are not part of any edge can be arbitrarily assigned or left out; but since A and B must be non-empty, we need at least one vertex in each.
But wait: The condition also requires that A and B be vertex covers. For an edge, if exactly one endpoint in A and one in B, then indeed both are covers. However, what if the graph has isolated vertices? They don't affect the covering property because there are no edges incident to them. So they can be placed arbitrarily. But the requirement that A and B are non-empty (since m≥1, vertex cover cannot be empty). So we need at least one vertex in each. If the graph has at least one edge, then any vertex that is an endpoint of an edge will be in either A or B as per bipartition. So both A and B will have at least one vertex as long as there is at least one edge, because each edge contributes one endpoint to A and one to B. Actually if the graph is a single edge (u,v), then one of u,v must be in A and the other in B, so both sets non-empty.
Thus the necessary and sufficient condition seems to be that the graph is bipartite. But is bipartite sufficient? Let's test: If G is bipartite, we can 2-color it: left part L and right part R. Then set A = L, B = R. Then every edge goes between L and R, so condition holds. A and B are disjoint, both are vertex covers (since each edge has one endpoint in L and one in R). However, is it required that both A and B are vertex covers themselves individually? Yes: L covers all edges because each edge has at least one endpoint in L (since each edge goes between L and R, actually it has exactly one, but that's enough). Similarly for R. So L and R are both vertex covers. Also they are non-empty? In a bipartite graph with at least one edge, it's possible that one part is empty? Not if there is at least one edge: an edge connects one vertex from L and one from R, so both parts must contain at least that endpoint. So both parts non-empty. So any bipartite graph with at least one edge yields a solution: A = one part, B = the other.
Now what about non-bipartite graphs? Could there still be two disjoint vertex covers? Suppose graph contains an odd cycle. The earlier reasoning that each edge must have exactly one endpoint in A and one in B implies that the assignment of vertices to sets A and B yields a proper 2-coloring of the graph. So if the graph is not bipartite, then no such assignment exists, because any assignment of vertices to two disjoint sets (with no vertex in both) that ensures each edge has at least one endpoint in each set forces exactly one endpoint in each set, which is exactly a proper 2-coloring. However, is it possible that a vertex is assigned to neither A nor B? Yes, we are allowed to leave vertices out (give to ourselves). So the assignment does not have to be a partition of all vertices; we only assign to A and B; other vertices are unassigned. Could that allow a solution in a non-bipartite graph? Let's consider. Suppose we have an odd cycle of length 3: vertices 1-2,2-3,3-1. We need to select A and B, disjoint, such that each edge has at least one endpoint in A and also at least one in B. Let's try to assign some vertices to A, some to B, some to none.
If we try to assign A={1}, B={2}. Then edge (1,2) is covered by both (1 in A,2 in B). Edge (2,3): 2 in B covers B, but A? 1 not incident, 2 not in A, 3 not in A. So A fails for edge (2,3) because no endpoint in A. So need A to cover edge (2,3). So we need either 2 in A or 3 in A. But 2 is already in B, cannot be in A. So we must put 3 in A. Then A={1,3}, B={2}. Edge (1,3): A covers it (1 and 3 in A), but B does not cover it because 2 not incident, and neither 1 nor 3 in B. So B fails. Could put 1 or 3 in B, but they are already in A; conflict. So impossible.
What if we leave some vertices unassigned, maybe we can have larger sets? Let's systematically analyze: For each edge, we need (u in A or v in A) AND (u in B or v in B). With A and B disjoint. This can be seen as a 2-SAT type constraint? Let's derive necessary condition. Represent each vertex can have three states: in A, in B, or neither. For each edge (u,v), we must have:
Not (u not in A AND v not in A) => at least one of u or v in A.
Not (u not in B AND v not in B) => at least one of u or v in B.
And disjointness: can't have both A and B simultaneously.
Now, can a non-bipartite graph satisfy this? Let's try a triangle with an extra isolated vertex maybe? But still triangle.
Consider an odd cycle length 5. Suppose we try to assign A and B. For each edge, need one endpoint in A and one in B? As argued earlier, because if an edge had both endpoints in A, then B fails; both in B fails A; one endpoint in A and other unassigned: then B must cover that edge, so the unassigned must be in B? Actually if u in A, v not in A nor B (unassigned), then for B to cover the edge, either u in B (can't because A) or v in B. So v must be in B. Similarly, if u in A and v in A, then B cannot cover because neither endpoint in B (since A and B disjoint). So v must be in B if only one is in A. So indeed, if one endpoint is in A and the other is not in A, then the other must be in B. And if one endpoint is in B and the other not in B, then the other must be in A. This essentially forces that for any edge, the states of its endpoints must be opposite (one in A, other in B) or possibly both in A? That leads to violation for B. Both in B leads to violation for A. Could both be unassigned? Then A fails, B fails. So the only satisfying assignments for an edge are: (A,B), (B,A). (Could we have A,A? No; B,B? No; A,unassigned? Then unassigned must be B; so becomes A,B. B,unassigned -> becomes B,A. unassigned,A -> becomes B,A? Wait: if v in A and u unassigned, then to satisfy A: ok (v in A). For B: need either u in B or v in B; v not in B, so u must be in B => so u in B, thus B,A. Similarly, unassigned, unassigned: fail. So indeed, the only way is one endpoint in A, the other in B. So assignment to A and B for vertices incident to edges must form a proper 2-coloring of the graph. Vertices that are not incident to any edge can be arbitrarily assigned to A or B or neither, but they don't affect covering. But if we assign an isolated vertex to A, it doesn't help with any edge but is allowed. But the constraints on edges force that for every edge, its endpoints have opposite A/B assignments. That means that if we consider the subgraph induced by edges, the assignment restricted to the vertices that are incident to at least one edge must be a proper 2-coloring. This implies that the graph must be bipartite (the whole graph, because isolated vertices don't affect bipartiteness). So the graph must be bipartite. Even if there are isolated vertices, the graph still must be bipartite (i.e., no odd cycle) because any odd cycle would create contradiction. So necessary condition: G is bipartite.
Thus the problem reduces to: Check if the graph is bipartite. If yes, output two disjoint vertex covers (which correspond to the two sides of bipartition). Additionally, we must ensure that each set is non-empty. As argued, if the graph has at least one edge, each part will have at least one vertex because edges connect both parts. However, consider graph with at least one edge but maybe all edges are incident to vertices in one part only? No: bipartition ensures that each edge connects a vertex from left and right; so both sides are non-empty. But if the graph has isolated vertices, they can be arbitrarily assigned but we can just output the bipartition of the connected components that have edges. So we need to compute a bipartition. Also note that the graph may be disconnected; we need to combine bipartitions consistently across components? Actually bipartite graph must be 2-colorable overall; that's a global property. So we need to check if it's bipartite; if yes, we get a 2-coloring (0/1). Then we can output all vertices with color 0 as set A, all vertices with color 1 as set B. But wait: What about isolated vertices? They have no edges, so any coloring works; we can assign them arbitrarily. But we could also assign them to either side; it doesn't matter. However, we must ensure both sets are non-empty. If the graph has at least one edge, both parts will be non-empty because each edge gives one vertex of each color. But consider a graph with an edge but all vertices except the two endpoints isolated? Actually the edge connects two vertices; both get different colors; so both sets contain at least one vertex. So non-empty satisfied.
Thus solution: If the graph is bipartite, output two lines: first line size of color 0 set, second line its vertices; then similarly for color 1 set. Order doesn't matter; also we could output A as color 0, B as color 1, or swapped. Ensure disjointness and both are vertex covers.
But wait: Example 1 output:
1
2
2
1 3
In that graph: n=4, m=2, edges: 1-2,2-3. This graph is a path 1-2-3 plus isolated vertex 4. Bipartite: colors: assign 2 as one color, 1 and 3 as opposite. So indeed one side has {2}, the other {1,3}. They output A as size 1, vertex 2; B as size 2, vertices 1 3. So they gave the smaller set first? It matches bipartition. They left vertex 4 out (unassigned). So that matches.
Second example: triangle (3,3). Not bipartite, output -1. So indeed it's just bipartiteness check.
But we must be careful: The problem statement says "both sets are vertex cover". In a bipartite graph, each part is a vertex cover? In a bipartite graph, the set of all vertices of one part is a vertex cover? Actually consider a bipartite graph with edges only between L and R. For any edge, it has one endpoint in L and one in R. So L covers all edges (each edge has at least one endpoint in L). Similarly R covers all edges. So yes.
Now, are there any tricky cases where bipartite condition is satisfied but the two sides are not vertex covers because of isolated vertices? No, isolated vertices don't affect.
Thus solution seems trivial: check bipartiteness via BFS/DFS, produce coloring.
But wait: The problem might be more subtle: Could there exist two disjoint vertex covers that are not exactly the bipartition? For example, suppose we have a bipartite graph, but we could choose A and B as subsets of each part, maybe not entire parts, and still both be vertex covers? Possibly, but the problem just asks to find any two disjoint subsets, not necessarily covering all vertices. However, if graph is bipartite, the easiest is to take the whole bipartition. That works and is valid output.
But consider the requirement: "both sets are vertex cover." Could there be a case where graph is bipartite, but one part is empty? Not if edges exist. So fine.
Thus answer: If graph is bipartite, output two sets = the two color classes (all vertices of each color). If graph is not bipartite, output -1.
But is there any scenario where graph is bipartite, but we have to output something else because the bipartition yields two vertex covers but maybe one of them is empty? If the graph has edges but all edges are incident to only one side? In bipartite graph, edges must go across; so both sides have at least one endpoint of some edge. However, there is a degenerate case: n=2, m=1: edge 1-2. Then bipartition yields colors: 0 for 1, 1 for 2. Both sides non-empty.
Now, what about graph with no edges? But problem states m ≥ 1. So at least one edge exists, thus both sides non-empty.
Thus algorithm: Do BFS from each unvisited vertex, assign colors 0/1, detect odd cycle. If odd cycle found -> -1. Else, collect all vertices of color 0 into list A, color 1 into list B. Output sizes and lists.
Complexities: O(n+m).
But we must consider: Could there be a case where the graph is bipartite but we need to output two sets that are disjoint and both are vertex covers, but we might output a vertex that has no edges? That's fine; it's allowed. The cover condition: For each edge, at least one endpoint is in set. Adding extra vertices doesn't break vertex cover property. So it's fine to include isolated vertices in A or B; they don't harm. However, we must ensure that the sets are disjoint; they will be because each vertex is assigned exactly one color.
Potential subtlety: The bipartition might be not unique; we can choose any valid 2-coloring. So it's fine.
Thus problem seems straightforward. But wait: The problem originates from Codeforces? Possibly "Pari and Arya"? I recall a Codeforces problem: 1109C? Not sure. Actually I think this is Codeforces Round #??? There is a problem "Pari and Arya and graph". Might be from Codeforces 688B? Actually I remember a problem "Pari and the graph" or something. I'm not sure. But anyway.
Let's double-check the condition: "both A and B are vertex cover" meaning each edge has at least one endpoint in A, and each edge has at least one endpoint in B. As argued, the only possibility is that each edge has exactly one endpoint in A and one in B (since A and B disjoint). That is correct. So it's equivalent to a proper 2-coloring of the graph, but we also allow vertices to be outside both sets. However, for any edge, the endpoints must be one in A and one in B, so they cannot be outside both. So both endpoints of any edge are forced to be assigned (one to A, one to B). Therefore, all vertices that are incident to at least one edge must be in either A or B. So the bipartition must cover all non-isolated vertices. So indeed the bipartition must assign a color to each vertex that appears in any edge. So it's exactly a 2-coloring of the graph (ignoring isolated vertices). So bipartite condition is necessary and sufficient.
Now, consider graphs that are bipartite but have multiple connected components. In each component, the bipartition is forced up to swapping. So we need to assign colors consistently across all components? Actually no, each component can be colored independently, but the final A and B are the union of color 0 from each component and union of color 1 from each component. That yields a global 2-coloring and still each edge connects opposite colors. So works.
Thus solution stands.
But there is a catch: The problem statement: "Each vertex should be given to no more than one of the friends (or you can even keep it for yourself)." So we can keep vertices. So we are not forced to assign all vertices. But from constraints, we must assign all vertices that are incident to edges because each edge requires one endpoint in A and the other in B. So those vertices must be assigned. So we have no choice but assign all non-isolated vertices. But isolated vertices can be kept.
Now, check the first example: vertex 4 is isolated, not assigned. So that matches.
Edge case: Graph is bipartite but has isolated vertices. Output A and B as described, which include only non-isolated vertices? Actually our coloring will assign each isolated vertex arbitrarily (since no edges, they can be colored any way). But if we assign them arbitrarily, they will be included in either A or B. That's also allowed, because they don't affect the covering property. The problem says we can keep vertices for ourselves. So it's not required to assign isolated vertices. But if we assign them, they would be in either A or B, which is still a valid solution because A and B are still vertex covers (since isolated vertices don't affect). Also they are disjoint. So including them is fine. However, we need to ensure that both sets are non-empty. That's already satisfied.
But the example left the isolated vertex out. So we might want to leave isolated vertices unassigned to mimic the example? The problem doesn't require to minimize or anything. It just requires to find any two disjoint subsets that are vertex covers. So we can include them or not. But we must output exactly the sets A and B; we can't output a vertex in both sets. If we include isolated vertices, it's still valid. But maybe there is a nuance: If we include an isolated vertex in A, then A still covers all edges (since it covers all edges via other vertices). So it's fine.
However, could there be a situation where including an isolated vertex causes the set to become empty? No.
Thus we can just color all vertices (including isolated) via BFS; that will assign them a color, which will be consistent across components. Since isolated vertices have no edges, BFS will assign them arbitrarily (color 0 from start). That yields a coloring; then A = all vertices with color 0, B = all with color 1. That will be valid. But we must ensure that both A and B are non-empty. If there is at least one edge, both colors will appear among vertices of that component. However, consider a graph with an edge and also isolated vertices; if we assign isolated vertices arbitrarily, it's possible that one color class ends up empty? Not if there is at least one edge: because the edge forces at least one vertex in each color among its endpoints. So even if all isolated vertices get assigned to one color, the other color still has at least one vertex from the edge. So both non-empty. So that's fine.
Thus simplest: perform BFS/DFS over all vertices, assign colors. If conflict, output -1. Otherwise output all vertices with color 0 as set A, color 1 as set B.
But we must be careful: When we start BFS on an isolated vertex, we assign it color 0 (say). That vertex will be in A. That's acceptable. But maybe the problem expects that we leave isolated vertices unassigned? The example left it unassigned. But it's not mandatory. In output format, we output two sets; they could include it or not. The judges likely accept any valid solution. However, we should check: Could there be a scenario where including isolated vertices in both sets could cause one set to be empty? No. But there is a more subtle issue: If we assign isolated vertices to both colors, then both sets are still vertex covers, but are there any constraints that the vertex cover must be non-empty? Already satisfied. So it's fine.
But maybe there is a requirement that A and B are disjoint subsets of vertices that are given to friends, and we can keep some for ourselves. If we assign an isolated vertex to A, then we are giving it to a friend, that's allowed. So no problem.
Thus solution stands.
But is it always possible to have A and B as full color classes? Let's think about condition: For each edge, exactly one endpoint in A and one in B. That's satisfied by a proper 2-coloring. So indeed full color classes work.
One more check: Could there be a graph that is bipartite but has multiple components; if we choose the coloring such that in some component we swap colors, would the union still satisfy the property? Yes, because each edge still has endpoints of opposite colors, regardless of global consistency. Actually if we independently color each component, we might end up with a vertex in component 1 colored 0, and in component 2 also colored 0; that's fine, because edges are only within components. So the union of all 0's and all 1's still gives that each edge connects a 0 and a 1. So it's consistent as long as within each component we have a bipartition. So any global assignment that is a proper 2-coloring works.
Thus BFS across entire graph (connected components) yields a global 2-coloring.
Graph with n=2, m=1, plus isolated vertex 3. Bipartite: The edge gives colors: say 1=0,2=1. Vertex 3 isolated can be colored 0 (so A={1,3}, B={2}) or 1 (A={1}, B={2,3}). Both valid.
Graph with only isolated vertices? m=0 but constraints say m>=1, so not needed.
Graph with odd cycle: no solution.
Thus answer is bipartiteness check.
But wait, could there be a graph that is bipartite but still cannot be split into two disjoint vertex covers because we need both vertex covers to be non-empty? Already satisfied. So -1 only when not bipartite.
Thus solution is simple.
However, I recall some Codeforces problem "Pari and the graph" maybe there is a twist: They might require that both A and B are vertex covers but also possibly minimal? No, just vertex cover.
One more nuance: Could there exist two disjoint vertex covers in a non-bipartite graph if we do not require to cover all vertices? Let's try to find a counterexample to my earlier reasoning. The earlier reasoning deduced that for each edge, it must be exactly one endpoint in A and one in B. That deduction used the fact that A and B are disjoint. It seems correct. Let's formalize: For an edge uv, conditions:
A covers uv => u ∈ A or v ∈ A.
B covers uv => u ∈ B or v ∈ B.
A ∩ B = ∅.
Now, suppose u ∈ A. Then u ∉ B because disjoint. So condition 2 reduces to v ∈ B (since u not in B). So we have u ∈ A, v ∈ B.
Similarly, if v ∈ A => u ∈ B.
If neither u nor v in A, fails 1. So indeed each edge must have exactly one endpoint in A and the other in B.
Thus any solution yields a 2-coloring where color A and B are assigned to endpoints. So the graph must be bipartite.
Now, is there any possibility that some vertices are in neither A nor B, but still each edge has one in A and one in B? That forces both endpoints to be assigned, so any vertex that is incident to an edge must be in either A or B. So isolated vertices can be unassigned. So graph must be bipartite on its non-isolated part. So indeed the whole graph must be bipartite (since isolated vertices don't affect cycles). So condition is necessary.
Thus algorithm is as described.
Now, we just need to implement bipartite check and output.
Implementation details:
Input: n,m, then m edges. Build adjacency list.
Use arrays: color[n+1], initialized to -1 (uncolored). Use BFS/DFS.
For each uncolored vertex, set color 0, push to queue. For each neighbor, if color[neighbor] == -1, assign opposite, push. If colored same, then not bipartite -> print -1 and exit.
After BFS, if no conflict, we have color array: 0 or 1 for all vertices (since we colored all components). However, if there are isolated vertices, they get colored as well.
Now, count how many vertices have color 0 -> cnt0. Similarly cnt1.
Since m>=1, both cnt0 and cnt1 are >0? Wait, is it guaranteed that both counts >0? If graph is bipartite and has at least one edge, yes: Because at least one edge's endpoints have different colors, so both colors appear at least once. So cnt0>0 and cnt1>0.
Output:
First set: vertices with color 0: print cnt0, newline, then list of indices (any order). In example, they had a trailing space after the list? Not required but okay.
Second set: vertices with color 1: print cnt1, newline, then list.
We must ensure no vertex appears in both sets (they won't). And each set is vertex cover (by property).
That's all.
But we need to be careful about large input: use fast I/O. Use adjacency list. BFS with queue.
Check for memory: n up to 100k, m up to 100k, adjacency list uses O(n+m). Fine.
Now, confirm with sample:
Sample1:
n=4,m=2 edges: 1-2,2-3. BFS: start 1 color 0; neighbor 2 color 1; neighbor 3 from 2 color 0; vertex 4 isolated, unvisited; after BFS from 1, we have color[1]=0,2=1,3=0. Then start BFS from 4, color 0 (or 1). Suppose we color it 0 (since initial assignment 0). Then color0 set: {1,3,4}? But sample output gave only {2} and {1,3} i.e., they assigned vertex 4 to neither. Our method would assign 4 to color0 (if we start BFS with color0). That yields A={1,3,4}, B={2}. Is that a valid solution? Check: Edges: 1-2 has 1 in A,2 in B -> OK. Edge 2-3: 3 in A,2 in B -> OK. Vertex 4 isolated doesn't affect. Both A and B are vertex covers. So this is also valid. So it should be accepted. So our output may differ but still correct. However, the sample output had a specific assignment: they left 4 unassigned. But our assignment includes 4 in A. That's fine. The problem statement: "Each vertex should be given to no more than one of the friends (or you can even keep it for yourself)." So giving vertex 4 to a friend (Pari or Arya) is allowed. So it's still a valid split. So any valid output is accepted.
Thus our solution is acceptable.
But maybe we need to ensure that both sets are vertex covers, and we must output them in some order? The problem just says print their descriptions; not specifying which is for whom. So order doesn't matter.
Now, consider if graph is bipartite but one part might be empty? Only if m=0, but m>=1 so not.
Thus solution is straightforward.
But wait: There is a nuance: In the problem statement, they say: "Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. u∈A or v∈A (or both)." So the standard definition. Then they want A and B both to be vertex covers, and disjoint. In bipartite graph, each part is a vertex cover. So we are good.
Thus answer: -1 if not bipartite, else output bipartition.
Now, what about the possibility that graph is bipartite but there is an edge that has both endpoints in the same part if we do a coloring? No, that would contradict bipartite definition.
Thus solution is done.
But given the problem is from Codeforces maybe there is a twist: They might require that both vertex covers are non-empty, and also maybe they require that each vertex is assigned to exactly one of the two friends or kept, but the two sets must be disjoint. So the bipartition yields exactly that.
Thus I'll implement.
Let's test with some custom cases:
Case: n=5,m=4 edges: 1-2,2-3,3-4,4-5 (path length 5). Bipartite: colors alternate. Output sets: {1,3,5} and {2,4}. Both are vertex covers.
Case: n=3,m=3 triangle: not bipartite -> -1.
Case: n=100000, star: center 1 connected to all others. Bipartite: center color 0, leaves color 1. Sets: {1} and {2..n}. Both covers: center covers all edges, leaves also cover all edges? Check: set of leaves: does it cover all edges? Each edge is between center and a leaf. Leaves set contains the leaf endpoint, so each edge has leaf in leaves set. So yes. So output {1} and {2..n}. That's valid. Both non-empty.
Edge: if the graph is disconnected, still works.
Now, consider a graph with a component that is bipartite but another component that is an isolated vertex. BFS will color the isolated vertex as 0 or 1; but that might make one of the sets have only isolated vertices? But if the other component has edges, both parts have at least one vertex from that component, so both non-empty regardless.
Thus algorithm robust.
Complexity: O(n+m).
Now, we'll produce final answer.
But hold on: We must also ensure that each set is not empty. If by some chance after coloring, one of the sets is empty (e.g., if there is an edge but both endpoints get same color due to error?), but that's only if not bipartite.
Now, double-check with special case: Graph with multiple components, one component has edge and is bipartite, another component is just isolated vertices. Suppose we color isolated vertices all to the same color, say color 0. Then color 0 set has at least one vertex from the edge component? Actually from edge component, both colors appear. So color 0 set will have at least one vertex from that component, so non-empty. Similarly color 1 set will have at least one vertex from that component. So fine.
Now, is there any scenario where the graph is bipartite but all edges are such that one of the colors appears only in vertices that are isolated? No, because if there is an edge, its endpoints have two different colors, so both colors appear among the endpoints. So both sets contain at least one endpoint from that edge.
Thus all good.
Now, the problem's note: "Note that because of m ≥ 1, vertex cover cannot be empty." That just emphasizes we need non-empty sets.
Implementation:
We'll read n,m.
adj = vector<vector>(n+1).
for i in 0..m-1: read u,v; adj[u].push_back(v); adj[v].push_back(u).
color = vector(n+1, -1). // -1 uncolored, 0 and 1 colors.
bool ok = true;
for v in 1..n:
if color[v]==-1:
// BFS/DFS
color[v] = 0;
queue q; q.push(v);
while not empty:
int cur = q.front(); q.pop();
for nb in adj[cur]:
if color[nb]==-1:
color[nb] = 1 - color[cur];
q.push(nb);
else if color[nb]==color[cur]:
ok = false; break;
if !ok break.
If !ok: print -1.
Else:
vector list0, list1;
for i in 1..n:
if color[i]==0) list0.push_back(i);
else list1.push_back(i);
// print
print list0.size()
print list0 elements separated by space (maybe newline after each? Actually format: first line k, second line k integers)
print list1.size()
print list1 elements
Note: Some caution: The output format in sample: "1\n2 \n2\n1 3 \n" they have spaces after numbers. We can just print numbers with spaces, maybe no trailing space. Acceptable.
Now, consider large output: Use printf or fast I/O in C++. Use endl or '\n'. Use vector and iterate.
Potential issue: Memory consumption for adjacency list: vector of vectors. Fine.
Now, is there any more subtle condition? Let's read problem statement again: "Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover." It might be interpreted that they want to partition the vertex set into two disjoint subsets A and B (each friend gets a set) and the rest unassigned. But the condition "both parts of the graph to be a vertex cover" means the induced subgraphs? Or the set of vertices they receive should be a vertex cover of the original graph? Usually, "their parts of the graph" might mean the vertices they receive, and they want that the set of vertices they receive is a vertex cover for the original graph. That matches our interpretation.
Thus we are good.
However, there is a variant: Could there be a solution where A and B are vertex covers but not necessarily covering all edges with both endpoints assigned? Our deduction shows it's forced to exactly one each.
Thus done.
But wait, there is one more nuance: The vertex cover definition: "Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set". So if A is a vertex cover, then every edge has at least one endpoint in A. In our assignment, each edge has exactly one in A and one in B, so it's fine.
Thus solution stands.
Now, let's consider if there is any possibility of having two disjoint vertex covers in a non-bipartite graph if we allow some vertices to be in both? No, they must be disjoint. So no.
Thus answer is simply bipartite check.
But could the graph be bipartite but one of the parts is not a vertex cover? No, it is.
Thus final answer.
But before finalizing, I'll test with some random bipartite graphs to ensure algorithm yields correct sets.
Let's do a small code mental test:
n=4,m=3 edges: 1-2,2-3,3-4 (path of length 3). Bipartite: colors: 1=0,2=1,3=0,4=1. Sets: {1,3} and {2,4}. Check: edge 1-2: 1 in A,2 in B -> A covers (1), B covers (2). Edge 2-3: 2 in B,3 in A -> A covers (3), B covers (2). Edge 3-4: 3 in A,4 in B -> A covers (3), B covers (4). So both sets are vertex covers. Good.
Now consider a graph with multiple edges? Not allowed.
Thus solution correct.
Now, ensure we treat graph with self-loops? Not allowed.
Now, what about graph with a vertex that has no edges? It's isolated. As argued, including it is fine.
But could there be a scenario where the graph is bipartite but all vertices are isolated? m=0 not allowed.
Thus fine.
Now, complexity: BFS O(n+m). Output O(n). Good.
Now, let's think about potential pitfalls: The BFS may be done on a graph with up to 100k nodes and edges; recursion depth if DFS could cause stack overflow; but we use BFS with queue. That's fine.
Now, one more nuance: In bipartite check, we must ensure we traverse all components. Already done.
Now, about memory: adjacency list as vector of vectors of int. Use .resize(n+1) or just push_back.
Now, output: print the sizes and lists. In the example, they printed a space after last number? That's not necessary. We can use:
But simpler: print size, then for each print number + space, then after loop print newline. It's fine if trailing space; judges usually ignore.
Now, final answer: code.
But I want to reflect: The problem might be from Codeforces Round #688 (Div. 2) problem C? Actually I recall a problem "Pari and the Multiple of Something"? Not sure.
Anyway.
Now, consider a graph that is bipartite but has a component with an odd cycle? No.
Thus answer.
But let's consider if there's any possibility of a graph that is not bipartite but still can be split into two disjoint vertex covers if we allow vertices to be in both? The problem says "disjoint". So no.
Thus final solution is trivial.
But maybe the problem expects to find not just any bipartition but something else? Could be that we need to output two disjoint vertex covers, but they don't have to be the full bipartition; they could be smaller subsets. However, since the condition forces that every vertex incident to an edge must be in either A or B (as argued), the bipartition is forced: the set of vertices that are assigned must be exactly the set of vertices incident to edges (maybe plus some isolated). So if we don't assign some non-isolated vertex, we would violate the condition because that vertex's incident edges would need the opposite endpoints in A and B, but if that vertex is unassigned, then for each edge incident to it, the other endpoint would have to be in both A and B? Let's examine: Suppose vertex u is not in A nor B, but has neighbor v. Edge uv: A must cover => u in A or v in A. Since u not in A, v must be in A. B must cover => u in B or v in B. Since u not in B, v must be in B. So v must be in both A and B, but A and B are disjoint. Contradiction unless there is no such edge. So any vertex incident to an edge must be assigned to either A or B. So indeed, the set of assigned vertices is exactly the set of vertices that have degree > 0 (non-isolated). So all non-isolated vertices are forced to be in either A or B, with the condition that edges force a proper 2-coloring. So the assignment is essentially a 2-coloring of the subgraph induced by non-isolated vertices. Therefore, the graph must be bipartite on that subgraph. Since isolated vertices don't affect bipartiteness, overall graph must be bipartite.
Thus no other solution.
Hence final code.
But wait, could there be a scenario where the graph is bipartite but we cannot assign all non-isolated vertices to A and B because then one of the sets might be empty? Already covered.
Now, check if there is any nuance about "both sets are vertex cover" meaning that each set must be a vertex cover of the original graph, not necessarily that each edge has both endpoints assigned? The argument holds.
Thus we have solution.
Now, let's double-check the problem statement from the user: "Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both)." So it's standard.
Thus final answer.
But is there any possibility that the problem expects us to output the two vertex covers such that they are minimal? Not required.
Now, implement.
I'll write in C++.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
code
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n+1);
for (int i=0; i<m; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> color(n+1, -1);
bool bipartite = true;
for (int i=1; i<=n; ++i) {
if (color[i] == -1) {
queue<int> q;
color[i] = 0;
q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
if (color[v] == -1) {
color[v] = 1 - color[u];
q.push(v);
} else if (color[v] == color[u]) {
bipartite = false;
break;
}
}
if (!bipartite) break;
}
if (!bipartite) break;
}
}
if (!bipartite) {
cout << "-1\n";
return 0;
}
vector<int> set0, set1;
for (int i=1; i<=n; ++i) {
if (color[i] == 0) set0.push_back(i);
else set1.push_back(i);
}
// Since m>=1, both sets should be non-empty; but just in case (if graph has no edges?), we check.
if (set0.empty() || set1.empty()) {
// This shouldn't happen because m>=1, but just to be safe:
// If one set empty, we can move an isolated vertex from the other set? But isolated vertices only appear if no edges, but m>=1 ensures there is at least one edge, so both sets non-empty. So we don't handle.
cout << "-1\n";
return 0;
}
cout << set0.size() << '\n';
for (size_t i=0; i<set0.size(); ++i) {
if (i) cout << ' ';
cout << set0[i];
}
cout << '\n';
cout << set1.size() << '\n';
for (size_t i=0; i<set1.size(); ++i) {
if (i) cout << ' ';
cout << set1[i];
}
cout << '\n';
return 0;
}
Now, check: For the sample, if we color isolated vertex 4 as 0, we output: set0 size 3, list 1 3 4; set1 size 1, list 2. That is different from sample but should be accepted. The problem statement says "If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions." So any valid description is fine. So no issue.
Now, we must ensure that we don't accidentally output a set that is not a vertex cover if the graph is bipartite but we colored isolated vertices in a way that one set becomes empty? Not possible because edge forces both colors present.
But consider a graph with exactly one edge and isolated vertices. Suppose we start BFS from one endpoint of the edge, we assign it color 0; its neighbor gets color 1; then later we color isolated vertices. When we encounter isolated vertex later (maybe from for loop), we start BFS with it: since no edges, it will be assigned color 0. So both colors exist (0 from first vertex and isolated, 1 from neighbor). So non-empty.
But if we started BFS on an isolated vertex first, we assign it color 0; then later process the edge component, we might assign its vertices colors 0 and 1 as well. But note: when we start BFS on the edge component, the vertices may already be uncolored. But if we start with isolated vertex, color it 0; then when we reach the edge component, we start BFS from one endpoint, it's uncolored, assign color 0; then its neighbor gets 1. So final colors: both endpoints of edge may be 0 and 1; isolated vertex is 0. So set0 includes both edge endpoint (the one colored 0) and isolated; set1 includes the other endpoint. So both non-empty.
Thus safe.
Now, one more nuance: The BFS coloring algorithm may produce a coloring that is not consistent across disconnected components? Actually each component is colored independently; there's no requirement that the global assignment is a proper 2-coloring across components because there are no edges between components. So it's fine. As long as within each component it's properly 2-colored.
Thus all good.
Now, is there any chance that the graph is bipartite but we might output one set that does not cover all edges? In our assignment, set0 covers all edges? Since each edge has exactly one endpoint in set0 (the 0-colored vertex) and one in set1, yes set0 covers all edges. So it's a vertex cover.
Now, the problem states: "Note that because of m ≥ 1, vertex cover cannot be empty." So we need to ensure our sets are non-empty. Since we have at least one edge, both sets contain at least one endpoint, so it's fine.
Now, final answer: Provide solution description and code.
But the user asked to "Recently, Pari and Arya did some research...". They might want a solution explanation? The user is presumably asking for solution help. But as ChatGPT, we should output the answer? The conversation seems like a problem statement. The user likely expects a solution code or explanation. Since it's a typical competitive programming problem, I can produce a solution explanation and code.
Given the instructions: "You are ChatGPT...". The user asked a question: "Recently, Pari and Arya did some research about NP-Hard problems ...". They might be pasting a problem statement and expecting solution? Probably they want a code or explanation. In this context, we can provide the solution reasoning and code.
Thus final output: explanation that it reduces to checking bipartiteness, and then code.
I'll produce final answer accordingly.
But before finishing, I'll re-check sample 2: triangle not bipartite, output -1. Good.
Now, check more complex: A graph with a component that is bipartite and another component that is an odd cycle? That makes the whole graph non-bipartite, so -1. So correct.
Now, check if there is any possibility that a non-bipartite graph still can be split because we can leave some vertices unassigned and assign others such that each edge has one in A and one in B? Our deduction showed that if a vertex is not assigned, all its neighbors must be assigned both A and B, impossible. So indeed any vertex incident to an edge must be assigned. Thus all vertices in non-bipartite components must be assigned, leading to contradiction. So -1.
Thus solution is final.
Now, potential pitfalls: Could there be a graph with multiple edges? Not allowed. So fine.
Now, about memory: using vector of vectors, but we can also use list for adjacency. For n=100k, m=100k, it's fine.
Now, code in C++.
We also need to ensure we use fast I/O.
Now, final output.
I think it's correct.
But wait, one more nuance: The problem says "both A and B are vertex cover". Could A and B be the same set? No, they must be disjoint. So no.
Now, we are done.
Now, we can also consider a solution in Python for readability, but given constraints, Python might be okay with efficient implementation, but C++ is safer. Usually Codeforces problems, C++ solution. The user didn't specify language, but we can provide a C++ solution.
Given the environment of ChatGPT, it's fine.
Thus final answer: Explanation + C++ code.
I will now produce final answer. The problem reduces to checking whether the graph is bipartite.
If it is not bipartite, it is impossible to have two disjoint vertex covers.
If it is bipartite, the two color classes form two disjoint vertex covers (each edge connects vertices of different colors, so both classes cover all edges).
We perform a BFS/DFS to 2‑color the graph. If we encounter an edge whose endpoints have the same color, we output -1.
Otherwise we output all vertices of color 0 as the first set and all vertices of color 1 as the second set.
The graph has at least one edge, so both sets are non‑empty.
cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n + 1);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> color(n + 1, -1); // -1 = uncolored, 0 / 1 = colors
bool bipartite = true;
for (int i = 1; i <= n && bipartite; ++i) {
if (color[i] == -1) {
queue<int> q;
color[i] = 0;
q.push(i);
while (!q.empty() && bipartite) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
if (color[v] == -1) {
color[v] = 1 - color[u];
q.push(v);
} else if (color[v] == color[u]) {
bipartite = false;
break;
}
}
}
}
}
if (!bipartite) {
cout << "-1\n";
return 0;
}
vector<int> set0, set1;
for (int i = 1; i <= n; ++i) {
if (color[i] == 0) set0.push_back(i);
else set1.push_back(i);
}
// Because m >= 1, both sets are guaranteed to be non‑empty.
cout << set0.size() << '\n';
for (size_t i = 0; i < set0.size(); ++i) {
if (i) cout << ' ';
cout << set0[i];
}
cout << '\n';
cout << set1.size() << '\n';
for (size_t i = 0; i < set1.size(); ++i) {
if (i) cout << ' ';
cout << set1[i];
}
cout << '\n';
return 0;
}