题目链接:

C. NP-Hard Problem

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
input
4 2
1 2
2 3
output
1
2
2
1 3
input
3 3
1 2
2 3
1 3
output
-1

题意:

给一个森林,问能否找到这样的两个集合,使每条边的至少一个点在这样的集合里;有的话输出这两个集合;

思路:

每一条边的两个点分别是这两个集合里的;如果出现奇数长度的环就怎么也无法满足了;

AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+;
const int maxn=;
const double eps=1e-; int n,m;
vector<int>ve[N];
int dis[N],vis[N],ansa[N],ansb[N];
queue<int>qu; int bfs(int x)
{
vis[x]=;
while(!qu.empty())qu.pop();
qu.push(x);
while(!qu.empty())
{
int fr=qu.front();
qu.pop();
int len=ve[fr].size();
for(int i=;i<len;i++)
{
int y=ve[fr][i];
if(!vis[y])
{
dis[y]=dis[fr]+;
qu.push(y);
vis[y]=;
}
else
{
if((dis[y]+dis[fr])%==)return ;
}
}
}
return ;
}
int check()
{
for(int i=;i<=n;i++)
{
if(!vis[i])
{
dis[i]=;
if( bfs(i)==)return ;
}
}
return ;
}
int main()
{ read(n);read(m);
int u,v;
for(int i=;i<m;i++)
{
read(u);read(v);
ve[u].push_back(v);
ve[v].push_back(u);
}
if(!check())cout<<"-1"<<"\n";
else
{
int A=,B=;
for(int i=;i<=n;i++)
{
if(dis[i]&)ansa[A++]=i;
else ansb[B++]=i;
}
cout<<A<<"\n";
for(int i=;i<A-;i++)printf("%d ",ansa[i]);
printf("%d\n",ansa[A-]);
cout<<B<<"\n";
for(int i=;i<B-;i++)printf("%d ",ansb[i]);
printf("%d\n",ansb[B-]); } return ;
}

codeforces 688C C. NP-Hard Problem(bfs判断奇数长度环)的更多相关文章

  1. hdu-5652 India and China Origins(二分+bfs判断连通)

    题目链接: India and China Origins Time Limit: 2000/2000 MS (Java/Others)     Memory Limit: 65536/65536 K ...

  2. 实验12:Problem D: 判断两个圆之间的关系

    Home Web Board ProblemSet Standing Status Statistics   Problem D: 判断两个圆之间的关系 Problem D: 判断两个圆之间的关系 T ...

  3. HDU 3342 Legal or Not(判断是否存在环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...

  4. POJ-3259 Wormholes---SPFA判断有无负环

    题目链接: https://vjudge.net/problem/POJ-3259 题目大意: 农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的 ...

  5. Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是 ...

  6. JS判断字符串长度的5个方法

    这篇文章主要介绍了JS判断字符串长度的5个方法,并且区分中文和英文,需要的朋友可以参考下 目的:计算字符串长度(英文占1个字符,中文汉字占2个字符)   方法一:    代码如下: String.pr ...

  7. iOStextFiled判断输入长度

    个人在开发当中发现在用textField的代理方法 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(N ...

  8. php--------使用 isset()判断字符串长度速度比strlen()更快

    isset()速度为什么比strlen()更快呢? strlen()函数函数执行起来相当快,因为它不做任何计算,只返回在zval 结构(C的内置数据结构,用于存储PHP变量)中存储的已知字符串长度.但 ...

  9. C 语言实例 - 判断奇数/偶数

    C 语言实例 - 判断奇数/偶数 C 语言实例 C 语言实例 以下实例判断用户输入的整数是奇数还是偶数. 实例 #include <stdio.h> int main() { int nu ...

随机推荐

  1. mysql 递归查询父节点 和子节点

    查父集合 --drop FUNCTION `getParentList` )) ) BEGIN ) default ''; ) default rootId; WHILE rootId is not ...

  2. SSH常见问题集锦

    /WEB-INF/web.xml Web应用程序配置文件,描述了 servlet 和其他的应用组件配置及命名规则. /WEB-INF/classes/包含了站点所有用的 class 文件,包括 ser ...

  3. 以太坊和IPFS如何存储数据

    如何将JSON文件存储在IPFS上,并使用Oraclize访问智能合约中的数据呢? 以太坊是一个成熟的区块链,使开发人员能够创建智能合约,在区块链上执行的程序可以由交易触发.人们经常将区块链称为数据库 ...

  4. [HNOI2009]梦幻布丁(链表+启发式合并)

    洛谷传送门 开始一个O(n^2)思路,每次每句要改变颜色的点,改变完颜色后重新计算颜色的段数,显然拉闸. 然后呢..然后就不会了. 看了别人博客,才知道有个叫做启发式合并的东西,就是把小的合并到大的上 ...

  5. hdu 1564水题Play a game

    #include<stdio.h> int main() {  int n;  while(scanf("%d",&n),n) {   n=n*n-1;   i ...

  6. ES6__Iterator和for...of循环

    /** * Iterator和for...of循环 */ // --------------------------------------------------------------- /** ...

  7. linux下reboot和shutdown关机命令详解

    我 们在操作Linux v/服务器的时候肯定会有需要重启系统,或者关闭系统等操作.有些用户是直接到VPS主机商家面板上操作的,这样一来比较麻烦,二来有些面板还不易于使用 容易导致面板卡死.所以最好的方 ...

  8. django学习之- Ajax

    提示:jquery要使用1版本,因为高版本已不兼容低版本的游览器.参考url:http://www.cnblogs.com/wupeiqi/articles/5703697.html原生ajax:Aj ...

  9. python学习之-- random模块

    random模块random.random():随机打印一个小数random.randint(1,10):随机打印1-10之间的任意数字(包括1和10)random.randrange(1,10):随 ...

  10. c标准库函数 strcat

    函数原型:extern char *strcat(char *dest,char *src) 参数说明:dest为一个目的字符串的指针,即被连接的字符串(在前),src为一个源字符串的指针(在后).所 ...