码迷,mamicode.com
首页 > 其他好文 > 详细

uva 11396Claw Decomposotion(二分图判定)

时间:2016-01-15 22:45:32      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:



题目大意:给出一个简单无向图,每一个点的度为3。推断是否能将此图分解成若干爪的形式。使得每条边都仅仅出如今唯一的爪中。

(点能够多次出如今爪中)

这道题实质上就是问这个图是否为二分图,dfs判定就可以

#include<cstdio>  
#include<cstring>  
#include<cmath>  
#include<cstdlib>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<queue>  
#include<stack> 
#include<string>
#include<map> 
#include<set>
#define eps 1e-6 
#define LL long long  
using namespace std;  

const int maxn = 300 + 5;
const int INF = 0x3f3f3f3f;
vector<int> G[maxn]; 
//dfs给二分图进行黑白二着色,用颜色1表示黑色,颜色2表示白色,0表示没着色 
int color[maxn];    //推断节点u所在的连通分量是否为二分图
int n; 
bool bipartite(int u) {
	for(int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if(color[v] == color[u]) return false;
		if(!color[v]) {
			color[v] = 3 - color[u];
			if(!bipartite(v)) return false;
		}
	}
	return true;
} 

void init() {
	memset(color, 0, sizeof(color));
	for(int i = 1; i <= n; i++) G[i].clear();
	int x, y;
	while(scanf("%d%d", &x, &y) == 2 && x) {
		G[x].push_back(y);
		G[y].push_back(x);
	}
}

void solve() {
	color[1] = 1;
	if(bipartite(1)) puts("YES");
	else puts("NO");
}

int main() {
	 //freopen("input.txt", "r", stdin);
	while(scanf("%d", &n) == 1 && n) {
		init();
		solve();
	}
	return 0;
}










uva 11396Claw Decomposotion(二分图判定)

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/5134576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!