您的位置:首页 > 其它

(beginer)DFS UVA 11396 Claw Decomposition

2014-02-08 09:54 218 查看
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(); }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: