还是宽搜,题目已经说明是连通图了,表示没有点是独立存在的,所以只需要进行一次宽搜,就可以遍历所有的点和所有的边。能否用两种颜色进行染色,关键在于有连通的两个点不能是相同的颜色,故宽搜时对访问点进行颜色判断和标记即可。
Run Time: 0sec
Run Memory: 1164KB
Code length: 1009Bytes
Submit Time: 2011-12-24 12:55:54
// Problem#: 4379
// Submission#: 1121129
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int main()
{
int n, m;
int i, j;
queue<int> q;
int color[ 1001 ];
bool path[ 1001 ][ 1001 ];
memset( color, 0, sizeof( color ) );
memset( path, false, sizeof( path ) );
scanf( "%d%d", &n, &m );
while ( m-- ) {
scanf( "%d%d", &i, &j );
path[ i ][ j ] = true;
path[ j ][ i ] = true;
}
q.push( 1 );
color[ 1 ] = -1;
while ( !q.empty() ) {
i = q.front();
for ( j = 1; j <= n; j++ ) {
if ( path[ i ][ j ] ) {
if ( color[ i ] == color[ j ] ) {
printf( "no\n" );
return 0;
}
if ( color[ j ] == 0 ) {
q.push( j );
color[ j ] = ( color[ i ] == 1 ? -1: 1 );
}
}
}
q.pop();
}
printf( "yes\n" );
return 0;
}