Uva 11396 爪分解

题目链接:https://vjudge.net/contest/166461#problem/A

题意:

给定一个图,特点是每个点的度都是3,求是不是原图可以分解为全部鸡爪;每条边只属于一个鸡爪;

分析:

每一个鸡爪的根对应三个其他的顶点,但是这三个点不能再作为鸡爪的根了,

这样,两个鸡爪根就不能直接相连,这就是二分图染色,其中作为根的在一边,作为附属点的另一边;

#include <bits/stdc++.h>

using namespace std;

const int maxn = 305;
vector<int> G[maxn];
int color[maxn];

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;
        }
    }
}

int main()
{
    int n;
    while(scanf("%d",&n),n) {
        memset(color,0,sizeof(color));

        for(int i=0;i<=n;i++)
            G[i].clear();
        int u,v;
        while(scanf("%d%d",&u,&v)) {
            if(u==0&&v==0)
                break;
            G[u].push_back(v);
            G[v].push_back(u);
        }

        color[1] = 1;
        if(bipartite(1))
            puts("YES");
        else puts("NO");

    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/TreeDream/p/6952534.html