当前位置:网站首页>染色法判断二分图
染色法判断二分图
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
边栏推荐
- CSDN common complex formula template record
- Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
- 20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
- Solving the weird problem that the query conditions affect the value of query fields in MySQL query
- Interface documentation system - Yapi
- HDU 2488 A Knight's Journey(DFS)
- Authentication principle of Ranger plug-in
- ArrayList分析1-循环、扩容、版本
- 问题解决:如何管理线程私有(thread_local)的指针变量
- Length of the longest integrable subarray
猜你喜欢

HDU 2488 A Knight's Journey(DFS)

NE555 waveform generator handle tutorial NE555 internal structure (I)

C#生成putty格式的ppk文件(支持passphrase)

【原创】 PLSQL 索引排序优化

Implementation of date class

Get to know the drawing component of flutter - custompaint

Two-stage RO: part 1

20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br
![[original] PLSQL index sorting optimization](/img/91/dcd0262705a19645f1a87d4cf03bc8.jpg)
[original] PLSQL index sorting optimization

P4学习——p4runtime
随机推荐
ArrayList分析1-循环、扩容、版本
Member management applet actual development 07 page Jump
Can JDBC based on openjdk connect to MySQL?
Join table query select generation
ArrayList分析1-循环、扩容、版本
Basic knowledge of Embedded Network - introduction of mqtt
Wechat official account development (1) introduction to wechat official account
Authentication principle of Ranger plug-in
1009 product of polynomials (25 points) [PTA class A]
HDU 2488 A Knight's Journey(DFS)
Chapter 53 overall understanding of procedures from the perspective of business logic implementation
2022-2028 global capsule shell industry research and trend analysis report
【原创】 PLSQL 索引排序优化
问题解决:如何管理线程私有(thread_local)的指针变量
The principle of journal node
History of deep learning
第53章 从业务逻辑实现角度整体性理解程序
Some views on libco
ArrayList analysis 1-cycle, capacity expansion, version
【2023联发科提前批笔试题】~ 题目及参考答案