天天看点

UVA 10004 BicoloringUVA 10004   Bicoloring 

UVA 10004   Bicoloring 

题意:输入数据,构造出一个图,你能用2种颜色去填充该图的每个节点,如果有2个相邻结点颜色相同,该图为NOT Bicoloring,如果每相邻颜色都不同,则为Bicolring。 思路:搜索的 水题,用DFS从每个点为起点都找一次,当找到回路的时候,判断该回路结点个数如果是奇数,必然有2个相邻结点颜色会相同,跳出。如果是偶数,那就。。。继续找吧 代码:

#include <stdio.h>
#include <string.h>

int n, m;
int lu[205][205];
int num[205];
int visd[205];
int vis[205][205];
int a, b;
int judge;
void dfs(int n, int nu)
{
    for (int i = 0; i < num[n]; i ++)
    {
	if (vis[n][lu[n][i]] == 0)
	{
	    if (visd[lu[n][i]] == 1)
	    {
		if (nu % 2)
		{
		    judge = 1;
		    return;
		}
		return;
	    }
	    visd[lu[n][i]] = 1;
	    vis[n][lu[n][i]] = vis[lu[n][i]][n] = 1;
	    dfs(lu[n][i], nu + 1);
	    visd[lu[n][i]] = 0;
	    vis[n][lu[n][i]] = vis[lu[n][i]][n] = 0;
	}
    }
}
int main()
{
    while (scanf("%d", &n) != EOF && n)
    {
	judge = 0;
	memset(lu, 0, sizeof(lu));
	memset(num, 0, sizeof(num));
	scanf("%d", &m);
	for (int i = 0; i < m; i ++)
	{
	    scanf("%d%d", &a, &b);
	    lu[a][num[a]]  = b;
	    lu[b][num[b]] = a;
	    num[a] ++;
	    num[b] ++;
	}
	for (int i = 0; i < n; i ++)
	{
	    memset(visd, 0, sizeof(visd));
	    memset(vis, 0, sizeof(vis));
	    visd[i] = 1;
	    dfs (i, 1);
	    if (judge)
	    {
		printf("NOT BICOLORABLE.\n");
		break;
	    }
	}
	if (!judge)
	    printf("BICOLORABLE.\n");
    }
    return 0;
}