ACWING--860染色法判定二分图

本文介绍了一种使用染色法判断二分图的方法,通过遍历图的每个节点并进行染色来检测是否存在奇数环,进而判断图是否为二分图。提供了完整的C++代码实现,包括读取图的结构、构建邻接表、深度优先搜索染色等步骤。

860. 染色法判定二分图

 

给定一个n个点m条边的无向图,图中可能存在重边和自环。

请你判断这个图是否是二分图。

输入格式

第一行包含两个整数n和m。

接下来m行,每行包含两个整数u和v,表示点u和点v之间存在一条边。

输出格式

如果给定图是二分图,则输出“Yes”,否则输出“No”。

数据范围

1≤n,m≤1051≤n,m≤105

输入样例:

4 4
1 3
1 4
2 3
2 4

输出样例:

Yes

染色法判断二分图。

二分图资料参考:https://www.cnblogs.com/ninedream/p/11203942.html

假如对当前点u染色,它的邻接点有三种情况:

1、未被染色,则继续将这个点染色。

2、 已经染色过,且与u点颜色不同,跳过此点。

3、已经染色过,与u点颜色相同,则不是二分图,出现奇数环。 

#include <algorithm>    //STL通用算法
#include <bitset>     //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>     //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>      //STL双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>      //基本输入/输出支持
#include<iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL队列容器
#include <set>       //STL 集合容器
#include <sstream>    //基于字符串的流
#include <stack>      //STL堆栈容器    
#include <stdexcept>    //标准异常类
#include <streambuf>   //底层输入/输出支持
#include <string>     //字符串类
#include <utility>     //STL通用模板类
#include <vector>     //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 100000+66;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
int n,m,k;
struct edges
{
    int to;
    int next;
} edge[maxn*2];
int head[maxn];
int color[maxn];
int cnt;
void add_edge(int u,int v)
{
    ++cnt;
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt;
}
bool dfs(int u,int c)
{
    color[u]=c;//染色
    for(int i=head[u]; ~i; i=edge[i].next)
    {
        int to=edge[i].to;
        if(color[to]==c)
        {
            return false;
        }
        else if(!color[to]&&!dfs(to,3-c))
            return false;
    }
    return true;
}
void init()
{
    cnt=0;
    for(int i=1; i<=maxn+10; i++)
    {
        head[i]=-1;
    }
    for(int i=1; i<=maxn+10; i++)
    {
        color[i]=0;
    }
}
int main()
{
    init();
    scanf("%d %d",&n,&m);
    for(int i=1; i<=m; i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        add_edge(u,v);
        add_edge(v,u);
    }
    bool flag=true;
    for(int i=1; i<=n; i++)
    {
        if(!color[i])
        {
            if(!dfs(i,1))
            {
                flag=false;
                break;
            }
        }
    }
    if(flag)
        printf("Yes");
    else
        printf("No");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值