当前位置:网站首页>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
边栏推荐
- JS方法大全的一个小文档
- Web interface testing of software testing
- (学习力+思考力) x 行动力,技术人成长的飞轮效应总结
- Two position relay st2-2l/ac220v
- 1175. Prime Arrangements
- JS to convert numbers into Chinese characters for output
- Golang treasure house recommendation
- Gavin's insight on the transformer live broadcast course - rasa project's actual banking financial BOT Intelligent Business Dialogue robot system startup, language understanding, dialogue decision-mak
- 双位置继电器DLS-5/2 DC220V
- DX-11Q信号继电器
猜你喜欢

fluttertoast
![[go] go implements row column conversion of sets](/img/d9/6272e55b2d9c6b6fbdf2537773bb83.png)
[go] go implements row column conversion of sets

Dls-42/6-4 dc110v double position relay

High quality pump SolidWorks model material recommended, not to be missed

Left join displays the specified value when the left join matching data is null

Oracle table creation and management

Hoo research | coinwave production - nym: building the next generation privacy infrastructure

Green, green the reed. dew and frost gleam.

Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.

Locking relay ydb-100, 100V
随机推荐
Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
Experiment 8 T-SQL, stored procedure
K210 site helmet
酒旅板块复苏,亚朵继续上市梦,距离“新住宿经济第一股“还有多远?
Error msb8031: building an MFC project for a non Unicode character set is deprecated
A letter to 5000 fans!
Win11安装redis 数据库以及redis desktop manager的下载
[daily record] - bug encountered in BigDecimal division operation
【学习笔记】简单dp
Mustache syntax
二十多年来第一次!CVPR最佳学生论文授予中国高校学生!
[learning notes] structure
MATLAB 最远点采样(FPS改进版)
Web compatibility testing of software testing
Vnctf 2022 cm CM1 re reproduction
Koa koa-combine-routers 分路由管理
分割链表[先取next再斩断链表防止断链]
mustache语法
Detailed analysis of operators i++ and ++i in JS, i++ and ++i
DLS-20型双位置继电器 220VDC