您的位置:首页 > 其它

UVA - 11396 Claw Decomposition(二分图染色)

2015-08-08 14:44 369 查看
题目大意:给你一张无向图,每个点的度数都是3。你的任务是判断能否把它分解成若干个爪(每条边只能属于一个爪)

解题思路:二分图染色裸题。可以得出:爪的中心点和旁边的三个点的颜色是不一样的

[code]#include <cstdio>
#include <cstring>
using namespace std;
#define N 310
#define M 2010

struct Edge{
    int to, Next;
}E[M];
int head
, color
, tot;
int n, m;

void AddEdge(int from, int to) {
    E[tot].to = to;
    E[tot].Next = head[from];
    head[from] = tot++;
}

void init() {
    memset(head, -1, sizeof(head));
    tot = 0;

    int u, v;
    while (scanf("%d%d", &u, &v) && u + v) {
        AddEdge(u, v);
        AddEdge(v, u);
    }
}

bool bipartite(int u) {
    for (int i = head[u]; i != -1; i = E[i].Next) {
        int v = E[i].to;
        if (color[v] == color[u])
            return false;
        if (!color[v]) {
            color[v] = 3 - color[u];
            if (!bipartite(v))
                return false;
        }
    }
    return true;
}

void solve() {
    memset(color, 0, sizeof(color));
    color[1] = 1;
    if (bipartite(1))
        printf("YES\n");
    else
        printf("NO\n");
}

int main() {
    while (scanf("%d", &n) != EOF && n) {
        init();
        solve();
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: