您的位置:首页 > 其它

NP-Hard Problemd(二分图判定着色)

2016-07-16 09:23 295 查看
链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121939#problem/C
NP-Hard Problem
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 688CDescriptionRecently, 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).InputThe 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.OutputIf 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.Sample InputInput
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1
题意:
这题大概的意思就是求图 G 的点集 V 的两个不相交子集 A 和 B ,还要求 A 和 B 必须为 G 的点覆盖。其实就是说,图G可不可以二分

解析:
只要保证每条边的点,有个是在A集合,有个是在B集合就可以啦,那么就可以用到顶点染色的方法,有连通的点必定要染上不一样的颜色。
还要注意的是,可能会有几个连通图。

源程序:
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn=1e5+7;
vector<int> a[maxn];
vector<int> d[2];
int vis[maxn],y[maxn],flag,n,m;
void dfs(int x,int f,int t)
{
d[t].push_back(x);
y[x]=t;
vis[x]=1;
for(int i=0;i<a[x].size();i++)
{
if(a[x][i]==f)continue;
if(vis[a[x][i]]&&y[a[x][i]]==t)flag=1;
if(vis[a[x][i]])continue;
dfs(a[x][i],x,1-t);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[x].push_back(y);
a[y].push_back(x);
}
for(int i=1;i<=n;i++)
{
if(flag) break;
if(!vis[i])dfs(i,-1,0);
}
if(flag)printf("-1\n");
else
{
printf("%d\n%d",d[0].size(),d[0][0]);
for(int i=1;i<d[0].size();i++)
printf(" %d",d[0][i]);
printf("\n");
printf("%d\n%d",d[1].size(),d[1][0]);
for(int i=1;i<d[1].size();i++)
printf(" %d",d[1][i]);
printf("\n");
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: