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. dos批处理文件中的变量小结

    很多情况下我们需要用到一些批处理文件中的变量,方便我们执行一些操作,这里简单整理下,方便需要的朋友   批处理中的变量,我把他分为两类,分别为"系统变量"和"自定义变量& ...

  2. python之列表操作

    1.列表的增操作(四种) append(object):append object to end,directly used on list insert(index,object):insert o ...

  3. 解决Visual Studio “无法导入以下密钥文件”的错误

    错误1无法导入以下密钥文件: Common.pfx.该密钥文件可能受密码保护.若要更正此问题,请尝试再次导入证书,或手动将证书安装到具有以下密钥容器名称的强名称 CSP: VS_KEY_ 1110Co ...

  4. docker使用笔记1

    rhel6安装 yum -y install docker-io ################################################ 进入容器命令 docker exec ...

  5. 2. java获取下周日-下周六的时间

    String[] arrDate = new String[7]; String[] arrWeek = new String[7]; int mondayPlus = 0; Calendar cd ...

  6. 46. linux下解压.tar.gz文件

    tar -zxvf jdk-7u55-linux-i586.tar.gz 步骤二:解压jdk-7u55-linux-i586.tar.gz ,执行以下命令: #mkdir /usr/local/jav ...

  7. session失效刷新后登录页面嵌入在iframe中的前台解决办法

    在前台登录页面中加入JS代码,判断登录页面是否在iframe中,在iframe中就跳转出去 例: //判断是否在iframe中,在里面就跳出去 if (top.location.href != loc ...

  8. Using Celery with Django

    参考1: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#using-celery-with-d ...

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

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

  10. Gearman简介

    gearman,从名字上看叫做“齿轮工”,就是通过齿轮把不同的组件组合在一起.通常,多语言多系统之间的集成是项目开发中一个比较头疼的问题.一般会采用RPC风格或者是REST风格的WebService. ...