当前位置:网站首页>Leetcode 785: judgment bipartite graph
Leetcode 785: judgment bipartite graph
2022-06-27 02:01:00 【Swarford】
Ideas :
The algorithm for determining bipartite graph is very simple , Is to use code to solve 「 Two color problem 」.
To put it bluntly, it is to traverse the graph , Dyeing while traversing , See if you can dye all nodes with two colors , And the colors of adjacent nodes are different .
class Solution {
boolean a=true;
boolean[] marked;
boolean[] color;
public boolean isBipartite(int[][] graph) {
int n=graph.length;
marked=new boolean[n];
color=new boolean[n]; // Automatically initialize to full false;
// Traverse every vertex
for(int i=0;i<n;i++){
dfs(graph,i);
}
return a;
}
void dfs(int[][] graph,int s){
// Termination conditions : If it is not a two fraction, there is no need to recurse
if(!a){
return;
}
marked[s]=true;
// Traverse adjacency table
for(int k:graph[s]){
if(!marked[k]){
// Node not accessed
// Color !
color[k]=!color[s]; // s and k adjacent ! So different colors !
dfs(graph,k);
}// Node has been accessed
if(color[k]==color[s]){
// The adjacent s and v The same color is not a bipartite graph
a=false;
}
}
}
}
边栏推荐
猜你喜欢

Hot discussion: what are you doing for a meaningless job with a monthly salary of 18000?

Learn the most basic operation of discodiffusion

在连接数据库的时候遇到了点问题,请问怎么解决呀?

markdown表格(合并)

lottie. JS creative switch button animal head

宁愿去996也不要待业在家啦!24岁,失业7个月,比上班更惨的,是没班可上

二叉树oj题目

canvas粒子篇之鼠标跟随js特效
![[graduation season] role conversion](/img/4e/aa763455da974d9576a31568fc6625.jpg)
[graduation season] role conversion

递归是会更秀strtok
随机推荐
SQLite reader plug-in tests SQLite syntax
UVM in UVM_ report_ Enabled usage
Shell脚本系列篇(1) 入门
Oracle/PLSQL: NumToDSInterval Function
Oracle/PLSQL: Translate Function
three. JS domino JS special effect
memcached基础11
Summary of config mechanism and methods in UVM (2)
Why divide the training set and the test set before normalization?
Don't be brainwashed. This is the truth about the wages of 90% of Chinese people
Oracle/PLSQL: NumToDSInterval Function
Reporting Classes中uvm_report_server的get_severity_count和get_server用法
Oracle/PLSQL: Upper Function
d的appendTo包装
memcached基础14
Memcached foundation 9
Oracle/PLSQL: Rtrim Function
WiFi-IoT 鸿蒙开发套件样例开发
Oracle/PLSQL: Substr Function
cookie,sessionstorage,localstorage区别