uva 11396 Claw Decomposition(二分图)

题意:给出一个n个节点的简单无向图,每个节点的度数为3,判断能否分解成题意给出的那种爪,一个中心节点带着三个边,每条边只属于一个爪,一个点可以属于多个爪

 

分析:如果一个点是一个爪的中心节点,那么它与它相连的三个点都不可能是一个爪的中心,那么就可以把图抽象为两种节点,染色节点和不染色节点,而且染色节点和染色节点不可能相邻,并且不染色节点也不可能与不染色节点相邻,那么这就是一个裸的二分图

 

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;

int color[maxn];
vector<int> G[maxn];
int n;
bool bipartite(int u){
    for(int i=0;i<G[u].size();i++){
            int v=G[u][i];
            if(color[v]==color[u])return false;
            if(!color[v]){
                    color[v]=3-color[u];
                    if(!bipartite(v))return false;
            }
    }
    return true;
}

void init(){
    memset(color,0,sizeof(color));
    for(int i=1;i<=n;i++)G[i].clear();
}

int main(){
    while(~scanf("%d",&n)&&n){
            init();
            int u,v;
            while(~scanf("%d%d",&u,&v)&&u+v){
                    G[u].push_back(v);
                    G[v].push_back(u);
            }
            color[1]=1;
            if(bipartite(1))
                puts("YES");
            else
                puts("NO");
    }
    return 0;
}

 

posted @ 2016-04-07 20:50  N维解析几何  阅读(155)  评论(0编辑  收藏  举报