hdu4751 二分圖的判斷,hdu4751二分圖

來源:互聯網
上載者:User

hdu4751 二分圖的判斷,hdu4751二分圖

http://acm.hdu.edu.cn/showproblem.php?pid=4751

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
/**hdu 4751 染色法二分圖的判斷題目大意:給定一個些人物關係問是否能把所有的人分成兩組,使得每一組的人都相互認識解題思路:任意兩個人如果不是相互認識就建邊,然後採用染色的方法判斷是否為二分圖即可*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int maxn=105;struct note{    int v,next;}edge[maxn*maxn];int head[maxn],ip;int color[maxn];void init(){    memset(head,-1,sizeof(head));    ip=0;}void addedge(int u,int v){    edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}bool dfs(int u,int col){    color[u]=col;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(color[v]!=-1)        {            if(color[v]==col)return false;            continue;        }        if(!dfs(v,!col))return false;    }    return true;}int n;int g[105][105];int main(){    while(~scanf("%d",&n))    {        memset(g,0,sizeof(g));        for(int i=1;i<=n;i++)        {            int x;            while(~scanf("%d",&x))            {                if(x==0) break;                g[i][x]=1;            }        }        init();        for(int i=1;i<=n;i++)        {            for(int j=i+1;j<=n;j++)            {                if(g[i][j]==0||g[j][i]==0)                {                    addedge(i,j);                    addedge(j,i);                }            }        }        memset(color,-1,sizeof(color));        bool flag=true;        for(int i=1;i<=n;i++)        {            if(color[i]==-1&&dfs(i,0)==false)            {                flag=false;                break;            }        }        if(flag)            printf("YES\n");        else            printf("NO\n");    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.