# UVA 11396：Claw Decomposition(二分图判定)

A claw is defined as a pointed curved nail on the end of each toe in birds, somereptiles, and some mammals. However, if you are a graph theory enthusiast, youmay understand the following special class of graph as shown in the followingfigure by the word claw.If you are more concerned about graph theory terminology, you may want todefine claw as K1,3.Lets leave the definition for the moment and come to the problem. You aregiven a simple undirected graph in which every vertex has degree 3. You are tofigure 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 edgeappears 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 and b,the endpoints of an edge (1 ≤ a, b ≤ V ). The edge list ends with a line with a pair of ‘0’. The end ofinput 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 and ‘NO’ otherwise.

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

Sample Output

NO
NO

AC代码：

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<string.h>
#include<queue>
using namespace std;
int mapp;
int vis,n;
bool dfs()
{
queue<int>q;
memset(vis,0,sizeof(vis));
q.push(0);
vis=1;
while(!q.empty())
{
int p=q.front();
q.pop();
for(int i=0; i<n; i++)
if(mapp[p][i])
{
if(vis[i]==0)
{
vis[i]=-vis[p];
q.push(i);
}
else if(vis[i]==vis[p])
return false;
}
}
return true;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
int a,b;
memset(mapp,0,sizeof(mapp));
while(~scanf("%d%d",&a,&b)&&(a||b))
mapp[a-1][b-1]=mapp[b-1][a-1]=1;
printf(dfs()?"YES\n":"NO\n");
}
return 0;
}