二分图判断(dfs)

二分图着色法判断

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>

using namespace std;
const int maxn = 210;
int map[maxn][maxn] , color[maxn];
int V , e , sta , last;
bool dfs(int v ,int c)
{
    color[v] = c;
    for(int i = 0 ; i < V ; i ++)
    {
        if(map[v][i] == 1 && color[i] == c)
            return false;
        if(map[v][i] == 1 && color[i] == 0 && !dfs(i,-c))
            return false;
    }
    return true;
}

int main(void)
{
    while(~scanf("%d %d",&V,&e))
    {
        memset(map,0,sizeof(map));
        memset(color,0,sizeof(color));
        for(int i = 1 ; i <= e ; i ++)
        {
            scanf("%d %d",&sta, &last);
            map[sta][last] = 1;
            map[last][sta] = 1;
        }
        if(!dfs(0,1))
            printf("NOT BICOLORABLE.\n");
        else
            printf("BICOLORABLE.\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Zenith_Habitant/article/details/83539152
今日推荐