您的位置:首页 > 其它

UVa 10004 Bicoloring

2010-04-26 23:08 323 查看
/*
File: 10004.cpp
Author: ACboy
Date: 2010-4-26
Result: 1A
Descripition: UVa 10004 Bicoloring
*/

#include <iostream>
using namespace std;

int edges[210][210]; /* adjacency information */
int degree[210];     /* outdegree of each vertices */
int nvertices;       /* number of vertices of graph */
int nedges;          /* number of edges of graph */
int colors[210];     /* the colors of the each vertices*/
int vis[210];        /* flag the vertices if visited */

void bfs() {
int queue[210];
int front;
int rear;
front = 0;
rear = 0;
memset(colors, 0, sizeof(colors));
memset(vis, 0, sizeof(vis));
vis[0] = 1;
queue[front++] = 0;
int c = 0; // 用来标记被成功染色的顶点的个数
while (rear < front) {
int v = queue[rear++];
int i;
int flag1 = 0; /* 用来标记顶点v的邻接点中是否有染色为1的点 */
int flag2 = 0; /* 用来标记顶点v的邻接点中是否有染色为2的点 */
for (i = 0; i < degree[v]; i++) {
if (colors[edges[v][i]] == 1) {
flag1 = 1;
}
if (colors[edges[v][i]] == 2) {
flag2 = 1;
}
}
if (flag2 == 0 && flag1 == 1) { /* 顶点v邻接点中只有染色为1的点*/
colors[v] = 2;              /* 将顶点v染色为2*/
c++;                        /* 成功染色的顶点个数加一 */
}
if (flag1 == 0 && flag2 == 1) {
colors[v] = 1;
c++;
}
if (flag1 == 0 && flag2 == 0) {
colors[v] = 1;
c++;
}
for (i = 0; i < degree[v]; i++) {
if (vis[edges[v][i]] == 0) {
queue[front++] = edges[v][i];
vis[edges[v][i]] = 1;
}
}
}
if (c == nvertices) {
cout << "BICOLORABLE." << endl;
} else {
cout << "NOT BICOLORABLE." << endl;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("10004.txt", "r", stdin);
#endif
while (cin >> nvertices) {
if (nvertices == 0) break;
else {
cin >> nedges;
int i;
int x, y;
memset(degree, 0, sizeof(degree));
for (i = 0; i < nedges; i++) {
cin >> x >> y;
edges[x][degree[x]] = y;
degree[x]++;
edges[y][degree[y]] = x;
degree[y]++;
}
bfs();
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  colors graph each file c 2010