hdu 4751 染色法判断二分图

来源:互联网 发布:张良一生 知乎 编辑:程序博客网 时间:2024/05/21 09:09

Divide Groups

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2539    Accepted Submission(s): 906


Problem Description

  This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos.
  After carefully planning, Tom200 announced his activity plan, one that contains two characters:
  1. Whether the effect of the event are good or bad has nothing to do with the number of people join in.
  2. The more people joining in one activity know each other, the more interesting the activity will be. Therefore, the best state is that, everyone knows each other.
  The event appeals to a great number of alumnus, and Tom200 finds that they may not know each other or may just unilaterally recognize others. To improve the activities effects, Tom200 has to divide all those who signed up into groups to take part in the activity at different time. As we know, one's energy is limited, and Tom200 can hold activity twice. Tom200 already knows the relationship of each two person, but he cannot divide them because the number is too large.
  Now Tom200 turns to you for help. Given the information, can you tell if it is possible to complete the dividing mission to make the two activity in best state.
 

Input
  The input contains several test cases, terminated by EOF.
  Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event.
  N lines follow. The i-th line contains some integers which are the id
of students that the i-th student knows, terminated by 0. And the id starts from 1.
 

Output
  If divided successfully, please output "YES" in a line, else output "NO".
 

Sample Input
33 01 01 2 0
 

Sample Output
YES
 

Source


把无相互关系的两个人分到两个集合


初涉染色法:

两种方法

dfs 

bfs

dfs:

#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespace std;int n,x;vector<int >v[103];int m[103][103];int color[103];bool dfs(int x,int c){    color[x]=c;    int len=v[x].size();    for(int i=0; i<len; i++)    {        int tmp=v[x][i];        if(color[tmp]!=-1)        {            if(color[tmp]==c)                return false;            continue;        }        if(!dfs(tmp,!c))            return false;    }    return true;}bool solve(){    int i,j;    memset(color,-1,sizeof(color));    for(int i=1; i<=n; i++)    {        if(color[i]==-1&&dfs(i,0)==false)            return false;    }    return true;}int main(){    while(~scanf("%d",&n))    {        memset(m,0,sizeof(m));        for(int i=1; i<=n; i++)        {            v[i].clear();            while(scanf("%d",&x),x)            {                m[i][x]=1;            }        }        for(int i=1; i<=n; i++)        {            for(int j=i+1; j<=n; j++)            {                if(m[i][j]==0||m[j][i]==0)                {                    v[i].push_back(j);                    v[j].push_back(i);                }            }        }        if(solve())            printf("YES\n");        else printf("NO\n");    }}



bfs:

#include<stdio.h>#include<vector>#include<algorithm>#include<string.h>#include<queue>#include<string.h>using namespace std;int m[103][103];int color[103];queue<int>q;int n;bool bfs(int x){    int i,j,tmp;    while(!q.empty())q.pop();    q.push(x);    while(!q.empty())    {        tmp=q.front();        q.pop();        for(i = 1; i <= n; i++)        {            if(i==tmp||m[i][tmp]&&m[tmp][i])continue;            if(color[i]==-1)            {                color[i]=color[tmp]^1;                q.push(i);            }            else if(color[i]==color[tmp])return true;        }    }    return false;}int main(){    int i,j,u,v,x;    while(~scanf("%d",&n))    {        memset(m,0,sizeof(m));        memset(color,-1,sizeof(color));        for(int i=1;i<=n;i++)        {            while(scanf("%d",&x),x)                m[i][x]=1;        }        for(i=1;i<=n;i++)        {            if(color[i]==-1)            {                color[i]=0;                if(bfs(i))break;            }        }        if(i<=n)            printf("NO\n");        else printf("YES\n");    }}