当前位置:网站首页>Chromatic judgement bipartite graph
Chromatic judgement bipartite graph
2022-07-01 01:19:00 【Come on, come on】
subject :
Input n m Express n vertices m side , There may be double edges and self rings . Judge whether the graph is a bipartite graph .
Ideas :
A bipartite graph is a value that divides a set of points into two halves , There are no edges in each set , But it can form an edge with points in another set , If and only if a graph does not have odd rings, it is a bipartite graph , Odd ring means that the number of sides in the ring is odd , Dyeing method : Dye a graph with only 1 and 2 Two colors ,dfs Every point , Connected dyed in different colors , If you find that the two points are connected and the color is the same , Then there is an odd ring , It must not be a bipartite graph .
Code :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Since the graph does not contain odd rings , So there must be no contradiction in the dyeing process
To judge a bipartite graph by coloring
for All points v :
if v No staining
dfs(v ,1) // v Dyeing 1 Color number
*/
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 It means you haven't dyed ,1,2 Represents two colors
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);// Undirected graph
add(b, a);
--m;
}
boolean flag = true;
for (int i = 1; i <= n; i++) {
if (color[i] == 0) {// If this dot has not been dyed ,
if (!dfs(i, 1)) { // spot i Dyeing failed , Then it shows that the graph is not a bipartite graph
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++;
}
/*
* Return to the current point v Dyeing of c Color condition
* Return the failure reason :
* 1.v The connection point of j Dyeing failed
* 2.v The connection point of j And v The dyeing condition is the same
* If it fails, it indicates that the graph is not a bipartite graph
* */
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;
}
}
Use cases :
4 4
1 3
1 4
2 3
2 4
Output :
Yes
边栏推荐
- uniapp官方组件点击item无效,解决方案
- 系统设置大页
- PHP online confusion encryption tutorial sharing + basically no solution
- Detailed analysis of operators i++ and ++i in JS, i++ and ++i
- Shift operators
- Vnctf 2022 cm CM1 re reproduction
- Practical shell knowledge
- Locking relay ydb-100, 100V
- 冲击继电器ZC-23/DC220V
- Cmu15445 (fall 2019) project 1 - buffer pool details
猜你喜欢
5. TPM module initialization
Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)
Cmu15445 (fall 2019) project 1 - buffer pool details
Docker deployment MySQL 8
孔乙己第一问之服务通信知多少?
Vnctf 2022 cm CM1 re reproduction
人穷志不短,穷学生也能玩转树莓派
Interpreting the scientific and technological literacy contained in maker Education
Impact relay zc-23/dc220v
Green, green the reed. dew and frost gleam.
随机推荐
C语言一点点(未来可会增加)
[LeetCode] 爬楼梯【70】
PHP online confusion encryption tutorial sharing + basically no solution
ASCII、Unicode、GBK、UTF-8之间的关系
Exercises on recursion in C language
pytorch编程知识(2)
[network packet loss and network delay? This artifact can help you deal with everything!]
Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support
The real topic of the 11th provincial competition of Bluebridge cup 2020 - crop hybridization
关于VCTK数据集
Shift operators
Double linked list: initialize insert delete traversal
Service
What is the difference between Pipeline and Release Pipeline in azure devops?
小程序自定义宫格
[learning notes] structure
DX-11Q信号继电器
Oracle table creation and management
Using C language to realize the exchange between the contents of two arrays (provided that the array is the same size)
[original] PLSQL index sorting optimization