当前位置:网站首页>染色法判断二分图
染色法判断二分图
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
边栏推荐
- 20220215 misc buctf easycap Wireshark tracks TCP flow hidden key (use of WinHex tool)
- CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
- leetcode 474. Ones and zeroes (medium)
- Sword finger offer 18 Delete the node of the linked list
- Two-stage RO: part 1
- ArrayList analysis 1-cycle, capacity expansion, version
- Basic data structure of redis
- CSDN常用复杂公式模板记录
- Set different background colors for the border and text of the button
- Web compatibility testing of software testing
猜你喜欢

集群与LVS介绍及原理解析

Solving the weird problem that the query conditions affect the value of query fields in MySQL query

Practical shell knowledge
![[daily record] - bug encountered in BigDecimal division operation](/img/82/3105586841076b9bb1c57eac221ddf.png)
[daily record] - bug encountered in BigDecimal division operation

Oracle data integrity

给按钮的边框和文字设置不同的背景色

What should I do without 50W bride price

Member management applet actual development 07 page Jump

Analysis of blocktoken principle

Sword finger offer 19 Regular Expression Matching
随机推荐
leetcode 474. Ones and Zeroes 一和零(中等)
Docsify building a personal minimalist knowledge warehouse
JS bubble sort and select sort
Authentication principle of Ranger plug-in
How to specify the number of cycles in JSTL- How to loop over something a specified number of times in JSTL?
Shift operators
CSDN常用复杂公式模板记录
HDU 2488 A Knight's Journey(DFS)
Deployment of mini version message queue based on redis6.0
CTF tool (1) -- archpr -- including installation / use process
【2023联发科提前批笔试题】~ 题目及参考答案
20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
问题解决:如何管理线程私有(thread_local)的指针变量
New content violation degree determination scana bad information monitoring capability update issue 5
Sword finger offer 19 Regular Expression Matching
连表查询 select 生成
[DaVinci developer topic] -37- detail IRV: introduction to inter runnable variable + configuration
关于Unity一般的输入操作方式
实验八 T-sql,存储过程
2022-2028 global weight loss ginger tea industry research and trend analysis report