UVA 11396 Claw Decomposition(二分圖判斷)

如果一個節點是一個爪的中心,那麼他只能與其餘三個輔助點相連,那麼就可以發現這是一張二分圖,X爲爪的中心,Y爲三個腳,建完圖染下色就可以了。
Claw Decomposition 
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=300+5;
int head[maxn*maxn],to[maxn*maxn],Next[maxn*maxn];
int color[maxn];
int n,m;
int tot;
void addedge(int u,int v)
{
    to[tot]=v;
    Next[tot]=head[u];
    head[u]=tot++;
}
bool getc(int u)
{
    for(int i=head[u];i!=-1;i=Next[i])
    {
        int v=to[i];
        if(color[v]==-1){
            color[v]=3-color[u];
            if(!getc(v)) return false;
        }
        else if(color[u]==color[v]) return false;
    }
    return true;
}
int main()
{
    while(scanf("%d",&n)&&n)
    {
        m=0;tot=0;
        memset(head,-1,sizeof(head));
        int u,v;
        while(scanf("%d%d",&u,&v)&&(u+v)){
            m++;
            addedge(u,v);
            addedge(v,u);
        }
        if(3*n!=2*m) {printf("NO\n");continue;}
        bool ok=true;
        memset(color,-1,sizeof(color));
        for(int i=1;i<=n;i++){
            if(color[i]==-1){
                color[i]=1;
                ok=getc(i);
                if(ok==false) break;
            }
        }
        if(ok) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章