C - NP-Hard Problem

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Input

Output

Sample Input

Sample Output

Hint

Description

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.

Sample Input

Input
`4 21 22 3`
Output
`12 21 3 `
Input
`3 31 22 31 3`
Output
`-1`

Sample Output

Hint

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.

```#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
const int MAX=1e5+5;
using namespace std;
vector <int>mp[MAX];
int d[MAX];
int vis[MAX];
int n,m;
int dfs(int x,int f)
{
vis[x]=1;
d[x]=f;
int flag;
for(int i=0;i<mp[x].size();i++)
{
if(d[mp[x][i]]==d[x])
return flag=0;
if(d[mp[x][i]]==0)
{
d[mp[x][i]]=-1*f;
if(!dfs(mp[x][i],-1*f))
return flag=0;
}

}
return flag=1;
}
int main()
{
while(cin>>n>>m)
{
int a,b,flag=1;
for(int i=0;i<MAX;i++)
mp[i].clear();
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
mp[a].push_back(b);
mp[b].push_back(a);
}
memset(d,0,sizeof(d));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
if(!dfs(i,1))
{flag=0;break;}
}
}
if(!flag)
cout<<-1<<endl;
else
{
int q=0,p=0;
for(int i=1;i<=n;i++)
{
if(d[i]==1)
q++;
if(d[i]==-1)
p++;
}
cout<<q<<endl;
for(int i=1;i<=n;i++)
if(d[i]==1)
printf("%d ",i);
cout<<endl;
cout<<p<<endl;
for(int i=1;i<=n;i++)
if(d[i]==-1)
printf("%d ",i);
cout<<endl;
}

}
}代码改良：```
```#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
const int MAX=1e5+5;
using namespace std;
vector<int>mp[MAX],ans1,ans2;
int d[MAX],n,m;
int dfs(int x)
{
if(d[x]==0)
d[x]=1;
if(d[x]==1)
ans1.push_back(x);
if(d[x]==-1)
ans2.push_back(x);
for(int i=0;i<mp[x].size();i++)
{
if(d[x]==d[mp[x][i]])
return 0;
if(d[mp[x][i]]==0)
{d[mp[x][i]]=-1*d[x];
if(!dfs(mp[x][i]))
return 0;}
}
return 1;
}
int main()
{ int a,b;
while(cin>>n>>m)
{   for(int i=1;i<=n;i++)
mp[i].clear();
ans1.clear();
ans2.clear();
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
mp[a].push_back(b);
mp[b].push_back(a);
}
int flag=1;
memset(d,0,sizeof(d));
for(int i=1;i<=n;i++)
{
if(d[i]==0)
{
if(!dfs(i))
{
flag=0;
break;
}
}

}
if(!flag)
cout<<-1<<endl;
else
{
cout<<ans1.size()<<endl;
for(int i=0;i<ans1.size();i++)
printf("%d ",ans1[i]);
cout<<endl;
cout<<ans2.size()<<endl;
for(int i=0;i<ans2.size();i++)
printf("%d ",ans2[i]);
cout<<endl;

}

}
}
```
` `

## C - NP-Hard Problem（二分图判定-染色法）的更多相关文章

1. CF687A. NP-Hard Problem[二分图判定]

A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

2. Wrestling Match---hdu5971(2016CCPC大连 染色法判断是否是二分图)

3. 染色法判断是否是二分图 hdu2444

用染色法判断二分图是这样进行的,随便选择一个点, 1.把它染成黑色,然后将它相邻的点染成白色,然后入队列 2.出队列,与这个点相邻的点染成相反的颜色 根据二分图的特性,相同集合内的点颜色是相同的,即 ...

4. HDU 2444：The Accomodation of Students（二分图判定+匹配）

http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:给出边,判断这个是否是一个二分图,并求最大匹配. 思路:先染色法求出是否是一个二分图,然后再匈牙利求 ...

5. COJ 0578 4019二分图判定

4019二分图判定 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给定一个具有n个顶点(顶点编号为0,1,… ...

6. HDU 2444 The Accomodation of Students（二分图判定+最大匹配）

这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...

7. 二分图点染色 BestCoder 1st Anniversary(\$) 1004 Bipartite Graph

题目传送门 /* 二分图点染色:这题就是将点分成两个集合就可以了,点染色用dfs做, 剩下的点放到点少的集合里去 官方解答:首先二分图可以分成两类点X和Y, 完全二分图的边数就是|X|*|Y|.我们的 ...

8. hdoj 3478 Catch(二分图判定+并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...

9. UVA 11080 - Place the Guards(二分图判定)

UVA 11080 - Place the Guards 题目链接 题意:一些城市.之间有道路相连,如今要安放警卫,警卫能看守到当前点周围的边,一条边仅仅能有一个警卫看守,问是否有方案,假设有最少放几 ...

10. poj2942 Knights of the Round Table，无向图点双联通，二分图判定

点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...

## 随机推荐

1. 【整理】Linux下中文检索引擎coreseek4安装，以及PHP使用sphinx的三种方式(sphinxapi，sphinx的php扩展，SphinxSe作为mysql存储引擎)

一,软件准备 coreseek4.1 (包含coreseek测试版和mmseg最新版本,以及测试数据包[内置中文分词与搜索.单字切分.mysql数据源.python数据源.RT实时索引等测 ...

2. Shell 脚本实现随机抽取班级学生

#/bin/bash function rand(){ min=\$ max=\$((\$-\$min+)) num=\$(date +%s%N) echo \$((\$num%\$max+\$min)) } rnd= ...

3. SQL Server自动化运维系列——关于数据收集（多服务器数据收集和性能监控）

需求描述 在生产环境中,很多情况下需要采集数据,用以定位问题或者形成基线. 关于SQL Server中的数据采集有着很多种的解决思路,可以采用Trace.Profile.SQLdiag.扩展事件等诸多 ...

4. c++ 注册类到 lua

test.h: #ifndef __TEST_H__ #define __TEST_H__ class CTest { public: CTest(); ~CTest(); int getA(); v ...

5. Redis常用命令入门5：有序集合类型

有序集合类型 上节我们一起学习了集合类型,感受到了redis的强大.现在我们接着学Redis的最后一个类型——有序集合类型. 有序集合类型,大家从名字上应该就可以知道,实际上就是在集合类型上加了个有序 ...

6. JavaScript Module Pattern: In-Depth

2010-03-12 JavaScript Module Pattern: In-Depth The module pattern is a common JavaScript coding patt ...

7. Windows 2008 R2 强制删除Cluster

在正常删除Cluster 节点之后,再添加节点时,报“节点已经加入群集”,无法加入,注册表信息删除后可正常移除Cluster服务,如下: HKEY_LOCAL_MACHINE\SYSTEM\Curre ...

8. Java并发编程核心方法与框架-Semaphore的使用

Semaphore中文含义是信号.信号系统,这个类的主要作用就是限制线程并发数量.如果不限制线程并发数量,CPU资源很快就会被耗尽,每个线程执行的任务会相当缓慢,因为CPU要把时间片分配给不同的线程对 ...

9. JAVA通过HTTP访问：Post+Get方式（转）

public class TestGetPost { /** * 向指定URL发送GET方法的请求 * @param url 发送请求的URL * @param param 请求参数,请求参数应该是n ...

10. windows下安装Appserv等php套件之后无法进入数据库管理的问题