当前位置:网站首页>LeetCode 785:判断二分图
LeetCode 785:判断二分图
2022-06-27 01:49:00 【斯沃福德】
思路:
判定二分图的算法很简单,就是用代码解决「双色问题」。
说白了就是遍历一遍图,一边遍历一边染色,看看能不能用两种颜色给所有节点染色,且相邻节点的颜色都不相同。
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]; // 自动初始化为全false;
//遍历每个顶点
for(int i=0;i<n;i++){
dfs(graph,i);
}
return a;
}
void dfs(int[][] graph,int s){
//终止条件:不是二分数则无需再递归
if(!a){
return;
}
marked[s]=true;
//遍历邻接表
for(int k:graph[s]){
if(!marked[k]){
//节点未访问过
//上色 !
color[k]=!color[s]; // s 和k相邻! 所以上不同的色!
dfs(graph,k);
}//节点已被访问过
if(color[k]==color[s]){
// 相邻的s和v颜色相同则不是二分图
a=false;
}
}
}
}
边栏推荐
- 1.44 inch TFT-LCD display screen mold taking tutorial
- Detailed explanation of ThreadLocal
- UVM in UVM_ config_ Setting and obtaining DB non-linear
- Oracle/PLSQL: Translate Function
- 为什么先划分训练集和测试集后归一化?
- 达梦数据库的卸载
- Oracle/PLSQL: Replace Function
- 执念斩长河暑期规划
- 福元医药上市在即:募资净额将达到16亿元,胡柏藩为实际控制人
- Simply learn the entry-level concepts of googlecolab
猜你喜欢

二叉树oj题目

你的case真的pass了吗?

Press key to control LED status reversal

图论知识及其应用初步调研

Installing the Damon database using the command line

Meituan: data management and pit avoidance strategy summarized after stepping on Thunder for several years

I earned 3W yuan a month from my sideline: the industry you despise really makes money!
![[graduation season] role conversion](/img/4e/aa763455da974d9576a31568fc6625.jpg)
[graduation season] role conversion

Systematic analysis of social networks using Networkx: Facebook network analysis case

Summary of config mechanism and methods in UVM (2)
随机推荐
Cookie, sessionstorage, localstorage differences
消费者追捧iPhone,在于它的性价比超越国产手机
Oracle/PLSQL: Substr Function
Daily question brushing record (V)
Summer planning for the long river
Memcached basics 14
Why pass SPIF_ Sendchange flag systemparametersinfo will hang?
Due to the invalidation of the prospectus of bori technology, CICC has stopped providing guidance to it and abandoned the listing on the Hong Kong stock exchange?
Arbre binaire OJ sujet
Oracle/PLSQL: Trim Function
简单学习GoogleColab的入门级概念
Pointer compression for JVM
Summary of config mechanism and methods in UVM (2)
Config in UVM_ How to use the DB mechanism
在 IDEA 里看个书很过分嘛!
Oracle/PLSQL: VSize Function
NOKOV动作捕捉系统使多场协同无人机自主建造成为可能
Oracle/PLSQL: Cast Function
Learn the most basic operation of discodiffusion
UVM in UVM_ config_ Setting and obtaining DB non-linear