UVa11396 - Claw Decomposition

本文探讨了一个特定的图论问题——爪分解。对于每个顶点度数为3的简单无向图,判断其是否能被分解成一系列的爪子(即K1,3)。输入包括多个图实例,每个实例包含顶点数及边的信息。输出则针对每个实例给出YES或NO的答案。

摘要生成于 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

 

 


Problemsetter: Mohammad Mahmudur Rahman

Special Thanks to: Manzurur Rahman Khan

判断二部图 用的DFS  其实BFS更佳,因为不容易溢出

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;

const int maxn = 300+10;
int head[maxn];
int color[maxn];
int n,cnt;
struct Edge{
    int nxt,to;
}edge[maxn*maxn];

void addedge(int u,int v){
    cnt++;
    edge[cnt].nxt = head[u];
    edge[cnt].to = v;
    head[u] = cnt;
}

bool dfs(int x){
    for(int i = head[x]; i != -1;i = edge[i].nxt){
        int v = edge[i].to;
        if(color[v] == color[x]) return 0;
        if(!color[v]){
            color[v] = 3-color[x];
            if(!dfs(v)) return false;
        }
    }
    return true;
}

void init(){
    memset(head,-1,sizeof head);
    memset(color,0,sizeof color);
    cnt = 0;
}

int main(){
    while(cin >> n&&n){
        init();
        int a,b;
        while(cin >> a >> b && a+b){
                addedge(a,b);
                addedge(b,a);
        }
        color[1] = 1;
        if(dfs(1)){
            cout<<"YES"<<endl;
        }else{
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值