[Bipartite graph] determines whether it is a bipartite graph.

Source: Internet
Author: User

The question of a game used to be a Hungarian algorithm to process the problem of the Bipartite Graph (that is, the problem is known as a bipartite graph). This is to judge the bipartite graph. Pay attention to the choice of the processing method.

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;}

[Bipartite graph] determines whether it is a bipartite graph.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.