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 k integers — 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.

题目链接:http://codeforces.com/contest/688/problem/C


题意:一个由n个点m条边组成的图,把他分成二部图。

思路:DBF或者BFS暴力搜索,当前这个点在一组,与他相邻的点必须在另一组。不符合就输出-1,符合输出2组点。

代码:

 #include<bits/stdc++.h>
using namespace std;
struct node
{
int from;
int to;
int d;
int next;
} edge[];
int head[];
void add(int i,int u,int v,int d)
{
edge[i].from=u;
edge[i].to=v;
edge[i].d=d;
edge[i].next=head[u];
head[u]=i;
}
int sign[];
int ans,sum1,sum2;
queue<int>Q;
void BFS(int u)
{
while(!Q.empty()) Q.pop();
Q.push(u);
while(!Q.empty())
{
u=Q.front();
Q.pop();
int i=head[u];
while(i!=)
{
if(sign[edge[i].from]==&&sign[edge[i].to]==)
{
sum2++;
sign[edge[i].to]=;
Q.push(edge[i].to);
}
else if(sign[edge[i].from]==&&sign[edge[i].to]==)
{
sum1++;
sign[edge[i].to]=;
Q.push(edge[i].to);
}
else if(sign[edge[i].from]==&&sign[edge[i].to]==) ans=;
else if(sign[edge[i].from]==&&sign[edge[i].to]==) ans=;
if(ans==) break;
i=edge[i].next;
}
if(ans==) break;
}
}
int main()
{
int i,j,n,m;
scanf("%d%d",&n,&m);
memset(head,,sizeof(head));
for(i=,j=; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(j++,u,v,);
add(j++,v,u,);
}
ans=;
sum1=sum2=;
memset(sign,,sizeof(sign));
for(i=; i<=n; i++)
{
if(sign[i]!=) continue;
sign[i]=;
sum1++;
BFS(i);
}
if(ans==)
{
cout<<sum1<<endl;
for(i=; i<=n; i++)
if(sign[i]==) cout<<i<<" ";
cout<<endl<<sum2<<endl;
for(i=; i<=n; i++)
if(sign[i]==) cout<<i<<" ";
cout<<endl;
}
else cout<<"-1"<<endl;
return ;
}

BFS暴搜

 #include<bits/stdc++.h>
using namespace std;
vector<int>v[];
queue<int>Q;
int sign[];
int ans,sum1,sum2;
void BFS(int u)
{
while(!Q.empty()) Q.pop();
Q.push(u);
while(!Q.empty())
{
u=Q.front();
Q.pop();
for(int i=; i<v[u].size(); i++)
{
if(sign[v[u][i]]==sign[u])
{
ans=;
return;
}
else if(sign[v[u][i]]==)
{
if(sign[u]==)
{
sum2++;
sign[v[u][i]]=;
}
else
{
sum1++;
sign[v[u][i]]=;
}
Q.push(v[u][i]);
}
}
}
}
int main()
{
int i,n,m,a,b;
scanf("%d%d",&n,&m);
memset(sign,,sizeof*(sign));
for(i=; i<m; i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
ans=;
for(i=; i<=n; i++)
{
if(sign[i]!=) continue;
sign[i]=;
sum1++;
BFS(i);
if(ans==) break;
}
if(ans==)
{
cout<<sum1<<endl;
for(i=; i<=n; i++)
if(sign[i]==) cout<<i<<" ";
cout<<endl<<sum2<<endl;
for(i=; i<=n; i++)
if(sign[i]==) cout<<i<<" ";
cout<<endl;
}
else cout<<"-1"<<endl;
}

BFS暴搜(vector)

Codeforces C. NP-Hard Problem 搜索的更多相关文章

  1. Codeforces 586D. Phillip and Trains 搜索

    D. Phillip and Trains time limit per test: 1 second memory limit per test :256 megabytes input: stan ...

  2. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  3. [codeforces 528]B. Clique Problem

    [codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...

  4. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  5. Codeforces 442B Andrey and Problem(贪婪)

    题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...

  6. CodeForces 867B Save the problem

    B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...

  7. Codeforces 776D The Door Problem

    题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...

  8. codeforces 803G Periodic RMQ Problem

    codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...

  9. [Codeforces 986E] Prince's Problem

    [题目链接] https://codeforces.com/contest/986/problem/E [算法] X到Y的路径积 , 可以转化为X到根的路径积乘Y到根的路径积 , 除以LCA到根的路径 ...

随机推荐

  1. 代码生成器 CodeSmith 的使用(六)

    在上一篇的版本中,我们生成了数据库所有表中的字段,如果要使数据库中的单个表 生成 PetaPoco 构架下的 ORM 映射,使那怎么办.这是这篇博客的主要内容. 首先来看完整的 Camel 规则模板: ...

  2. Android SDK的安装与环境配置

    一.Android SDK工具下载.安装 Android SDK工具下载:http://www.androiddevtools.cn/ SDK下载页面如下,由于电脑Windows系统所以下载的Wind ...

  3. tornado-模板继承extend,函数和类的导入

    大 import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # ...

  4. Zookeeper 介绍翻译

    源网址链接 https://zookeeper.apache.org/ Apache Zookeeper 开放源码的服务器,提供高可靠的分布式协调服务. Zookeeper是一个维护配置信息,命名服务 ...

  5. python实现一个栏目的分页抓取列表页抓取

    python实现一个栏目的分页抓取列表页抓取 #!/usr/bin/env python # coding=utf-8 import requests from bs4 import Beautifu ...

  6. centos7防火墙使用方法

    参考网站:https://blog.csdn.net/achang21/article/details/52538049 添加开放指定端口: [root@yao bin]# firewall-cmd ...

  7. leetcode242

    public class Solution { public bool IsAnagram(string s, string t) { Dictionary<char, int> dic ...

  8. angular ng-bind-html

    使用ng-bind-html 可以在页面上展示html内容,让html内容加进去后不是一代码形式出现,而是以页面形式展现 需要先引入angular-sanitize.min.js,这个可以在githu ...

  9. shiro 框架

    惊天给大家总结一点shiro框架的小知识 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应 ...

  10. eclipse包层级显示和工作空间显示

    本文两件事:设置包层级显示.设置工程的工作空间显示 一.各package包分层显示 平铺显示,实在不方便开发!也不方便查看工程包的层级结构,如下: 更换成层级显示: 二.工作空间显示 包用来区分类,工 ...