当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
2021电赛F题openmv和K210调用openmv api巡线,完全开源。
Double linked list: initialize insert delete traversal
Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
解析融合学科本质的创客教育路径
[LeetCode] 两数之和【1】
【原创】 PLSQL 索引排序优化
Fluent JSON serialization deserialization
Document service design
Q弹松软的大号吐司,带来更舒服的睡眠
DLS-20型双位置继电器 220VDC
leetcode 474. Ones and zeroes (medium)
Exercises on recursion in C language
用Steam教育启发学生多元化思维
Hoo research | coinwave production - nym: building the next generation privacy infrastructure
ArrayList analysis 1-cycle, capacity expansion, version
Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)
解读创客教育所蕴含的科技素养
Xjy-220/43ac220v static signal relay
Cmu15445 (fall 2019) project 1 - buffer pool details
ESP8266 RC522








