洛谷1330 染色

https://www.luogu.org/problemnew/show/P1330 

这就是刷专题不好的地方,挺好的一道题目,我还没看题就知道是染色问题。。。。

注意:

  1. 图可能不是连通的
  2. 图有环
//很坑的一道题目,没有说序号是从小到大的 
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
vector<int> v[maxn];
int color[maxn];
int num[maxn]; 
int cnt1,cnt2;

bool dfs(int n,int c)
{
	if(color[n]){
		if(c==color[n]) return true;
		else return false;
	}
	if(1==c) cnt1++;
	else cnt2++;
	color[n]=c;
	int i,j;
	for(i=0;i<v[n].size();i++)
		if(!dfs(v[n][i],-1*c)) return false;
	return true;
}

int main()
{
	int N,M;
	cin>>N>>M;
	int i,j,k,temp1,temp2;
	for(i=0;i<M;i++){
		scanf("%d%d",&temp1,&temp2);
		v[temp1].push_back(temp2);
		v[temp2].push_back(temp1);
	} 
	int num=0;
	for(i=1;i<=N;i++){
		if(0==v[i].size()) continue;//要处理好单独一个点的情况 
		cnt1=0,cnt2=0;
		if(0==color[i]&&!dfs(i,1)){
			printf("Impossible\n");
			return 0;
		}
		num+=min(cnt1,cnt2);
	}
	printf("%d\n",num);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值