原题:

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. python-内置函数、装饰器

    本节内容:一之前课程回顾: 在书写代码的时候,先写简单的逻辑在写复杂的逻辑.概念梳理:1.函数在传递实参的时候是传递的是引用而不是从内存中重新赋相同值给形参.比如: def test(x): x.ap ...

  2. vi 编辑器命令 (share)

    转自:http://man.ddvip.com/soft/vieditor/vi.html 一.Unix编辑器概述 编辑器是使用计算机的重要工具之一,在各种操作系统中,编辑器都是必不可少的部件.Uni ...

  3. Erlang 104 OTP

    笔记系列 Erlang环境和顺序编程Erlang并发编程Erlang分布式编程YawsErlang/OTP 日期              变更说明 2014-12-21 A Outline, 1 A ...

  4. 分支语句 if的嵌套 循环语句

    0930 今天学习内容做以下总结: 语句的分类:顺序语句,分支语句(选择,条件),循环语句 分支语句 格式1:if(表达式(要么是true 要么是false)){} 格式2:if(){}slse{}  ...

  5. 如何在JBoss WildFly 8 自定义log4j日志

    最近在 JBoss WildFly 8 下部署 Web应用,自定义的 log4j 日志不工作.console下无日志输出,用System.out.println都不输出内容到console. 原因是J ...

  6. JQUERY操作css与css()方法、获取设置尺寸;

    一.jQuery addClass() 方法 向不同的元素添加 class 属性.在添加类时,您也可以选取多个元素 <style> .aa { color:red; }; </sty ...

  7. Ogre1.6.5 编译链接错误之FreeImage

    这两天想重新学习下ogre,但是在vs2010上编译1.6.5的版本上遇到链接失败的问题,耗了不少时间这里记一下. 主要是一些重定义报错. >msvcprtd.lib(MSVCP100D.dll ...

  8. QQ空间漫步者

    主要功能(QQ空间) 判断空间权限并跳过无法访问 留下足迹并可选:同时留言(可单独),赞主页(可单独),赞说说(可单独) 其他附加功能,导出QQ,导入群成员,好友,空间访客,说说评论,发表说说 送空间 ...

  9. LeetCode 【190. Reverse Bits】

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  10. MSP430设置串口波特率的方法

    给定一个BRCLK时钟源,波特率用来决定需要分频的因子N:               N = fBRCLK/Baudrate 分频因子N通常是非整数值,因此至少一个分频器和一个调制阶段用来尽可能的接 ...