原题:

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 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 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 + ;
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 = ; i < G[u].size(); i++) {
int v = G[u][i];
// 如果下一个点没涂过色的话,为其涂上不同的颜色
if(color[v] == ) {
color[v] = c ^ ;
dfs(v, c ^ );
}
// 如果下一个点跟当前点颜色相同的话,失败返回
if(color[v] == c) {
ok = false;
return;
}
}
} int main() {
scanf("%d%d", &n, &m);
for(int i = ; i < m; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
// 为每个连通分量涂色
for(int i = ; i <= n; i++) {
if(color[i] == ) {
color[i] = ;
dfs(i, );
}
}
// 输出失败或A和B
if(ok == false) {
puts("-1");
}
else {
for(int i = ; i <= n; i++) {
if(color[i] == ) {
v1.push_back(i);
}
if(color[i] == ) {
v2.push_back(i);
}
}
printf("%d\n", v1.size());
for(int i = ; i < v1.size(); i++) {
printf("%d ", v1[i]);
}
printf("\n%d\n", v2.size());
for(int i = ; i < v2.size(); i++) {
printf("%d ", v2[i]);
}
puts("");
}
return ;
}

最新文章

  1. JS 的事件委托机制
  2. Linux中Screen命令使用方法
  3. 闭包-&gt;类的实例数组排序
  4. 1.Windows安装PostgreSQL
  5. 原生javascript-常用的函数
  6. Git版本管理:Windows下Git配置与使用指南
  7. ubuntu1304下安装boa服务器
  8. IE-“无法浏览网页” 教你十招解决疑难杂症
  9. AOJ/堆与动态规划习题集
  10. Eclipse常用快捷键记录
  11. TCP的TIME_WAIT
  12. 【转】 Keil C51重定向printf到串口
  13. Tomcat学习总结(2)——Tomcat使用详解
  14. 《Just for Fun》---读后感
  15. Shader 学习工具篇 可视化公式工具ZGrapher
  16. androidcookie存储sqllite
  17. Qt应用程序重启
  18. Redis学习-redis概述
  19. 谈谈Ajax(一)
  20. 如何实现 Copying derived entities using only base class pointer

热门文章

  1. java.lang.ClassCastException: java.lang.String cannot be cast to com.jy.hfims.domain 映射实体类型错误
  2. viewpager接受值图片轮播
  3. MVC 全局异常处理及禁用显示头
  4. [转]MySQL服务器上添加一个允许远程访问的用户
  5. 【C】 01 - 再学C语言
  6. urlrewriter的使用
  7. PHP中float变量转换为int时,结果有误的问题!
  8. python(26)查看文件的大小
  9. c#调用Mysql带参数的存储过程
  10. ios 屏幕概况