UVa11396 - Claw Decomposition(染色问题)

本文探讨了图论中一个有趣的问题——爪图分解。给定一个每个顶点度数为3的简单无向图,判断该图是否可以被分解为一系列的爪图(K1,3)。文章详细介绍了输入输出格式,并提供了示例来帮助理解。

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

 

Problem B
Claw Decomposition

Input: Standard Input

Output: Standard Output

 

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.

 

Let’s leave the definition for the moment & 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 & 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 & NO otherwise.

 

Sample Input                                                  Output for 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

NO

NO

 

#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

const int N = 310;

typedef vector<int> vii;

int n;
int e;
vector<vii> adjList;
int color[N];

bool input()
{
    scanf("%d", &n);
    if (n == 0) return false;

    int a, b;
    e = 0;
    adjList.clear();
    adjList.assign(n, vii());

    while (1) {
        scanf("%d%d", &a, &b);
        if (a == 0 && b == 0) break;
        e++;
        a--, b--;
        adjList[a].push_back(b);
        adjList[b].push_back(a);
    }

    return true;
}

bool dfs(int u, int c)
{
    color[u] = c;
    //printf("u=%d\n", u);
    for (size_t i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (!color[v]) {
            if (!dfs(v, 3 - c)) return false;
        } else {
            if (color[v] == c) return false;
        }
    }

    return true;
}

void solve()
{
    if (n * 3 != 2 * e) {
        printf("NO\n");
        return;
    }

    memset(color, 0x00, sizeof(color));
    if (dfs(0, 1)) {
        printf("YES\n");
    } else {
        printf("NO\n");
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("e:\\uva_in.txt", "r", stdin);
#endif

    while (input()) {
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值