NP-Hard Problem(二分图染色)

来源:互联网 发布:勒索软件 中国 编辑:程序博客网 时间:2024/05/29 03:12

NP-Hard Problem

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 0001 ≤ 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 kintegers — 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.


问题分析:利用dfs进行染色标记判断是否为二分图,二分图染色法,先给一个color数组全部赋值为-1,然后dfs开始染色,每次都染成一个Color颜色,第一个Color颜色设置为0,然后依次设置为1,0,1.....这样就可以把每个没个点分成0,1两组,这里有个方法就是递归的时候传递的color参数为color^1,就可以让1变为0,0变为1,然后如果 dfs中发现该点已经染色,判断是否已经染成该染的颜色,如果是该染的颜色,直接return,否则flag设置为false,return。

第一个例子运行结果与例子结果不一样,网上找了很多,也是这样……不知道是不是bug……

#include<stdio.h>#include<string.h>#include<iostream>#include<vector>using namespace std;const int maxn = 100010;vector<int>v[maxn],ans[2];int color[maxn];bool flag;void dfs(int i,int Color)//i为顶点序号,Color为涂的颜色 {if (color[i] != -1)//如果颜色不匹配,不能二分,否则已有相应颜色,就跳过 {if (color[i] != Color)//如果颜色不匹配,说明不能够二分flag = false;return ;}color[i] = Color;ans[Color].push_back(i);//将顶点i归入颜色为Color的一边、for(int j=0; j<v[i].size(); j++) //枚举与顶点i相连接的点,进行染色 {dfs(v[i][j],Color^1);if (flag == false)return ; }}int main(){int n,m;int u,w;scanf("%d%d",&n,&m);for(int i=0; i<m; i++){scanf("%d%d",&u,&w);v[u].push_back(w);v[w].push_back(u);flag = true;memset(color,-1,sizeof(color));//表示都没有涂色 for(int i=1; i<=n; i++){if (color[i] == -1 )dfs(i,0);}if (flag == false)printf("-1\n");else{for(int i=0; i<2; i++){printf("%d\n",ans[i].size());for(int j=0; j<ans[i].size(); j++)printf("%d%c",ans[i][j],j==ans[i].size()-1?10:32);}}return 0;}


0 0