CodeForces687A NP-Hard Problem(二分图染色)

CodeForces687A NP-Hard Problem(二分图染色)

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 u i and v i (1  ≤  u i,  v i  ≤  n), denoting an undirected edge between u i and v i. 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.
Input
4 2
1 2
2 3
Output
1
2
2
1 3
Input
3 3
1 2
2 3
1 3
Output
-1

题意

给出一张图,将点分别染成两种颜色,要求有边相连的点的颜色不同(我以颜色1,颜色2来区分),并且将颜色相同的点存入同一个集合。如果这种方案存在,则输出两种颜色的顶点个数和每个顶点的编号,不存在就输出-1。二分图染色裸题,bfs染色即可。千万要每个点都进行一次bfs,因为有些点可能是孤立的,若只进行一次bfs则好多点都没有被上到色。这里wa了好久。。。

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<functional>
#include<map>
//#include<unordered_map>
#define lowbit(x) ((x)&-(x));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10,NN=2e3+10,INF=0x3f3f3f3f,LEN=20;
const ll MOD=1e9+7;
const ull seed=31;
struct Edge{
	int next,to;
}edge[N];
vector<int>vec1,vec2;
int num_edge,n,m;
int head[N],match[N],vis[N];
bool flag;
bool reverse_boy[N],mark[N];
void add_edge(int from,int to){
	edge[num_edge].next=head[from];
	edge[num_edge].to=to;
	head[from]=num_edge++;
}
bool bfs(int s){
	queue<int>q;
	q.push(s);
	vis[s]=1;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		mark[u]=false;
		for(int i=head[u];i!=-1;i=edge[i].next){
			int v=edge[i].to;
			if(!vis[v]){
				if(vis[u]==1) vis[v]=2;
				else vis[v]=1;
				q.push(v);
			}
			else if(vis[u]==vis[v]) return false;
		}
	}
	return true;
}
void init(){
	vec2.clear();
	vec1.clear();
	num_edge=0;
	flag=true;
	memset(head,-1,sizeof head);
	memset(vis,0,sizeof vis);
}
int main(){
	while(~scanf("%d%d",&n,&m)){
		init();
		memset(mark,false,sizeof mark);
		for(int i=1;i<=m;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			add_edge(u,v);
			add_edge(v,u);
			mark[u]=mark[v]=true;
		}
		bool flag=true;
		for(int i=1;i<=n;++i){
			if(mark[i]&&!bfs(i)){
				puts("-1");
				flag=false;
				break;
			}
		}
		if(!flag) continue;
		for(int i=1;i<=n;i++){
			if(vis[i]==2) vec2.push_back(i);
			else if(vis[i]==1) vec1.push_back(i);
		}
		int temp2=vec2.size(),temp1=vec1.size();
		sort(vec2.begin(),vec2.end());
		sort(vec1.begin(),vec1.end());
		printf("%d\n",temp2);
		for(int i=0;i<temp2;i++){
			printf("%d",vec2[i]);
			printf(i==temp2-1?"\n":" ");
		}
		printf("%d\n",temp1);
		for(int i=0;i<temp1;i++){
			printf("%d",vec1[i]);
			printf(i==temp1-1?"\n":" ");
		}
	}
}

猜你喜欢

转载自blog.csdn.net/Hc_Soap/article/details/107772461