uva 11396 Claw Decomposition(二分图判定 BFS)

uva 11396 Claw Decomposition

这里写图片描述
A claw is defined as a pointed curved nail on the end of each toe in birds, some
reptiles, and some mammals. However, if you are a graph theory enthusiast, you
may understand the following special class of graph as shown in the following
figure by the word claw.
If you are more concerned about graph theory terminology, you may want to
define claw as K1,3.
Lets leave the definition for the moment and come to the problem. You are
given a simple undirected graph in which every vertex has degree 3. You are to
figure out whether the graph can be decomposed into claws or not.
Just for the sake of clarity, a decomposition of a graph is a list of subgraphs such that each edge
appears in exactly one subgraph in the list.
Input
There will be several cases in the input file. Each case starts with the number of vertices in the graph,
V (4 ≤ V ≤ 300). This is followed by a list of edges. Every line in the list has two integers, a and b,
the endpoints of an edge (1 ≤ a, b ≤ V ). The edge list ends with a line with a pair of ‘0’. The end of
input is denoted by a case with V = 0. This case should not be processed.
Output
For every case in the input, print ‘YES’ if the graph can be decomposed into claws and ‘NO’ otherwise.
Sample Input
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
0
Sample Output
NO
NO

题目大意:给出一个n个结点的简单无向图,每个点的度数均为3。你的任务是判断能否把它分解成若干个爪。在你的分解方案中,每条边必须恰好只属于一个爪,但同一个节点可以出现在多个爪里。
解题思路:和爪中心相邻的点不能是爪中心,和爪子相邻的点,不能是爪子。上色加BFS。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;

typedef long long ll;
const int N = 305;
int n;
int Gra[N][N], color[N], vis[N];

void input() {
    memset(Gra, 0, sizeof(Gra));
    memset(color, -1, sizeof(color));
    memset(vis, 0, sizeof(vis));
    int a, b;
    while (scanf("%d %d", &a, &b)) {
        if (!a && !b) break;    
        Gra[a][b] = Gra[b][a] = 1;
    }
}

bool BFS() {
    queue<int> Q;
    Q.push(1);
    color[1] = 1;
    while (!Q.empty()) {
        int u = Q.front(); Q.pop(); 
        int c = color[u];
        for (int v = 1; v <= n; v++) {
            if (Gra[u][v]) {
                if (!vis[v]) {
                    Q.push(v);
                    vis[v] = 1;
                }
                if (color[v] != -1) {
                    if (c == color[v]) return false;
                } else {
                    color[v] = c^1;
                }
            }
        }
    }
    return 1;
}

int main() {
    while (scanf("%d", &n) != EOF) {
        if (!n) break;
        input();    
        if (BFS()) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值