您的位置:首页 > 其它

Uva 11396 Claw Decomposition(二分图判断)

2016-08-03 22:31 363 查看
题意:给出一个n个节点的简单无向图,每个点的度数为3,你的任务是判断能否把它恰好分解成若干个爪,你的分解方案中,每条边必须恰好属于一个爪,但一个点可以出现在多个爪里。

分析:稍微分析一下我们发现,只有二分图能符合条件。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define got(x) (1ll<<x)
using namespace std;
vector <int> G[400];
int n,m,x,y,color[400];
bool dfs(int u,int col)
{
color[u] = col;
for(int v : G[u])
{
if(color[v] && color[v] == color[u]) return false;
if(!color[v] && !dfs(v,3-col)) return false;
}
return true;
}
int main()
{
while(~scanf("%d",&n) && n)
{
bool flag = false;
m = 0;
memset(color,0,sizeof(color));
for(int i = 1;i <= n;i++) G[i].clear();
while(scanf("%d%d",&x,&y) && x+y)
{
m++;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = 1;i <= n;i++)
if(!color[i])
if(!dfs(i,1))
{
cout<<"NO"<<endl;
flag = true;
break;
}
if(!flag) cout<<"YES"<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: