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. 如何基于Spring Boot搭建一个完整的项目

    前言 使用Spring Boot做后台项目开发也快半年了,由于之前有过基于Spring开发的项目经验,相比之下觉得Spring Boot就是天堂,开箱即用来形容是绝不为过的.在没有接触Spring B ...

  2. ADB常用命令(二)

    参考  http://adbshell.com/commands 常用命令 查看adb 版本 adb version 打印所有附加模拟器/设备的列表 adb devices 设备序列号 adb get ...

  3. mysql中utf8编码的utf8_bin,utf8_general_cs,utf8_bin的区别

    utf8_general_ci 不区分大小写,这个你在注册用户名和邮箱的时候就要使用. utf8_general_cs 区分大小写,如果用户名和邮箱用这个 就会照成不良后果 utf8_bin: com ...

  4. python基础--类的经典类vs新式类

    经典类VS新式类区别 1)写法新式类class Person(object):#new style 经典类class Persion: #classical style 2)调用父类 新式写法用sup ...

  5. 基于vue配置axios

    转载地址:https://juejin.im/post/5a02a898f265da43052e0c85 1.背景 在项目开发中ajax请求是必不可缺少 一部分ajax请求不需要loading或则请求 ...

  6. ThinkPHP文件目录说明

    1.ThinkPHP文件包下目录结构说明 2.ThinkPHP文件目录下文件说明 3.Conf目录下 4.Library目录

  7. AdvStringGrid 复选框、goRowSelect

    var I: Integer; begin do begin AdvStringGrid1.AddCheckBox(, I, True, True); AdvStringGrid1.Cells[,I] ...

  8. Python学习笔记:出生日期转化为年龄

    在数据挖掘项目中,有时候个体的出生日期包含信息量过大,不适合作为一个有效数据进入模型算法训练,因此有必要把出生日期转化为年龄age,age是一个很好的特征工程指示变量. import pandas a ...

  9. django model常用字段类型

    摘自 http://www.cnblogs.com/wt869054461/p/4014271.html V=models.AutoField(**options) #int:在Django代码内是自 ...

  10. box-sizing 盒子模型不改变大小

    参考 box-sizing 相关说明 在需要设置元素的内边距或者外边距而不想改变整体大小时,可以使用该属性,记录之. .simple { width: 500px; margin: 20px auto ...