cf #360 div2C - NP-Hard Problem(二分图判断+输出)
2016-06-30 23:13
351 查看
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 kintegers — the indices of vertices. Note that because of m ≥ 1,
vertex cover cannot be empty.
Examples
input
output
input
output
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.
题意:给定N个点和M条边,判断是否可以分为二分图。二分图即就是存在两个互不相交的集合,使得每条边的两个顶点分别属于两个集合。
算法:二分图判断,染色法(dfs)。开始对任意一未染色的顶点染色,之后判断其相邻的顶点中,若未染色则将其染上和相邻顶点不同的颜色, 若已经染色且颜色和相邻顶点的颜色相同则说明不是二分图,若颜色不同则继续判断,用深搜即可。
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g(100005);
int visit[100005];
int color[100005];
int n,m;
int flag;
set <int> A,B;
void dfs(int k,int col){
if(visit[k] && color[k] == col) flag = 1;
if(visit[k]) return ;
visit[k] = 1;
if(col) A.insert(k);
else
B.insert(k);
color[k] = !col;
for(int i = 0;i < g[k].size();i ++)
if(g[k][i])
dfs(g[k][i],!col);
}
int main()
{
while(cin>>n>>m){
for(int i = 0;i < 100005;i ++) g[i].clear();
int x,y;
for(int i = 1;i <= m;i ++){
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
memset(visit,0,sizeof(visit));
flag = 0;
for(int i = 1;i <= n;i ++)
if(!visit[i])
dfs(i,0);
if(flag) cout<<-1<<endl;
else{
cout<<A.size()<<endl;
for(set<int>::iterator it = A.begin();it != A.end();it ++){
if(it != A.begin()) cout<<" ";
cout<<*it;
}
cout<<endl;
cout<<B.size()<<endl;
for(set<int>::iterator it = B.begin();it != B.end();it ++){
if(it != B.begin()) cout<<" ";
cout<<*it;
}
cout<<endl;
}
}
return 0;
}
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 kintegers — 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.
题意:给定N个点和M条边,判断是否可以分为二分图。二分图即就是存在两个互不相交的集合,使得每条边的两个顶点分别属于两个集合。
算法:二分图判断,染色法(dfs)。开始对任意一未染色的顶点染色,之后判断其相邻的顶点中,若未染色则将其染上和相邻顶点不同的颜色, 若已经染色且颜色和相邻顶点的颜色相同则说明不是二分图,若颜色不同则继续判断,用深搜即可。
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g(100005);
int visit[100005];
int color[100005];
int n,m;
int flag;
set <int> A,B;
void dfs(int k,int col){
if(visit[k] && color[k] == col) flag = 1;
if(visit[k]) return ;
visit[k] = 1;
if(col) A.insert(k);
else
B.insert(k);
color[k] = !col;
for(int i = 0;i < g[k].size();i ++)
if(g[k][i])
dfs(g[k][i],!col);
}
int main()
{
while(cin>>n>>m){
for(int i = 0;i < 100005;i ++) g[i].clear();
int x,y;
for(int i = 1;i <= m;i ++){
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
memset(visit,0,sizeof(visit));
flag = 0;
for(int i = 1;i <= n;i ++)
if(!visit[i])
dfs(i,0);
if(flag) cout<<-1<<endl;
else{
cout<<A.size()<<endl;
for(set<int>::iterator it = A.begin();it != A.end();it ++){
if(it != A.begin()) cout<<" ";
cout<<*it;
}
cout<<endl;
cout<<B.size()<<endl;
for(set<int>::iterator it = B.begin();it != B.end();it ++){
if(it != B.begin()) cout<<" ";
cout<<*it;
}
cout<<endl;
}
}
return 0;
}
相关文章推荐
- strcpy函数的实现
- Ubuntu更新出现 The system is running in low-graphics mode解决
- MYSQL---查询数据
- html基础一
- Android学习之多线程
- OSGI入职训练之 org.springframework.beans.BeanInstantiationException
- OSChina 周五乱弹 ——程序员怎么要求老板涨工资
- 海量数据找中位数
- mac(OS X)系统下安装node, npm, express
- DROP_SNAPSHOT_RANGE过程不能清理表RM$_SNAPSHOT_DETAILS
- 【转】个人常用iOS第三方库以及XCode插件介绍 -- 不错
- 玩一玩MEAN
- 怎样网站架构
- 关于性能优化的笔记
- 数据结构——c语言描述 第五章 (2) 稀疏矩阵的转置算法
- hiho week104 splay
- Qt编程之信号和槽
- 自定义数据类型 --- 结构体类型全解(swift2.3)
- 一致性算法--Paxos
- APiGateWay产品tyk restful接口定义