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[500][500];
int vis[500],n;
bool dfs()
{
    queue<int>q;
    memset(vis,0,sizeof(vis));
    q.push(0);
    vis[0]=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;
}

我想对千千说~