当前位置:网站首页>染色法判断二分图
染色法判断二分图
2022-07-01 00:37:00 【浪了来来啊】
题目:
输入n m 表示n 个顶点 m 条边,可能有重边和自环。判断该图是不是一个二分图。
思路:
二分图是值可以将点集分成两半,每个集合内都没有边,但可以与另一个集合中的点构成边,当且仅当一个图不存在奇数环的时候是二分图,奇数环是指环中的边数为奇数,染色法:将一个图染成只有1和2两种颜色,dfs每一个点,连通的染成不同颜色,如果发现这两个点连通且颜色相同,则说明出现了奇数环,一定不是一个二分图。
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
由于图中不含有奇数环,所以染色过程中一定没有矛盾
染色法来判断二分图
for 所有点 v :
if v 没有染色
dfs(v ,1) // v染成 1号色
*/
public class Two_Fen_Gra {
static int N = 1000, M = 20000, idx = 0;
static int[] h = new int[N], e = new int[M], ne = new int[M];
static int[] color = new int[N];// 0代表没有染过色,1,2代表两种颜色
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]), m = Integer.parseInt(s[1]);
for (int i = 1; i <= n; i++) {
h[i] = -1;
}
while (m != 0) {
String[] s2 = br.readLine().split(" ");
int a = Integer.parseInt(s2[0]);
int b = Integer.parseInt(s2[1]);
add(a, b);// 无向图
add(b, a);
--m;
}
boolean flag = true;
for (int i = 1; i <= n; i++) {
if (color[i] == 0) {// 如果这个点没有被染过颜色,
if (!dfs(i, 1)) { // 点 i 染色失败,则说明该图不是二分图
flag = false;
break;
}
}
}
if (flag) System.out.println("Yes");
else System.out.println("No");
}
static void add(int a, int b) {
ne[idx] = h[a];
e[idx] = b;
h[a] = idx++;
}
/*
* 返回当前点v的染c色情况
* 返回失败原因:
* 1.v的连接点j染色失败
* 2.v的连接点j与v的染色情况相同
* 返回失败则说明该图不是二分图
* */
static boolean dfs(int v, int c) {
color[v] = c;
for (int i = h[v]; i != -1; i = ne[i]) {
int j = e[i];
if (color[j] == 0) {
if (!dfs(j, 3 - c)) return false;
} else if (color[j] == c) return false;
}
return true;
}
}
用例:
4 4
1 3
1 4
2 3
2 4
输出:
Yes
边栏推荐
- Oracle data integrity
- [original] PLSQL index sorting optimization
- Two-stage RO: part 1
- P4 learning - Basic tunneling
- [DaVinci developer topic] -37- detail IRV: introduction to inter runnable variable + configuration
- New content violation degree determination scana bad information monitoring capability update issue 5
- From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
- Double linked list: initialize insert delete traversal
- Redis based distributed lock
- The question of IBL precomputation is finally solved
猜你喜欢
5. TPM module initialization
Packing and unpacking of C #
Exercises on recursion in C language
P4 learning - p4runtime
New content violation degree determination scana bad information monitoring capability update issue 5
剑指 Offer 18. 删除链表的节点
[daily record] - bug encountered in BigDecimal division operation
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
Sword finger offer 18 Delete the node of the linked list
随机推荐
Solving the weird problem that the query conditions affect the value of query fields in MySQL query
Oracle data integrity
Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
left join左连接匹配数据为NULL时显示指定值
Mindjet mindmanager2022 mind map decompression installer tutorial
The principle and related problems of acid in MySQL
Vnctf 2022 cm CM1 re reproduction
C language file operation for conquering C language
Chapter 53 overall understanding of procedures from the perspective of business logic implementation
Luogu p1168 median
Double linked list: initialize insert delete traversal
Deployment of mini version message queue based on redis6.0
[daily record] - bug encountered in BigDecimal division operation
CSDN常用复杂公式模板记录
剑指 Offer 18. 删除链表的节点
Oracle-数据完整性
Using asyncio for concurrency
剑指 Offer 19. 正则表达式匹配
leetcode 474. Ones and zeroes (medium)
PyTorch安装并使用gpu加速