您的位置:首页 > 其它

UVa 11396 爪分解(二分图判定)

2017-04-30 16:42 197 查看
https://vjudge.net/problem/UVA-11396

题意:

给出n个结点的简单无向图,每个点的度数均为3。你的任务是判断能否把它分解成若干爪。每条边必须属于一个爪,但同一个点可以出现在多个爪里。

思路:

一个鸡爪当中,有一个中心点,即度为3的点,还有3个边缘点。

每条边都连接了一个中心点和一个边缘点,于是就是二分图判定。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;

const int maxn=300+5;

vector<int> G[maxn];
int color[maxn];

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]==0)
{
color[v]=3-color[u];
if(!bipartite(v))   return false;
}
}
return true;
}

int n;

int main()
{
//freopen("D:\\input.txt","r",stdin);
while(~scanf("%d",&n) && n)
{
for(int i=1;i<=n;i++)   G[i].clear();
int u,v;
while(true)
{
scanf("%d%d",&u,&v);
if(u==0 && v==0)  break;
G[u].push_back(v);
G[v].push_back(u);
}
memset(color,0,sizeof(color));
color[1]=1;
bool flag = bipartite(1);
if(flag)  puts("YES");
else puts("NO");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: