UVA - 11396 Claw Decomposition(二分图染色)

本文介绍了一种针对每个顶点度数为3的无向图进行爪分解的方法。通过二分图染色来判断图是否能被正确分解成若干个爪结构。使用邻接表实现图的存储,并通过递归函数完成染色过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:给你一张无向图,每个点的度数都是3。你的任务是判断能否把它分解成若干个爪(每条边只能属于一个爪)

解题思路:二分图染色裸题。可以得出:爪的中心点和旁边的三个点的颜色是不一样的

#include <cstdio>
#include <cstring>
using namespace std;
#define N 310
#define M 2010

struct Edge{
    int to, Next;
}E[M];
int head[N], color[N], tot;
int n, m;

void AddEdge(int from, int to) {
    E[tot].to = to;
    E[tot].Next = head[from];
    head[from] = tot++;
}

void init() {
    memset(head, -1, sizeof(head));
    tot = 0;

    int u, v;
    while (scanf("%d%d", &u, &v) && u + v) {
        AddEdge(u, v);
        AddEdge(v, u);
    }
}

bool bipartite(int u) {
    for (int i = head[u]; i != -1; i = E[i].Next) {
        int v = E[i].to;
        if (color[v] == color[u])
            return false;
        if (!color[v]) {
            color[v] = 3 - color[u];
            if (!bipartite(v))
                return false;
        }
    }
    return true;
}

void solve() {
    memset(color, 0, sizeof(color));
    color[1] = 1;
    if (bipartite(1))
        printf("YES\n");
    else
        printf("NO\n");
}

int main() {
    while (scanf("%d", &n) != EOF && n) {
        init();
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值