您的位置:首页 > 其它

CodeForces 687A NP-Hard Problem

2016-07-28 08:28 483 查看
A. NP-Hard Problem

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 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


题目大意:给出一个图G,将顶点染成两种颜色,要求有边相连的顶点颜色不同。如果这种方案存在,输出两种颜色的顶点数及每个顶点;不存在则输出-1。

解题思路:根据给定条件建图,然后DFS,利用辅助数组标记染成的颜色,将每种颜色的结果放入变长数组ans中,最后输出ans的长度及每个元素即可。

代码如下:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100005;
bool flag = 1;
int color[maxn];
vector<int> graph[maxn];
vector<int> ans[2];
void init(int n)
{
for(int i = 1;i <= n;i++)
graph[i].clear();
}

void addedge(int u,int v)
{
graph[u].push_back(v);
}
int res;
void dfs(int u,int col)
{
//res++;
if(!flag)
return ;
if(color[u] != -1){
if(color[u] != col)
flag = false;
return ;
}
color[u] = col;
ans[col].push_back(u);
for(int i = 0;i < graph[u].size();i++){
dfs(graph[u][i],1 ^ col);
}
}

int main()
{
int n,m;
int u,v;

while(scanf("%d %d",&n,&m) != EOF){
init(n);
flag = 1;
res = 0;

for(int i = 0;i < m;i++){
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
}
memset(color,-1,sizeof(color));
for(int i = 1;i <= n;i++){
if(color[i] == -1){
dfs(i,0);
}
}

if(flag){
printf("%d\n",ans[0].size());
for(int i= 0;i < ans[0].size();i++){
if(i)
printf(" ");
printf("%d",ans[0][i]);
}
printf("\n");
printf("%d\n",ans[1].size());
for(int i= 0;i < ans[1].size();i++){
if(i)
printf(" ");
printf("%d",ans[1][i]);
}
printf("\n");
}
else
printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CodeForces