您的位置:首页 > 其它

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

2017-07-17 18:29 281 查看
题目大意:给你一张无向图,每一个点的度数都是3。

你的任务是推断是否能把它分解成若干个爪(每条边仅仅能属于一个爪)

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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: