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. django中的 form 表单操作

     form组件  1. 能做什么事?   1. 能生成HTML代码  input框   2. 可以校验数据   3. 保留输入的数据   4. 有错误的提示   1. 定义   from django ...

  2. 异步请求fetch之初体验

    更好阅读体验可移步我的博客:Blog 导读 传递信息到服务器,从服务器获取信息,是前端发展的重中之重,尤其是现在前后端分离的大前提下,前后端的数据交互是前端的必修科目了.从很久之前到现在,ajax都是 ...

  3. C# List<string>和ArrayList用指定的分隔符分隔成字符串

    原文地址:https://www.cnblogs.com/ahwwmb/p/4166707.html 串联字符串数组的所有元素,其中在每个元素之间使用指定的分隔符 List<string> ...

  4. django-paginator

    py code... from django.core.paginator import Paginator class NewsListView(View): def get(self, reque ...

  5. Python--001

    C 和 Python.Java.C#等 C语言: 代码编译得到 机器码 ,机器码在处理器上直接执行,每一条指令控制CPU工作 其他语言: 代码编译得到 字节码 ,虚拟机执行字节码并转换成机器码再后在处 ...

  6. leetcode476

    public class Solution { public int FindComplement(int num) { //计算数字二进制的反码 var list = new List<int ...

  7. Activity服务类-8 RuntimeService服务类

    一共89个接口1.启动流程实例(20个方法)//使用给定的键在流程定义的最新版本中启动一个新的流程实例.ProcessInstance startProcessInstanceByKey(String ...

  8. 4 并发编程-(进程)-守护进程&互斥锁

    一.守护进程 主进程创建子进程,然后将该进程设置成守护自己的进程,守护进程就好比崇祯皇帝身边的老太监,崇祯皇帝已死老太监就跟着殉葬了. 关于守护进程需要强调两点: 其一:守护进程会在主进程代码执行结束 ...

  9. Others-Goldengate 数据同步

    GoldenGate 是一家创建于1995年的美国公司,开发总部设在旧金山,在北美,欧洲和亚洲(包括新加坡.印度.澳大利亚)设有支持中心. 公司名称 GoldenGate 总部地点 旧金山 成立时间 ...

  10. XmlHttpRequest对象 ajax核心之一

    XMLHttpRequest 对象 XML XSLT XML 解析器 XMLHttpRequest 对象用于在后台与服务器交换数据. 什么是 XMLHttpRequest 对象? XMLHttpReq ...