原题:

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

codeforces 360 C - NP-Hard Problem的更多相关文章

  1. [codeforces 360]A. Levko and Array Recovery

    [codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...

  2. 【codeforces 442B】 Andrey and Problem

    http://codeforces.com/problemset/problem/442/B (题目链接) 题意 n个人,每个人有p[i]的概率出一道题.问如何选择其中s个人使得这些人正好只出1道题的 ...

  3. Codeforces Gym 100015A Another Rock-Paper-Scissors Problem 找规律

    Another Rock-Paper-Scissors Problem 题目连接: http://codeforces.com/gym/100015/attachments Description S ...

  4. 屏蔽Codeforces做题时的Problem tags提示

    当在Codeforces上做题的时,有时会无意撇到右侧的Problem tags边栏,但是原本并不希望能够看到它. 能否把它屏蔽了呢?答案是显然的,我们只需要加一段很短的CSS即可. span.tag ...

  5. codeforces#253 D - Andrey and Problem里的数学知识

    这道题是这种,给主人公一堆事件的成功概率,他仅仅想恰好成功一件. 于是,问题来了,他要选择哪些事件去做,才干使他的想法实现的概率最大. 我的第一个想法是枚举,枚举的话我想到用dfs,但是认为太麻烦. ...

  6. Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读

    http://codeforces.com/contest/426/problem/B 对称标题的意思大概是.应当指出的,当线数为奇数时,答案是线路本身的数 #include<iostream& ...

  7. Codeforces 803 G. Periodic RMQ Problem

    题目链接:http://codeforces.com/problemset/problem/803/G 大致就是线段树动态开节点. 然后考虑到如果一个点还没有出现过,那么这个点显然未被修改,就将这个点 ...

  8. Codeforces 903G Yet Another Maxflow Problem - 线段树

    题目传送门 传送门I 传送门II 传送门III 题目大意 给定一个网络.网络分为$A$,$B$两个部分,每边各有$n$个点.对于$A_{i} \ (1\leqslant i < n)$会向$A_ ...

  9. Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分

    Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...

随机推荐

  1. js数组常用方法汇总

    判断某个对象是否是数组: instanceof.Array.isArray() 对于一个网页或者一个全局作用域可以使用instanceof操作符. if(value instanceof Array) ...

  2. Linux的sleep()和usleep()的使用和区别

    Linux的sleep()和usleep()的使用和区别 函数名: sleep头文件: #include <windows.h> // 在VC中使用带上头文件 #include <u ...

  3. unity, 顶点对齐

    按住v键,选中物体的一个顶点,可以对齐到其它物体的某个顶点上. 参考https://docs.unity3d.com/Manual/PositioningGameObjects.html

  4. Servlet与JSP的区别

    一.基本概念 1.1 Servlet Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面.它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器 ...

  5. UIView的使用

    UIView是iOS中所有视图的基类,表示屏幕上的一块矩形区域. UIView的基本属性包括: 1.frame,控制视图的显示位置和大小 2.backgroundColor,控制视图的背景颜色 3.a ...

  6. IIS:日志代码分析

    如何看IIS日志代码,打开IIS日志后,你会看见里面有很多访问记录.baiduspider,Googlebot等就是蜘蛛了.蜘蛛爬过后都会留下记录的,状态代码列在下面: 100 - 表示已收到请求的一 ...

  7. My Package

    一.新建一文件夹,名称为MyBase,存放Java的基本类. 二.在MyBase包中创建基本类Base.java. package MyBase; public class Base { public ...

  8. ORM框架

    半自动:iBATIS是一个半自动化的ORM框架,需要通过配置方式指定映射SQL语句 全自:由框架本身生成(如Hibernate自动生成对应SQL来持久化对象),即Hibernate属于全自动ORM框架 ...

  9. IntelliJ IDEA使用记录

    一.快捷键 1. 生成main方法 在编写代码的时候直接输入psv就会看到一个psvm的提示,此时点击tab键一个main方法就写好了. psvm 也就是public static void main ...

  10. 设置ajax 同步执行

    Ajax请求默认的都是异步的如果想同步 async设置为false就可以(默认是true) var html = $.ajax({  url: "some.php",  async ...