785. Is Graph Bipartite?( 判断是否为二分图)

Given an undirected graph, return true if and only if it is bipartite.

Recall that a graph is bipartite if we can split it‘s set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.

The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and jexists.  Each node is an integer between 0 and graph.length - 1.  There are no self edges or parallel edges: graph[i]does not contain i, and it doesn‘t contain any element twice.

Example 1:
Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation:
The graph looks like this:
0----1
|    |
|    |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.
Example 2:
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation:
The graph looks like this:
0----1
| \  |
|  \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.

说明:1、输入数组中的graph[i],表示顶点i所有相邻的顶点,比如对于例子1来说,顶点0和顶点1,3相连,顶点1和顶点0,2相连,顶点2和结点1,3相连,顶点3和顶点0,2相连。

方法一:bfs
class Solution {
    public static boolean isBipartite(int[][] graph) {
        if(graph.length<=1)
            return true;
        int color[]=new int[graph.length];
        for(int i=0;i<graph.length;i++){
            if(color[i]==0){
                color[i]=1;
                Queue<Integer> queue=new LinkedList<Integer>();
                queue.offer(i);
                while(queue.size()>0){
                    int t1=queue.poll();
                    for(int j=0;j<graph[t1].length;j++){
                        int t2=graph[t1][j];
                        if(color[t2]==0){
                            color[t2]=color[t1]==1?2:1;
                            queue.offer(t2);
                        }
                        else{
                            if(color[t1]==color[t2])
                                return false;
                        }
                    }
                }
            }
        }
        return true;
    }
}

方法二:dfs

class Solution {
    private boolean flag=true;
    public boolean isBipartite(int[][] graph) {
        if(graph.length<=1)  return true;
        int [] color=new int[graph.length];
        for(int i=0;i<graph.length;i++){
            if(color[i]==0){
                color[i]=1;
                dfs(i,graph,color);
                if(!flag){
                    return flag;
                }
            }
        }
        return true;
    }
    void dfs(int pos,int[][] graph,int[] color){
        for(int j=0;j<graph[pos].length;j++){
            int k=graph[pos][j];
            if(color[k]==0){
                color[k]=color[pos]==1?2:1;
                dfs(k,graph,color);
            }else{
                if(color[k]==color[pos]){
                    flag=false;
                    return;
                }
            }
        }
    }
}

原文地址:https://www.cnblogs.com/shaer/p/10961569.html

时间: 2024-11-06 03:51:52

785. Is Graph Bipartite?( 判断是否为二分图)的相关文章

[leetcode]785. Is Graph Bipartite? [bai&#39;pɑrtait] 判断二分图

Given an undirected graph, return true if and only if it is bipartite. Example 1: Input: [[1,3], [0,2], [1,3], [0,2]] Output: true Explanation: The graph looks like this: 0----1 | | | | 3----2 We can divide the vertices into two groups: {0, 2} and {1

785.Is Graph Bipartite?

Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.

LeetCode - 785. Is Graph Bipartite?

判断一个给定图是不是二分图. 题目提供一个用二维数组存储的邻接表. 常规的二分图判断,点着色. 注意要将图存入类中,因为dfs需要访问图中的点. 1 class Solution { 2 private int[][] graph; 3 private boolean[] visited; 4 private int[] colors; 5 6 public boolean isBipartite(int[][] graph) { 7 this.graph = graph; 8 int V =

BFS判断是否是二分图Bipartite算法

二分图bipartite 使用BFS广度优先判断一个图是否是二分图.基本图操作. 参考 http://www.geeksforgeeks.org/bipartite-graph/ #pragma once #include <stdio.h> #include <iostream> #include <queue> using namespace std; class CheckwhetheragivengraphisBipartiteornot { const sta

HDU 2444 The Accomodation of Students (判断是否是二分图,然后求最大匹配)

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B

HDU 2444 The Accomodation of Students(判断是否是二分图)

题目链接 题意:n个学生,m对关系,每一对互相认识的能住一个房间.问否把这些学生分成两组,要求每组的学生都互不认识.求最多需要多少个房间. 是否能分成两组?也就是说判断是不是二分图,判断二分图的办法,用染色法 把初始点染成黑色,然后与之相连的染成白色,重复,使路径黑白相间, 如果当前点的颜色和与他相连点的颜色相同时,则说明这个图不是二分图 求最多需要多少个房间?也就是求最大匹配数. #include <iostream> #include <cstdio> #include <

hdu2444 二分图的匹配,先判断是否为二分图

http://acm.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C

染色法判断是否是二分图 hdu2444

用染色法判断二分图是这样进行的,随便选择一个点, 1.把它染成黑色,然后将它相邻的点染成白色,然后入队列 2.出队列,与这个点相邻的点染成相反的颜色 根据二分图的特性,相同集合内的点颜色是相同的,即 但是如果这个图不是二分图,那么就会这样 把与1相邻的点2,3染成白色,然后入队列,然后2出队列,要把与2相邻的点2,3染成黑色,但是都染过了,所以不用染色 但是3的颜色应该与2相反(如果是二分图的话),可是没有相反,所以就不是二分图 1 #include <stdio.h> 2 #include

hdu2444The Accomodation of Students (最大匹配+判断是否为二分图)

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2244 Accepted Submission(s): 1056 Problem Description There are a group of students. Some of them may know each other,