Consolexin's blog Consolexin's blog
首页
  • 算法基础

    • 图论
    • 字符串
    • 动态规划
    • 二分
    • 滑动窗口
    • 排序
  • Project

    • CppServer
  • 相关书籍

    • 现代C++编程
  • 书籍

    • SQL必知必会
    • MySQL必知必会
分类
标签
归档
GitHub (opens new window)

Consolexinhun

小学生
首页
  • 算法基础

    • 图论
    • 字符串
    • 动态规划
    • 二分
    • 滑动窗口
    • 排序
  • Project

    • CppServer
  • 相关书籍

    • 现代C++编程
  • 书籍

    • SQL必知必会
    • MySQL必知必会
分类
标签
归档
GitHub (opens new window)
  • 图论

    • 单源最短路
    • 欧拉图
    • 拓扑排序
    • 并查集
    • 最小生成树
    • 二分图
      • 概念
      • 题目
        • LC 0785. 判断二分图
      • 参考链接
  • 字符串

  • 动态规划

  • 二分

  • 滑动窗口

  • 排序

  • 算法
  • 图论
consolexinhun
2021-10-21
目录

二分图

# 概念

二分图:所有边的两个顶点刚好分别在两个集合中,且这两个集合内部没有边连接

完全二分图:集合中任意一个点都与另一个集合中的所有点都连通

# 题目

# LC 0785. 判断二分图

https://leetcode-cn.com/problems/is-graph-bipartite/

class Solution {
public:
    bool isBipartite(vector<vector<int>>& graph) {
        // graph 是一个邻接矩阵
        // 0 没染色,1 白色,-1 黑色
        int n = graph.size();
        vector<int> c(n, 0);  // 点的颜色
        for(int i = 0; i < (int)graph.size(); i++){
            if(c[i] == 0){
                if(!dfs(i, 1, graph, c)) return false;
            }
        }
        return true;
    }

    bool dfs(int i, int pre, vector<vector<int>>&graph, vector<int>&c){
        // i 表示现在的点,pre 表示上一个颜色
        if(c[i]) return c[i] == -pre;

        c[i] = -pre;
        for(auto t: graph[i]){
            if(!dfs(t, -pre, graph, c)) return false;
        }
        return true;
    }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 参考链接

图论基础 (opens new window)

编辑 (opens new window)
#图论
上次更新: 2025/05/21, 06:42:57
最小生成树
KMP算法

← 最小生成树 KMP算法→

最近更新
01
6-其他操作
05-20
02
4-联结
05-20
03
7-管理
05-20
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Consolexinhun | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×