C. NP-Hard Problem

题目连接:

http://www.codeforces.com/contest/688/problem/C

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 k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

Sample Input

4 2

1 2

2 3

Sample Output

1

2

2

1 3

Hint

题意

给你一个无向图,问你能不能变成二分图。

题解

dfs一遍就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
vector<int> E[maxn];
vector<int> ans[2];
int n,m,vis[maxn],flag,type[maxn];
void dfs(int x,int f,int ty){
ans[ty].push_back(x);
type[x]=ty;
vis[x]=1;
for(int i=0;i<E[x].size();i++){
if(E[x][i]==f)continue;
if(vis[E[x][i]]&&type[x]==type[E[x][i]])flag=1;
if(vis[E[x][i]])continue;
dfs(E[x][i],x,1-ty);
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
E[a].push_back(b);
E[b].push_back(a);
}
for(int i=1;i<=n;i++){
if(flag)break;
if(!vis[i])dfs(i,-1,0);
}
if(flag==1){printf("-1\n");return 0;}
cout<<ans[0].size()<<endl;
for(int i=0;i<ans[0].size();i++)
cout<<ans[0][i]<<" ";
cout<<endl;
cout<<ans[1].size()<<endl;
for(int i=0;i<ans[1].size();i++)
cout<<ans[1][i]<<" ";
cout<<endl;
}

Codeforces Round #360 (Div. 2) C. NP-Hard Problem 水题的更多相关文章

  1. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  2. Codeforces Round #384 (Div. 2) A. Vladik and flights 水题

    A. Vladik and flights 题目链接 http://codeforces.com/contest/743/problem/A 题面 Vladik is a competitive pr ...

  3. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  4. Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题

    A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...

  5. Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题

    B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...

  6. Codeforces Round #368 (Div. 2) A. Brain's Photos 水题

    A. Brain's Photos 题目连接: http://www.codeforces.com/contest/707/problem/A Description Small, but very ...

  7. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

  8. Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题

    A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...

  9. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

  10. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

随机推荐

  1. imperva 网管替换

    事情是这样的 某某银行的imperva DAM审计设备出现蜂鸣的响声.经检查电源没有问题,怀疑是硬盘坏了 . 然后我就去底层查看 运行命令 :impctl platform storage raid ...

  2. 2 - django-urls路由系统基本使用

    目录 1 路由系统(urls控制) 1.1 正则字符串参数 1.2 url的分组 1.2.1 无名分组 1.2.2 有名分组 1.3 URLconf 在什么上查找 1.4 include(路由分发) ...

  3.  Meltdown论文翻译【转】

    转自:http://www.wowotech.net/basic_subject/meltdown.html#6596 摘要(Abstract) The security of computer sy ...

  4. iBt(001-004)原文与试译

    Unit 001 Basic building materials include: timber, mud, stone, marble, brick, tile, steel, and cemen ...

  5. overridePendingTransition()使用

    实现两个 Activity 切换时的动画.在Activity中使用有两个参数:进入动画和出去的动画. 注意1.必须在 StartActivity()  或 finish() 之后立即调用.2.而且在 ...

  6. OSGiBundle出现 Could not find bundle: org.eclipse.equinox.console的解决方案

    按照网上教程创建OSGI HelloWorld实例配置run configuration时出现Could not find bundle: org.eclipse.equinox.console 和C ...

  7. 24 The Go image package go图片包:图片包的基本原理

    The Go image package  go图片包:图片包的基本原理 21 September 2011 Introduction The image and image/color packag ...

  8. Sublime Text 3 web 开发常用配置

    前沿 Sublime Text 是一个代码编辑器(Sublime Text 2是收费软件,但可以无限期试用),也是HTML和散文先进的文本编辑器.Sublime Text是由程序员Jon Skinne ...

  9. C# 图片和二进制之间的转换

    1> 图片转二进制  public byte[] GetPictureData(string imagepath){/**/////根据图片文件的路径使用文件流打开,并保存为byte[] Fil ...

  10. CSS常见简写规则整理

    外边距(margin) margin-top margin-right margin-bottom margin-left 简写顺序为顺时针方向(上.右.下.左),如:margin: 1px 2px ...