Leecode 785. 判断二分图 DFS染色/BFS

news/2024/6/29 12:02:51 标签: 深度优先, 宽度优先, leetcode, 算法, 数据结构

原题链接:Leecode 785. 判断二分图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

BFS

class Solution {
public:
    bool isBipartite(vector<vector<int>>& graph) {
        int n=graph.size();
        vector<int> color(n,0);
        queue<int> q;
        for(int i=0;i<n;i++)
        {
            if(!color[i])
            {
                color[i]=1;
                q.push(i);
            }
            while(!q.empty())
            {
                int cur=q.front();
                q.pop();
                for(auto x: graph[cur])
                {
                    if(color[x]==color[cur]) return false;
                    else if(color[x]==0)
                    {
                        q.push(x);
                        color[x]=-color[cur];
                    }
                }
            }
        }
        return true;
    }
};

DFS

class Solution {
public:
    vector<int> color;
    bool isBipartite(vector<vector<int>>& graph) {
        int n=graph.size();
        color.resize(n);
        for(int i=0;i<n;i++)
        {
            if(!color[i] && !dfs(graph,i,1)) return false;
        }
        return true;
    }
    bool dfs(vector<vector<int>>& graph,int i,int col)
    {
        color[i]=col;
        for(auto x: graph[i])
        {
            if(color[x]==color[i]) return false;
            else if(!color[x] && !dfs(graph,x,-col)) return false;
        }
        return true;
    }
};

http://www.niftyadmin.cn/n/1264822.html

相关文章

统计学习基础--第一、二章 导论

一、data的理解 我们把data分为训练集和测试集&#xff0c;其中训练集用于建立模型&#xff0c;通常要占data的80%&#xff1b;而测试集则是用于预测分析&#xff0c;观察拟合出的模型的效果。 二、数据预处理 1、处理数据文件格式&#xff1b; 2、观察数据是否有缺失值或异…

Leecode 886. 可能的二分法 DFS染色/BFS

原题链接&#xff1a;Leecode 886. 可能的二分法 和这道题大同小异&#xff1a;Leecode 785. 判断二分图 DFS染色/BFS BFS class Solution { public:bool possibleBipartition(int n, vector<vector<int>>& dislikes) {vector<int> color(n1,0);queue&…

统计学习基础--第三章 线性回归

目录 一、简单线性回归 1、表达式 2、估计系数 &#xff08;1&#xff09;方法&#xff1a;最小二乘法 &#xff08;2&#xff09;实质&#xff1a;​ &#xff08;3&#xff09;结果 &#xff08;4&#xff09;评估系数估计的准确性 3、评估模型的准确性 二、多元线性回…

统计学习基础--第四章 分类

目录 一、逻辑斯谛回归&#xff08;logistic&#xff09; 1、Logistic模型 &#xff08;1&#xff09;概率 &#xff08;2&#xff09;逻辑斯谛函数 &#xff08;3&#xff09;注意 2、估计回归系数 &#xff08;1&#xff09;方法&#xff1a;极大似然估计 &#xff0…

Leecode 207. 课程表 拓扑排序/DFS

原题链接&#xff1a;Leecode 207. 课程表 和这道题差不多&#xff1a;Leecode 802. 找到最终的安全状态 拓扑排序/DFS&#xff0c;这里的安全状态就是本题的无环的意思。 拓扑排序 class Solution { public:bool canFinish(int numCourses, vector<vector<int>&g…

Leecode 210. 课程表 II 拓扑排序

原题链接&#xff1a;Leecode 210. 课程表 II class Solution { public:vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {vector<int> degree(numCourses);vector<vector<int>> g(numCourses);vector…

统计学习基础——第五章 重抽样

目录 一、重抽样 1、概念 2、用途 3、缺点 4、方法 二、交叉验证法&#xff08;CV&#xff09; 1、验证集方法 &#xff08;1&#xff09;原理 &#xff08;2&#xff09;评价指标&#xff1a;均方误差 &#xff08;3&#xff09;缺陷 2、留一交叉验证法&#xff08…

统计学习基础——第六章 线性模型选择与正则化

目录 一、子集选择 1、原理 2、最优子集选择 &#xff08;1&#xff09;原理 &#xff08;2&#xff09;不足&#xff1a;计算效率不高。 &#xff08;3&#xff09;改进&#xff1a;分支定界法。 3、逐步选择 &#xff08;1&#xff09;作用 &#xff08;2&#xff0…