C - NP-Hard Problem

Description

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 kintegers — 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
题意:给出点的个数和连接这些点的边(给你个无向图),判断是否能构成二分图,如果能就输出左右集合,不能输出-1.
分析:
二分图的定义:有两个顶点集且每条边的两个点分别位于两个集合,每个集合中不能含有一条完整的边。
二分图的判断方法:先对任意一条没有染色的点进行染色,再判断与其相邻的点是否染色,如果没有染色就对其染上与其相邻的 点不同的颜色,如果有染色且颜色与其相邻点的颜色相同就不是二分图,若颜色不同就继续判断(用bfs)。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = + ;
vector<int>g[maxn],v[];
bool ok = true;
int color[maxn];
void dfs(int k,int c){
if(!ok)return;
if (color[k] != -){
if (color[k] != c) ok=false;
return;
}
int len = g[k].size();
if(len!=)
{
color[k] = c;
v[c].push_back(k);
}
for (int i = ; i < len; ++i) dfs(g[k][i],c^);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for (int i = ; i < m; ++i){
int u,w;
scanf("%d%d",&u,&w);
g[u].push_back(w);
g[w].push_back(u);
}
memset(color,-,sizeof color);
for (int i = ; i <= n; ++i){
if (color[i] == -)dfs(i,);
}
if (!ok)printf("-1\n");
else {
for (int i = ; i < ; ++i)
{
int len = v[i].size();
printf("%d\n",len);
for (int j = ; j < len; ++j){
if (j)printf(" ");
printf("%d",v[i][j]);
}
printf("\n");
}
}
return ;
}
 

codeforces 360 C的更多相关文章

  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 360 E - The Values You Can Make

    E - The Values You Can Make Description Pari wants to buy an expensive chocolate from Arya. She has  ...

  3. codeforces 360 D - Remainders Game

    D - Remainders Game Description Today Pari and Arya are playing a game called Remainders. Pari choos ...

  4. 套题 codeforces 360

    A题:Opponents 直接模拟 #include <bits/stdc++.h> using namespace std; ]; int main() { int n,k; while ...

  5. codeforces 360 C - NP-Hard Problem

    原题: Description Recently, Pari and Arya did some research about NP-Hard problems and they found the  ...

  6. codeforces 360 B

    B - Levko and Array 题目大意:给你你个长度为n的数列a,你最多改变k个值,max{ abs ( a[ i + 1] - a[ i ] ) } 的最小值为多少. 思路:这个题很难想到 ...

  7. Codeforces Round #360 div2

    Problem_A(CodeForces 688A): 题意: 有d天, n个人.如果这n个人同时出现, 那么你就赢不了他们所有的人, 除此之外, 你可以赢他们所有到场的人. 到场人数为0也算赢. 现 ...

  8. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集

    D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...

  9. Codeforces Round #360 (Div. 2) D. Remainders Game 数学

    D. Remainders Game 题目连接: http://www.codeforces.com/contest/688/problem/D Description Today Pari and ...

随机推荐

  1. Mac 安装配置rz、sz

    在Iterm2中修改配置: 安装lrzsz brew install lrzsz 下载iterm2-zmodem cd /usr/local/bin sudo wget https://raw.git ...

  2. 用任务计划管理计划任务对付任务计划-禁止WPS提示升级

    作为一名至今还在坚守着64位XP的XP用到死星人,因为准备升级电脑,所以准备移民外星,开始使用Windows7.其实我新电脑买来一年了,为了坚守XP,扔在一边没有装(华硕Z9PE-D8 WS主板,双E ...

  3. 开发维护大型 Java 项目的建议

    假设你是正在开发和维护一个包含2000个类并使用了很多框架的Java开发者.你要如何理解这些代码?在一个典型的Java企业项目小组中,大部 分能够帮你的高级工程师看起来都很忙.文档也很少.你需要尽快交 ...

  4. mina中游戏客户端服务端数据交互流程

    ====================================================================================CLIENT    encode ...

  5. 第一天--html

    <!Doctype html><html>    <head>        <meta charset="UTF-8">      ...

  6. Session在类库中的使用

    转自:http://www.cnblogs.com/JiangXiaoTian/articles/3490904.html 网站开发中,为了保存用户的信息,有时候需要使用session.如果我们在as ...

  7. java项目上线过程

    关于如何将Javaweb上线,部署到公网,让全世界的人都可以访问的问题.小编将作出系列化,完整的流程介绍. 1.在myeclipse中开发好项目,打包成war格式,不会的同学参考以下 http://z ...

  8. WPF 如何绘制不规则按钮,并且有效点击范围也是不规则的

    最近在做一个东西,如地图,点击地图上的某一区域,这一区域需要填充成其他颜色.区域是不规则的,而且点击该区域的任一点,都能够变色.普通的按钮只是简单的加载一幅图肯定是不行的.查了很多资料,终于把它搞定了 ...

  9. hadoop2.6.4 搭建伪分布式

    安装jdk1.7 http://www.cnblogs.com/zhangXingSheng/p/6228432.html  ------------------------------------- ...

  10. zookeeper3.3.6 伪分布式安装

    下载地址(http://zookeeper.apache.org/releases.html#download)   一:下载zookeeper的安装包,解压,进入到zk的目录文件,进入conf目录 ...