Uva11396 Claw Decomposition(爪分解)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值