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 #370 (Div. 2) A. Memory and Crow 水题

    A. Memory and Crow 题目连接: http://codeforces.com/contest/712/problem/A Description There are n integer ...

  2. Codeforces Round #311 (Div. 2) A. Ilya and Diplomas 水题

    A. Ilya and Diplomas Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/ ...

  3. Codeforces Round #394 (Div. 2) A. Dasha and Stairs 水题

    A. Dasha and Stairs 题目连接: http://codeforces.com/contest/761/problem/A Description On her way to prog ...

  4. Codeforces Round #404 (Div. 2) B. Anton and Classes 水题

    B. Anton and Classes 题目连接: http://codeforces.com/contest/785/problem/B Description Anton likes to pl ...

  5. Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crosswor 水题

    A. One-dimensional Japanese Crossword 题目连接: http://codeforces.com/contest/721/problem/A Description ...

  6. Codeforces Round #358 (Div. 2) A. Alyona and Numbers 水题

    A. Alyona and Numbers 题目连接: http://www.codeforces.com/contest/682/problem/A Description After finish ...

  7. Codeforces Round #307 (Div. 2) A. GukiZ and Contest 水题

    A. GukiZ and Contest Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...

  8. Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题

    B. Pasha and Tea Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/prob ...

  9. Codeforces Round #350 (Div. 2) B. Game of Robots 水题

    B. Game of Robots 题目连接: http://www.codeforces.com/contest/670/problem/B Description In late autumn e ...

  10. Codeforces Round #379 (Div. 2) A. Anton and Danik 水题

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

最新文章

  1. 请查收你的GitHub 2020 年度报告!
  2. (chap2 简单的Http协议) 请求报文和响应报文
  3. real210移植记录-支持eMMC,增加菜单操作
  4. 题解【bzoj4653 [NOI2016] 区间】
  5. JavaScript判断设备类型加载对应网页并设置两端通用事件
  6. python程序在函数内执行得更快
  7. int main() 与 void main()
  8. 03. 数组中重复的数字
  9. 74ls90设计十进制计数器电路图_利用555与74ls90制作电子秒表
  10. 使用Directory.EnumerateFiles进行批处理
  11. Python 之 文件
  12. HDU 4455 Substrings(线性dp,很有意思)
  13. 数据结构:实验二 队列的基本功能实现
  14. 三菱PLC与上位机通讯协议讲解
  15. 安装SQL Sever2017时出现“Polybase要求安装Oracle JRE 7更新51(64位)或更高版本规则失效”的解决办法
  16. 常用编程语言命令大全
  17. OpenLayers汇总目录
  18. 简单实用!一文掌握效度分析所有知识点!
  19. keil stm32标准库放在哪里_使用Keil MDK以及标准外设库创建STM32工程
  20. hahaha终于注册了一个博客

热门文章

  1. 【学习笔记】圆方树(CF487E Tourists)
  2. CVPR 2018 最具创意论文 TOP10
  3. 拆轮子:requests
  4. RapidJson 的使用
  5. Nginx + Tomcat + HTTPS 配置不需要在 Tomcat 上启用 SSL 支持
  6. Android Studio调试功能使用总结【转】
  7. mysql merg引擎学习
  8. 12款Javascript表格控件(DataGrid)
  9. 前后端分离 与 不分离
  10. linux系统上项目部署