原题:
Description
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex coverproblem 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.
Sample Input
Input
4 21 22 3
Output
12 21 3
Input
3 31 22 31 3
Output
-1 提示:首先,这是个图,图就很容易想到深搜遍历和广搜遍历啊!!~~ 没错,这题可以用深搜遍历。这里的每个点都可以连接多个点,相当于深搜里的多个方向。从第一个点开始搜,遍历所有“相连的点”。储存时,为了达成上述的数据结构,以顶点为基础,建立vector <int> Vertex[ ] 下标对应顶点编号。存储与之相连的点。2种颜色表示不同的人,非1即2 (用 1^3 2^3 可以实现相互转化)或者非0即1(0^1 1^1 可以实现相互转化)ps:这是通过着手顶点来做的,还不知道可不可以着手 边 去做。(遍历所有的边) 代码:
#include<cstdio> #include<cstring> #include<iostream> using namespace std; #define ABS(x) ((x)>0?(x):-(x)) #define ll long long const int maxn = 1e5 + 10; bool ok = true; int n, m, u, v, color[maxn]; vector <int> v1, v2, G[maxn]; // color[u]的值为1和2分别代表为u涂上两种不同的颜色 void dfs(int u, int c) { for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; // 如果下一个点没涂过色的话,为其涂上不同的颜色 if(color[v] == 0) { color[v] = c ^ 3; dfs(v, c ^ 3); } // 如果下一个点跟当前点颜色相同的话,失败返回 if(color[v] == c) { ok = false; return; } } } int main() { scanf("%d%d", &n, &m); for(int i = 0; i < m; i++) { scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } // 为每个连通分量涂色 for(int i = 1; i <= n; i++) { if(color[i] == 0) { color[i] = 1; dfs(i, 1); } } // 输出失败或A和B if(ok == false) { puts("-1"); } else { for(int i = 1; i <= n; i++) { if(color[i] == 1) { v1.push_back(i); } if(color[i] == 2) { v2.push_back(i); } } printf("%d\n", v1.size()); for(int i = 0; i < v1.size(); i++) { printf("%d ", v1[i]); } printf("\n%d\n", v2.size()); for(int i = 0; i < v2.size(); i++) { printf("%d ", v2[i]); } puts(""); } return 0; }