zoukankan      html  css  js  c++  java
  • 二分图判定【图的搜索】

    二分图判定

     

    给定一个图,要给图上每个顶点染色,并且要使得相邻的顶点颜色不同。问是否能最多用2种颜色进行染色?

    参考如下:

    Source Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    vector<int> Edge[1010];
    int colorArr[1010];
    
    void initColorArr(int length){
        for(int i=0;i<=length;++i)
            colorArr[i]=0;
    }
    
    bool DFS(int index,int color){
        colorArr[index]=color;
        for(int cnt=0;cnt<Edge[index].size();++cnt){
            if(colorArr[Edge[index][cnt]]==color)
                return false;
            if(colorArr[Edge[index][cnt]]==0){
                return DFS(Edge[index][cnt],-color);
            }
        }
        return true;
    }
    
    int main(){
        int N,M;  //n为顶点数,m为边数
        int edge_a,edge_b;
        bool flag;
        while(cin>>N>>M){
            for(int index=0;index<N;++index)
                Edge[index].clear();
            initColorArr(N);
            for(int index=0;index<M;++index){
                cin>>edge_a>>edge_b;
                Edge[edge_a].push_back(edge_b);
                Edge[edge_b].push_back(edge_a);
            }
            flag=true;
            for(int index=0;index<N;++index){
                if(colorArr[index]==0){
                    if(DFS(index,1)==false){
                        cout<<"No"<<endl;
                        flag=false;
                        break;
                    }
                }
            }
            if(flag)
                cout<<"Yes"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    C++基础学习1-编译与链接
    html学习
    使用BP拦截POST请求包
    2019.9.17
    搭建LAMP环境
    2019.9.16
    2019.9.12
    2019.9.11
    手脱无名壳tslgame_rl
    一款自制壳的脱壳
  • 原文地址:https://www.cnblogs.com/Murcielago/p/4232273.html
Copyright © 2011-2022 走看看