UVA 10004 - Bicoloring
生活随笔
收集整理的這篇文章主要介紹了
UVA 10004 - Bicoloring
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
模擬染色,因為只有兩種顏色,所以分別用 0、 1 代表這兩種顏色,然后從0開始深搜,如果
每個點都能染上色,且相鄰兩點的顏色不同,則符合要求。
#include<stdio.h>#include<string.h>
#define MAXN 210
int map[MAXN][MAXN];
int paint[MAXN];
int u, v, M, N;
bool dfs( int i, int color)
{
for( int j = 0; j < N; j ++)
{
if( map[i][j])
{
if( paint[j] != -1 && paint[j] != color)
return false;
else if( paint[j] == - 1)
{
paint[j] = color;
if( !dfs( j, 1 - color)) //用 0和1分別代表白‘黑兩種顏色
return false;
}
}
}
return true;
}
void init()
{
scanf( "%d", &M);
memset( map, 0, sizeof map);
for( int i = 0; i < M; i ++)
{
scanf( "%d%d", &u, &v);
map[u][v] = map[v][u] = true;
}
memset( paint, -1, sizeof paint);
paint[0] = 1;// 如果這里等于0的話則dfs( 0, 1)。
}
int main()
{
while( scanf( "%d", &N), N)
{
init();
if( dfs( 0, 0))
printf( "BICOLORABLE.\n");
else
printf( "NOT BICOLORABLE.\n");
}
return 0;
}
?
轉載于:https://www.cnblogs.com/Yu2012/archive/2011/12/06/2278372.html
總結
以上是生活随笔為你收集整理的UVA 10004 - Bicoloring的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《Pro ASP.NET MVC 3 F
- 下一篇: 牙齿矫正大概要多少钱啊?