【二分图】 判断是否是二分图

一次比赛的题,以前都是匈牙利算法处理二分图问题(即已知是二分图),这次是判断二分图,注意处理方式的选择。

Mediacy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 103    Accepted Submission(s): 18

Problem Description

In yrleep`s class,there are only two kinds of relationship between the classmates which can not be  passed,namely incompatible and live in harmony.So yrleep wants to put them into two parts to  make both of them get along well with each other in each part . Can his ideas be implemented?

Input

The input consists of multiple test cases.the first line input two integers n,m,n indicates the number of people

The next m lines,each line has two integers a,b,indicates a and b are incompatible.

Output

If his idea can come true ,output yes.if not,output no.

Sample Input

5 3

1 2

3 4

4 5

Sample Output

yes

#include <cstdio>
#include <iostream>
#include <memory.h>
#include <vector>
using namespace std;
const int maxn = 10000 + 10;
vector G[maxn];
int n, m;
int color[maxn];
bool dfs(int v, int c)
{

    color[v] = c;
    for(int i=0; i< G[v].size(); i++) {
        //如果相邻的顶点同色,则返回false
        if(color[G[v][i]] == c) return false;
        //如果相邻的顶点还没有被染色,则染成-c
        if(color[G[v][i]] == 0 && !dfs(G[v][i], -c)) return false;
    }
    return true;
}

void solve()
{
    for(int i=0; i
        if(color[i] == 0) {
            if(!dfs(i,1)) {
                printf("no\n");
                return ;
            }
        }
    }
    printf("yes\n");
}

int main()
{
//    freopen("in.txt","r",stdin);
    int i, x, y;
    while(~scanf("%d%d",&n,&m)) {
        memset(color, 0, sizeof (color) );
        for(i=1; i<=n; ++i) G[i].clear();
        for(i=1; i<=m; ++i) {
            scanf("%d%d",&x,&y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        solve();
    }
    return 0;
}

  

时间: 2024-10-24 21:07:41

【二分图】 判断是否是二分图的相关文章

hdu 2444 二分图判断与最大匹配

题意:有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出No. 首先判断是否为二分图,然后判断最大匹配 Sample Input 4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6Sample Output No 3 1 #include<iostream> 2 #include<string.h> 3 #i

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 <

二分图判断(交叉染色)

二分图又称二部图. 二分图是无向图. 设G=(V,E)是一个无向图.如顶点集V可分割为两个互不相交的子集,并且图中每条边依附的两个顶点都分属两个不同的子集. 例如这就是一个二分图. 大概就是把顶点分成两堆,每堆内部没有边. 无向图G为二分图的充分必要条件是,G至少有两个顶点, 且其所有回路的长度均为偶数. 最大独立点集:在二分图中,求最少的点集,使得任意两个点之间没有直接边连接. 最小点覆盖:在二分图中,求最少的点集,使得每一条边至少都有端点在这个点集中. 二分图判断:二分图染色. 给一个无向图

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

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

hdu 2444 The Accomodation of Students 【二分图判断+求最大匹配】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=2444 题意:判断所有人是否分为两个集合,每个集合里的人互不相识. 思路:先判断是否为二分图,是的话求最大匹配,否则输出"No". 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #i

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,

hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2444 Description There are a group of students. Some of them may know