博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF687A. NP-Hard Problem[二分图判定]
阅读量:7078 次
发布时间:2019-06-28

本文共 3486 字,大约阅读时间需要 11 分钟。

A. 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
Note

In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).

In the second sample, there is no way to satisfy both Pari and Arya.


 

裸的二分图判定

不一定连通太坑人

////  main.cpp//  cf687a////  Created by Candy on 9/20/16.//  Copyright © 2016 Candy. All rights reserved.//#include
#include
#include
#include
using namespace std;const int N=1e5+5;int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){
if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f;}int n,m,cnt1=0,cnt2=0;struct edge{ int v,ne;}e[N<<1];int cnt=0,h[N];inline void ins(int u,int v){ cnt++; e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt; cnt++; e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;}int col[N];bool dfs(int u){ for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(col[v]==col[u]) return false; if(!col[v]){ col[v]=3-col[u]; if(!dfs(v)) return false; } } return true;}int main(int argc, const char * argv[]) { n=read();m=read(); for(int i=1;i<=m;i++) ins(read(),read()); for(int i=1;i<=n;i++) if(col[i]==0){ if(h[i]==0) continue; col[i]=1; if(!dfs(i)){ printf("-1"); return 0; } } for(int i=1;i<=n;i++) {
if(col[i]==1) cnt1++;if(col[i]==2) cnt2++;} printf("%d\n",cnt1); for(int i=1;i<=n;i++) if(col[i]==1) printf("%d ",i); printf("\n%d\n",cnt2); for(int i=1;i<=n;i++) if(col[i]==2) printf("%d ",i); return 0;}

 

转载地址:http://avpml.baihongyu.com/

你可能感兴趣的文章
使用Python+OpenCV进行图像模板匹配(Match Template)
查看>>
Assembly Loop
查看>>
Windows注册密码能保护你的数据安全吗?
查看>>
微服务和容器对企业带来什么样的影响?
查看>>
新工具填补Docker管理空白
查看>>
行为驱动开发使用体验
查看>>
智慧城市:中国特色新型城镇化的发展方向
查看>>
新恶意软件使用Tor在Mac OS X系统打开“后门”
查看>>
JIRA管理思路
查看>>
2017年我国城市大数据市场规模将达189亿元
查看>>
P2P追债也用上大数据
查看>>
英特尔眼中的未来
查看>>
对于linux操作系统的认识和了解
查看>>
使用LoadRunner监测MySQL数据库的性能
查看>>
泰国TCCtech公司投资850万美元扩大数据中心规模
查看>>
Amlogic连续三年居中国OTT芯片市占率第一
查看>>
如何用Java将excel数据导入数据库
查看>>
美国全球光伏调查对华影响有限
查看>>
机器选角、票房预测,大数据如何改变中国电影?
查看>>
Facebook重组欧洲管理层:意法等地区销售主管将离职
查看>>