(beginer)DFS UVA 11396 Claw Decomposition

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


题意:给出n个节点的简单无向图,每个点的度数是3.你的任务是判断能不能把它分解成若干个爪(K1,3),在你的分解方案中,每条边必须恰好属于一个爪,但同一个节点可以出现在多个爪里。

思路:首先每条边都要属于一个爪,对于每一个点,与他相邻的点在爪中的位置一定是不同的,如果这个点是爪的中心,那么周围3个点都是爪子的3个2度的点,如果这个点是爪中2度的点,那么周围3个点都应该是3度的那个中心节点。所以就是把节点分成两类就行了。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
const int maxn = 2000;

struct Node 
{
int y;
Node *next;
}edge[maxn] , *first[maxn];

int n , m;
int color[310];

void init()
{
memset(edge,0,sizeof(edge));
memset(first,0,sizeof(first));
m = 0;
}

void add(int x,int y)
{
edge[++m].y = y;
edge[m].next = first[x];
first[x] = &edge[m];
}

void input()
{
int x , y;
while (scanf("%d%d",&x,&y),x+y)
{
add(x,y);
add(y,x);
}
}

bool bipartite(int x)
{
Node * p = first[x];
while (p)
{
int y = p->y;
if (color[x]==color[y]) return false;
if (!color[y])
{
color[y] = 3-color[x];
if (!bipartite(y)) return false;
}
p = p->next;
}
return true;
}

void solve()
{
memset(color,0,sizeof(color));
color[1] = 1;
if (bipartite(1)) cout << "YES" << endl;
else cout << "NO" << endl;
}

int main()
{
while (scanf("%d",&n),n)
{
init();
input();
solve();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值