您的位置:首页 > 其它

Uva11396 Claw Decomposition(爪分解)

2016-07-10 08:15 190 查看
uva11396 爪分解

【问题描述】

给出n(n≤300)个节点的简单无向图(无自环无重边),每个点的度为3。现在你需要判断能否将它分解成若干个爪(如图所示)。在你的方案中,每条边必须恰好属于一个爪,但同一个节点可以出现在多个爪里。

        

 


【输入格式】

  多组输入数据:

  每组数据第一行为这个图的点数n,第二行开始每行2个整数a, b(1 <= a, b <= n)为该图的边,以”0 0”结束。

【输出格式】

  对于每组数据,如果能分解则输出”YES”否则输出”NO”

【输入样例】

4

1 2

1 3

1 4

2 3

2 4

3 4

0 0

6

1 2

1 3

1 6

2 3

2 5

3 4

4 5

4 6

5 6

0 0

【输出样例】

NO

NO

【数据范围】

n≤300

【来源】

《大白书》373页 , Uva11396

分析:此题是一道经典的二分图题目,从题目中可以很轻易地知道爪的中间结点和爪的边缘节点属于两种类型,爪的边缘节点永远只能是边缘节点,否则爪的便就不能仅属于一个爪。故可凭此进行二分。

代码如下:

#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#define maxn 10005
using namespace std;
int T,m,n,color[maxn]={0};
vector<int>g[maxn];
bool BFS(int s){
queue<int>q;
q.push(s);
color[s]=1;   //white
while(!q.empty()){
int i=q.front();q.pop();
int sz=g[i].size();
for(int k=0;k<sz;k++){
int j=g[i][k];
if(color[i]==color[j])return false;
if(color[j]==0){
q.push(j);
color[j]=3-color[i];
}
}
}
return true;
}
void work(){
while(scanf("%d",&n)==1){
int x,y;
for(int i=1;i<=n;i++)g[i].clear();
scanf("%d%d",&x,&y);
while(x!=0&&y!=0){
g[x].push_back(y);
g[y].push_back(x);
scanf("%d%d",&x,&y);
}
bool ok=true;
memset(color,0,sizeof(color));
for(int t=1;t<=n;t++){
if(color[t]==0){
ok=BFS(t);
if(!ok)break;
}
}
if(ok)printf("YES\n");
else printf("NO\n");
}
}
int main(){
work();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: