二分图的定义及判断

摘要:
图形[MAXN];j<伦恩;41}4243intmain()44{45intT;sizeof(graph));&sizeof(vis));i<69break;BFS1#include<stdio.h>算法>13#include&llt;1920vector<graph[MAXN];21intcolor[MAXN;

二分图又称作二部图,是图论中的一种特殊模型。 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A, j in B),则称图G为一个二分图。

二分图的另一种等价的说法是,可以把每个节点着以黑色和白色之一,使得每条边的两个端点颜色不同.不难发现,非连通的图是二分图当且仅当每个连通分量都是二分图,因此我们只考虑无向连通图。


二分图的定义及判断第1张     

上图就是一个二分图

二分图的定义及判断第2张

上图不是个二分图

那么我们如何去判断一个图是否是二分图呢?

这里我们采取的是二分图染色法:用两种颜色,对所有顶点逐个染色,且相邻顶点染不同的颜色,如果发现相邻顶点染了同一种颜色,就认为此图不为二分图。 当所有顶点都被染色,且没有发现同色的相邻顶点,就退出

第一种写法:DFS

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <stdbool.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <string.h>
 8 #include <math.h>
 9 #include <vector>
10 #include <queue>
11 #include <stack>
12 #include <set>
13 #include <map>
14 
15 #define INF 0x3f3f3f3f
16 #define LL long long
17 #define MAXN 1000010
18 using namespace std;
19 
20 vector<int> graph[MAXN];
21 int color[MAXN];
22 int vis[MAXN];
23 
24 bool DFS(int u)
25 {
26     vis[u] = 1;
27     int len = graph[u].size();
28     for (int j=0;j<len;j++)
29     {
30         int v = graph[u][j];
31         if (vis[v] == 0)  //如果没有染色
32         {
33             color[v] = color[u]^1;
34             if (!DFS(v))
35                 return false;
36         }
37         else if (color[u]==color[v]) //如果已经染色,但是相连的两点颜色相同
38             return false;
39     }
40     return true;
41 }
42 
43 int main()
44 {
45     int T;
46     scanf("%d",&T);
47     while (T--)
48     {
49         int n,m;
50         scanf("%d%d",&n,&m);
51         memset(graph,0, sizeof(graph));
52         for (int i=1;i<=m;i++)
53         {
54             int a,b;
55             scanf("%d%d",&a,&b);
56             graph[a].push_back(b);
57             graph[b].push_back(a);
58         }
59         memset(color,0, sizeof(color));
60         memset(vis,0, sizeof(vis));
61         int flag = true;
62         for (int i=1;i<=n;i++)
63         {
64             if (vis[i] == 0)
65             {
66                 if (!DFS(i))
67                 {
68                     flag = false;
69                     break;
70                 }
71             }
72         }
73         if (flag)
74             printf("Yes
"); // 是二分图
75         else
76             printf("No
");
77     }
78 }

第二种方法:BFS

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <stdbool.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <string.h>
 8 #include <math.h>
 9 #include <vector>
10 #include <queue>
11 #include <stack>
12 #include <set>
13 #include <map>
14 
15 #define INF 0x3f3f3f3f
16 #define LL long long
17 #define MAXN 100
18 using namespace std;
19 
20 vector<int> graph[MAXN];
21 int color[MAXN];
22 
23 
24 bool BFS(int u)
25 {
26     queue<int> que;
27     que.push(u);
28     color[u] = 1;
29     while (!que.empty())
30     {
31         int x = que.front();
32         que.pop();
33         for (int i=0;i<graph[x].size();i++)
34         {
35             int y = graph[x][i];
36             if (color[y] == 0)
37             {
38                 color[y] = color[x]^1;
39                 que.push(y);
40             }
41             else
42             {
43                 if (color[x] == color[y])
44                     return false;
45             }
46         }
47     }
48     return true;
49 }
50 
51 int main()
52 {
53     int n,m;
54     cin >> n >> m;
55     for (int i=1;i<=m;i++)
56     {
57         int x,y;
58         cin >> x >> y;
59         graph[x].push_back(y);
60         graph[y].push_back(x);
61     }
62     memset(color,0, sizeof(color));
63     //cout << BFS(1) << endl;
64     if (BFS(1))
65         cout << "YES" << endl;
66     else
67         cout << "NO" << endl;
68     return 0;
69 }

免责声明:文章转载自《二分图的定义及判断》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇9、QT QLineEdit 密码模式海量小文件存储与Ceph实践下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

filter 以及 orderBy的使用

filter用于关键字过滤操作,orderBy用于排序操作,运行界面如下: 点击标题Name与Email实现排序功能,输入框中输入关键字进行过滤,同时实现根据关键字进行过滤后进行排序操作: ng-repeat="user in users | filter:keyword | orderBy:sortField:reverse" index.html &...

利用JavaCSV API来读写csv文件

http://blog.csdn.net/loongshawn/article/details/53423121 http://javacsv.sourceforge.net/ 转载请注明来源-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/53423121 1 背景 CSV文件...

django数据库的增删改查

正常启动服务安装数据库cmd命令:python manage.py makemigrations 创建用户 在pxm里面会有一个py文件..这就是生成了表结构cmd命令:python manage.py migrate 自带的一些表 导入这就是正向导入数据库 反向导入数据库:python manage.py inspectdb > son1/mode...

SpringBoot:Sqlite3+SpringBoot2.1.3+Mybatis-Puls整合项目

应公司要求完成sqlite3数据库的增改查小功能,特此记录一下。 1.建造项目 结构如下 因为是提供给前端调用所以做了接口。 2.Pom依赖文件 下面是这个项目所依赖的jar包。 <parent> <groupId>org.springframework.boot</groupId>...

Maven的Dependency怎么找?

用了Maven,所需的JAR包就不能再像往常一样,自己找到并下载下来,用IDE导进去就完事了,Maven用了一个项目依赖(Dependency)的概念,用俗话说,就是我的项目需要用你这个jar包,就称之为我的项目依赖你这个包,换句话说,你这个JAR包就是我这个项目的Dependency。       于是很多人在刚用Maven的时候,就会在加Depende...

window.open的小技巧分享(转)

   今天再次谈起window.open是因为发现了一个比较好玩的小技巧,详细内容我们稍后详细说明。       聊到window.open,不得不说明一下他的使用方法,主要有两种形式:   window.open()没有任何参数,这种方式可以新标签打开页面 window.open(url, name, pars),带有参数的可以在当前页面打开窗口...