zoukankan      html  css  js  c++  java
  • 广度优先搜索有环图

    // 以1 为起点 ,广度优先搜索 , 图是有连通,有环的 ,所以要标记颜色 , 白色代表 未访问 , 灰色代表 已进入队列 , 黑色代表访问完 。
    #include <stdio.h>
    #include <vector>
    #include <queue>
    #include <iostream>
    using namespace std ;

    enum colors { white , gray , black } ;
    colors color[10] ;
    vector<int> v[9] ;
    void bfs()
    {
    queue<int> q ;
    q.push( 1 );

    int temp ;
    while ( ! q.empty())
    {
    temp = q . front();
    cout << temp <<endl ;//输出结果
    color[ temp ] = black ; //标记已经访问
    for ( int i = 0 ; i < v[temp].size() ; i ++ )
    if ( color[ v[temp][i] ]== 0 ) //判断条件要为未标记 ,未进入队列 , 白色 才可以进入
    {
    color[ v[temp][i] ] = gray ; //标记成灰色
    q.push( v[temp][i]) ; //压入队列
    }
    q.pop(); //弹出优先队列
    }
    }
    int main()
    {

    int a , b ;
    memset( color ,white , sizeof( color )) ;
    for ( int i = 0 ; i < 11 ; i ++)
    {
    scanf("%d%d" , &a , &b );
    v[a].push_back( b) ;
    v[b].push_back( a );
    }
    bfs( );

    }
  • 相关阅读:
    Sgu294He's Circles
    [HNOI2008]Card洗牌
    传球游戏
    [BZOJ1478]Sgu282 Isomorphism
    [POJ2154]Color
    [ZOJ1961]Let it Bead
    BZOJ1257 [CQOI2007]余数之和sum
    BZOJ1192 [HNOI2006]鬼谷子的钱袋
    BZOJ4614 [Wf2016]Oil
    BZOJ3209 花神的数论题
  • 原文地址:https://www.cnblogs.com/lzhenf/p/2300499.html
Copyright © 2011-2022 走看看