UVa 10004 - Bicoloring

题目:求给定的图是否是双色图。

分析:图论,搜索。利用bfs或者floodfill求解即可。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

int point[ 201 ];
int table[ 201 ];
int queue[ 201 ];
int inque[ 201 ];
int maps[ 201 ][ 201 ];

int bfs( int n )
{
	table[0] = 1;
	queue[0] = 0;
	
	int head = 0,tail = 1;
	while ( head < tail ) {
		int now = queue[head ++];
		int col = table[now];
		for ( int i = 0 ; i < n ; ++ i )
			if ( maps[now][i] ) {
				if ( table[i] == col )
					return 1;
				else if ( !inque[i] ) {
					inque[i] = 1;
					table[i] = 3-col;
					queue[tail ++] = i;
				}
			}
	}
	return 0;
}

int main()
{
	int n,m,a,b;
	while ( scanf("%d",&n) && n ) {
		memset( maps, 0, sizeof( maps ) );
		memset( table,0, sizeof( table) );
		memset( inque,0, sizeof( inque) );
		
		scanf("%d",&m);
		for ( int i = 0 ; i < m ; ++ i ) {
			scanf("%d%d",&a,&b);
			maps[a][b] = 1;
			maps[b][a] = 1;
		}
		
		if ( bfs( n ) ) 
			printf("NOT BICOLORABLE.\n");
		else printf("BICOLORABLE.\n");
	}
	
	return 0;
}

你可能感兴趣的:(UVa 10004 - Bicoloring)