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

题目链接:http://codeforces.com/contest/688/problem/C


题意:一个由n个点m条边组成的图,把他分成二部图。

思路:DBF或者BFS暴力搜索,当前这个点在一组,与他相邻的点必须在另一组。不符合就输出-1,符合输出2组点。

代码:

 #include<bits/stdc++.h>
using namespace std;
struct node
{
int from;
int to;
int d;
int next;
} edge[];
int head[];
void add(int i,int u,int v,int d)
{
edge[i].from=u;
edge[i].to=v;
edge[i].d=d;
edge[i].next=head[u];
head[u]=i;
}
int sign[];
int ans,sum1,sum2;
queue<int>Q;
void BFS(int u)
{
while(!Q.empty()) Q.pop();
Q.push(u);
while(!Q.empty())
{
u=Q.front();
Q.pop();
int i=head[u];
while(i!=)
{
if(sign[edge[i].from]==&&sign[edge[i].to]==)
{
sum2++;
sign[edge[i].to]=;
Q.push(edge[i].to);
}
else if(sign[edge[i].from]==&&sign[edge[i].to]==)
{
sum1++;
sign[edge[i].to]=;
Q.push(edge[i].to);
}
else if(sign[edge[i].from]==&&sign[edge[i].to]==) ans=;
else if(sign[edge[i].from]==&&sign[edge[i].to]==) ans=;
if(ans==) break;
i=edge[i].next;
}
if(ans==) break;
}
}
int main()
{
int i,j,n,m;
scanf("%d%d",&n,&m);
memset(head,,sizeof(head));
for(i=,j=; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(j++,u,v,);
add(j++,v,u,);
}
ans=;
sum1=sum2=;
memset(sign,,sizeof(sign));
for(i=; i<=n; i++)
{
if(sign[i]!=) continue;
sign[i]=;
sum1++;
BFS(i);
}
if(ans==)
{
cout<<sum1<<endl;
for(i=; i<=n; i++)
if(sign[i]==) cout<<i<<" ";
cout<<endl<<sum2<<endl;
for(i=; i<=n; i++)
if(sign[i]==) cout<<i<<" ";
cout<<endl;
}
else cout<<"-1"<<endl;
return ;
}

BFS暴搜

 #include<bits/stdc++.h>
using namespace std;
vector<int>v[];
queue<int>Q;
int sign[];
int ans,sum1,sum2;
void BFS(int u)
{
while(!Q.empty()) Q.pop();
Q.push(u);
while(!Q.empty())
{
u=Q.front();
Q.pop();
for(int i=; i<v[u].size(); i++)
{
if(sign[v[u][i]]==sign[u])
{
ans=;
return;
}
else if(sign[v[u][i]]==)
{
if(sign[u]==)
{
sum2++;
sign[v[u][i]]=;
}
else
{
sum1++;
sign[v[u][i]]=;
}
Q.push(v[u][i]);
}
}
}
}
int main()
{
int i,n,m,a,b;
scanf("%d%d",&n,&m);
memset(sign,,sizeof*(sign));
for(i=; i<m; i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
ans=;
for(i=; i<=n; i++)
{
if(sign[i]!=) continue;
sign[i]=;
sum1++;
BFS(i);
if(ans==) break;
}
if(ans==)
{
cout<<sum1<<endl;
for(i=; i<=n; i++)
if(sign[i]==) cout<<i<<" ";
cout<<endl<<sum2<<endl;
for(i=; i<=n; i++)
if(sign[i]==) cout<<i<<" ";
cout<<endl;
}
else cout<<"-1"<<endl;
}

BFS暴搜(vector)

最新文章

  1. MyBatis源码分析(2)—— Plugin原理
  2. BZOJ4551——[Tjoi2016&amp;Heoi2016]树
  3. my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post 分类: leetcode 2015-08-01 14:35 113人阅读 评论(0) 收藏
  4. 第二章 Mysql 数据类型简介--(整数类型、浮点数类型和定点数类型,日期与时间类型,字符串类型,二进制类型)
  5. Java三大特征之------多态
  6. Linux系统(X64)安装Oracle11g完整安装图文教程另附基本操作
  7. int型长度
  8. Media Queries详解--转
  9. EC读书笔记系列之10:条款16、17
  10. MySql监控优化
  11. 【原创】大叔经验分享(24)hive metastore的几种部署方式
  12. Golang 微框架 Gin 简介
  13. openwrt如何关掉防火墙?
  14. openstack nova工作流程
  15. Java面向对象三大特性
  16. [android] 手机卫士手势滑动切换屏幕
  17. HTML学习-1网页基础知识
  18. Testlink定位实例
  19. REST API 开发
  20. Python系列6之面向对象

热门文章

  1. Poi对excel的基本操作
  2. opencv3.1+contrib的配置大总结(配置了两天,遇到问题无数)
  3. html调用静态json例子
  4. MPI 环境配置,MPICH,VisualStudio
  5. scala.XML处理
  6. jpa 一对一
  7. J2SE 8的Lambda --- Comparator
  8. javascript时间日期操作
  9. PYTHON-进阶-装饰器小结,转载
  10. Struts和Hibernate使用总结