UVA10004

用dfs来做,如果dfs的下一个没访问过,则给其涂上 和相连相反的,如果访问过,则判断其颜色是否和其相连的相同


AC代码:

#include<iostream>
using namespace std;

const int N = 200 + 5;
int G[N][N];
int vis[N];
int color[N];
int m,n;
bool ok;
void init () {
	for (int i = 0 ; i < N ;i++) {
		for (int  j = 0 ; j < N ;j++) {
			G[i][j] = 0;
		}
		vis[i] = 0;
		color[i] = 0 ;
	}
	ok = true;
}
void colorclear() {
	for (int i = 0;i < m ;i++) {
		if (vis[i])
			color[i] = 0;
	}
}
void dfs (int x,int Color) {
	if (vis[x] == 1 && color[x] == Color &&color[x] != 0) {
		ok = false;
		return;
	}
	if (vis[x] == 1)
		return ;
	vis[x] = 1;
	color[x] = -Color;
	for (int i = 0; i < m ;i++) {
		if (G[x][i])
			dfs(i,color[x]);
	}
}
int main () {
	int sta,en;
	while (cin >> m) {
		init();
		if (!m)
			break;
		cin >> n;
		for (int i = 0 ; i < n ;i++) {
			cin >> sta;
			cin >> en;
			G[sta][en] = 1;
			color[sta] = 1;
			color[en] = 1;
		}
		for (int i = 0 ; i < m ; i++ ) 
			for (int j = 0; j < n ; j++) {
				if (G[i][j] && !vis[i]) {
					dfs(i ,-color[i]);
					colorclear();
				}
			}
		if (ok == true)
			cout <<"BICOLORABLE."<<endl;
		else 
			cout <<"NOT BICOLORABLE."<<endl;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值