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. 可视化学习Tensorboard

    可视化学习Tensorboard TensorBoard 涉及到的运算,通常是在训练庞大的深度神经网络中出现的复杂而又难以理解的运算.为了更方便 TensorFlow 程序的理解.调试与优化,发布了一 ...

  2. 谈谈 Python 程序的运行原理

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,谈谈 Python 程序的运行原理 这篇文章准确说是『Python 源码剖析』的 ...

  3. VisualSVN:允许修改svn提交日志(pre-revpro-change hook)

    有时候需要对之前版本提交的错误的日志信息进行修改或者进行补充描述: 1.在windows 7( 64位 )下使用TortoiseSVN客户端,选中代码目录,点击右键,选择<显示日志> 在出 ...

  4. win10 QQ远程协助部分界面点不了

    win10 QQ远程协助部分界面点不了. 把对方电脑的电脑管家全部退出,退出了也不行. 是win10的防火墙?安全策略?

  5. spring quartz 任务注入spring service

    SchedulerFactoryBean+AdaptableJobFactory+QuartzJobBean package schedule.quartz5; import org.quartz.S ...

  6. Activity服务类-4 HistoryService服务类

    一共个方法15个方法 用于查询历史工作流信息1.创建查询(7个方法)//创建一个新的编程查询来搜索{@link HistoricProcessInstance}.HistoricProcessInst ...

  7. 返回顶部 fixed oncheck(点击按钮)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 模板引擎-freemarker

    Freemarker 是一款模板引擎,是一种基于模版生成静态文件的通用 工具,它是为java程序员提供的一个开发包. 可通过将Word或者Excel模板另存为xml格式,在其中修改要替换的内容. 基本 ...

  9. Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To sta

    有三种导致这种错误的原因. 第一个: 是因为tomcat的服务没有被关闭所导致的,将服务关闭即可 找到tomcat的安装目录,进入bin文件夹,找到tomcat7w.exe,双击这个文件,点击stop ...

  10. ready 事件 DOM(文档对象模型) 已经加载....

    定义和用法 当 DOM(文档对象模型) 已经加载,并且页面(包括图像)已经完全呈现时,会发生 ready 事件. 由于该事件在文档就绪后发生,因此把所有其他的 jQuery 事件和函数置于该事件中是非 ...