原题:

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. s查找父节点

    查找所有的父节点,包括本身,不包括就<>id with tbs as(select * from TB_HomeBase where ID=223 union all select a.* ...

  2. Candy Store

    Candy Store Time Limit: 30000ms, Special Time Limit:75000ms, Memory Limit:65536KB Total submit users ...

  3. Hibernate一对多 特殊的数据类型的注解

    1.String 类型 @ElementCollection(targetClass = String.class,fetch = FetchType.EAGER) @CollectionTable( ...

  4. MongoDB学习笔记一:入门

    文档:多个键及其关联的值『有序』地放置在一起. {"greeting" : "Hello, world!", "foo" : 3}集合:一组 ...

  5. Python 基础语法(三)

    Python 基础语法(三) --------------------------------------------接 Python 基础语法(二)------------------------- ...

  6. spring-实现配置文件读取

    spring 实现配置读取 Java 的配置读取方式一般是采用java.utils.Properties 或是apache的Configuration工具:然而 spring 框架内置了配置文件的读取 ...

  7. [linux] FastDFS访问文件,400 Bad Request

    linux 安装nginx,FastDFS后,启动访问指定文件出错, 文件名称格式化错误. 解决办法: vi /etc/fdfs/mod_fastdfs.conf 将 url_have_group_n ...

  8. CentOS 6.8下安装MySQL 5.6.33

    此处操作,包含MySQL的客户端及服务端. MySQL下载地址: http://dev.mysql.com/downloads/mysql/5.6.html MySQL--.linux_glibc2. ...

  9. Hadoop学习19--推测式执行

    所谓推测式执行,就是计算框架判断,如果有一个task执行的过慢,则会启动备份任务,最终使用原任务+备份任务中执行较快task的结果.产生原因一般是程序bug.负载倾斜. 那么这个较慢,是怎么判断的呢? ...

  10. SELECT INTO FROM 与 INSERT INTO SELECT区别鉴赏

    .INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Tabl ...