Codeforces Round #360 (Div. 1)A:二分图判断

A. 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 0001 ≤ 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

h ttp://codeforces.com/contest/687/problem/A
问题概述: 输入一个图,判断它是否能构成二分图,若可以,输出其中一次构造,否则输出-1

什么是二分图:
定义:顶点集V可分割为两个互不相交的子集,并且图中每条边依附的两个顶点都处在不同的子集,两个子集内的顶点不相邻
充要条件:图G至少有两个顶点,且所有回路的长度均为偶数

如何判断图G是否为二分图:
DFS二分图染色法(步骤):
①:定义一个标记数组lor[n],lor[k]=0表示k点还没有遍历到,lor[k]=1表示k点在左集合中,lor[k]=-1表示k点在右集合中,初始化所有值为0
②:遍历n个点,只要这个点的标记值lor[i]=0就对它进行搜索,并初始让它进入左集合(lor[k]=1)
③:搜索时对于每一个点,它的相邻点必须在另一个集合中,即它们的lor标记值不能相等!否则意味着图G不可能是二分图
④:假设搜到了x点,然后p点和x点相邻,那么如果lor[x]=1且lor[p]=0,就让lor[p] = -1并继续搜索p点,如果lor[x]=1且lor[p]=-1那么就说明p点已经搜过,没必要继续搜,但若lor[p]=lor[x]=1,见③,对于lor[x]=-1方法类似



#include<stdio.h>
#include<vector>
using namespace std;
vector<int> v[100005], a, b;
int ok = 1, lor[100005] = {0};
void Sech(int x);
int main(void)
{
	int i, n, m, c, d;
	scanf("%d%d", &n, &m);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d", &c, &d);
		v[c].push_back(d);
		v[d].push_back(c);
	}
	for(i=1;i<=n;i++)
	{
		if(lor[i]==0)
		{
			lor[i] = 1;
			a.push_back(i);
			Sech(i);
		}
	}
	if(ok==0)
		printf("-1\n");
	else
	{
		printf("%d\n", a.size());
		for(i=0;i<a.size();i++)
		{
			if(i==0)
				printf("%d", a[i]);
			else
				printf(" %d", a[i]);
		}
		printf("\n%d\n", b.size());
		for(i=0;i<b.size();i++)
		{
			if(i==0)
				printf("%d", b[i]);
			else
				printf(" %d", b[i]);
		}
		printf("\n");
	}
	return 0;
}

void Sech(int x)
{
	int i, t;
	if(ok==0)
		return;
	for(i=0;i<v[x].size();i++)
	{
		t = v[x][i];
		if(lor[t]==lor[x])
			ok = 0;
		else
		{
			if(lor[x]==1 && lor[t]==0)
			{
				b.push_back(t), lor[t] = -1;
				Sech(t);
			}
			else if(lor[x]==-1 && lor[t]==0)
			{
				a.push_back(t), lor[t] = 1;
				Sech(t);
			}
		}
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值