当前位置:网站首页>leetcode785. 判断二分图(中等)
leetcode785. 判断二分图(中等)
2022-06-11 16:01:00 【重you小垃】



题意转换->将所有节点分成两组,每条边两个结点分别来自两组。
思路:dfs染色(两种)。 每条边两个结点染不同的颜色(1 / -1),若两端结点颜色一致 则 return false
注意:可能不是连通图->多次dfs
class Solution {
public:
int color[105] = {
0};
int dfs(vector<vector<int>>& graph, int colr, int m) {
color[m] = colr;
int next = (colr == 1 ? -1 : 1);
for (auto& a : graph[m]) {
if (color[a] == color[m]) return false;
if (abs(color[a] - color[m]) == 2) continue;
if (!dfs(graph, next, a)) return false;
}
return true;
}
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
for (int i = 0; i < n; ++i) {
if (!color[i] && !dfs(graph, 1, i)) return false;
}
return true;
}
};
代码二的写法:bfs,队头和即将入队的两个元素颜色不一致即可
边栏推荐
- [daily question series]: how to test web forms?
- How to manage concurrent write operations? Get you started quickly
- postgresql创建表
- This "invisible" robot may be your future colleague
- Aaai2022 latest "time series data processing" report, 127 pages of PPT describing time series data processing and medical application progress
- CLP information - financial standardization "14th five year plan" highlights data security
- AI tool for cutting-edge technology exploration: analog detection
- The difference between go language array and slice
- [从零开始学习FPGA编程-17]:快速入门篇 - 操作步骤2-5- VerilogHDL硬件描述语言符号系统与程序框架(软件程序员和硬件工程师都能看懂)
- laravel 监听模式
猜你喜欢

Go language - array

It's really not human to let the express delivery arrive before the refund

laravel 监听模式

Opengauss version 3.0.0 was officially released, and immediately experience the first lightweight version in the community

What if the win10 security center cannot be closed

用户界面之工具栏详解-AutoRunner自动化测试工具

什么是泛型?为什么要使用泛型?泛型怎么用?那包装类呢?

Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library

项目经理如何击退被工作汇报支配的恐惧感?

Customized thread communication (lock) of JUC
随机推荐
Selenium-- display waiting (medium) -- detailed explanation
jdbc调试错误,求指导
Aaai2022 latest "time series data processing" report, 127 pages of PPT describing time series data processing and medical application progress
[Yugong series] June 2022 Net architecture class 079 cluster principle of distributed middleware schedulemaster
AI tool for cutting-edge technology exploration: analog detection
Collect | thoroughly understand the meaning and calculation of receptive field
面试高频算法题---最长回文子串
[Yugong series] June 2022 Net architecture class 076- execution principle of distributed middleware schedulemaster
Golang map basic operations
Maui introductory tutorial series (1. framework introduction)
[从零开始学习FPGA编程-17]:快速入门篇 - 操作步骤2-5- VerilogHDL硬件描述语言符号系统与程序框架(软件程序员和硬件工程师都能看懂)
项目经理如何击退被工作汇报支配的恐惧感?
AI function of cutting-edge technology exploration: slow SQL discovery
CLP information - No. 1 central document on Strengthening Rural Revitalization financial services
Discussion on opengauss parallel decoding
Database resource load management (Part 2)
泰雷兹云安全报告显示,云端数据泄露和复杂程度呈上升趋势
Data enhancement
书籍《阅读的方法》读后感
What if you can't access the desktop after the computer is turned on