• Divide Groups 二分图的判定


    Divide Groups

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


    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
    3 3 0 1 0 1 2 0
     
    Sample Output
    YES
     
    #include <cstdio>
    #include <cmath>
    #include <vector>
    #include <cstring>
    #include <iostream>
    using namespace std;
    const int Max=111;
    int G[Max][Max];
    int path[Max][Max],book[Max],color[Max],book1[Max];
    int n;
    bool dfs(int u,int co)
    {
    //  cout<<u<<" "<<color[u]<<" "<<co<<endl;
      if(color[u]!=-1&&color[u]!=co) return true;
      if(color[u]==co) return false;
      color[u]=!co;
      for(int i=1;i<=n;i++)
      {
          if(u==i) continue;
          if(path[u][i]||path[i][u])
          if(book[i]) if(!dfs(i,!co)) return false;
      }
      return true;
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            memset(book,0,sizeof(book));
            memset(book1,-1,sizeof(book1));
            memset(G,0,sizeof(G));
            memset(path,0,sizeof(path));
            memset(color,-1,sizeof(color));
            int tmp;
            for(int i=1;i<=n;i++)
            {
                while(scanf("%d",&tmp))
                {
                    if(tmp==0) break;
                    G[i][tmp]=1;
                }
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                     if(G[i][j]!=1||G[j][i]!=1)
                     {
                         if(i==j) continue;
                         book[i]=book[j]=1;
                         path[i][j]=1;
                     }
                }
            }
            int flag=0;
            for(int i=1;i<=n;i++)
            {
              if(book[i])
               if(!dfs(i,1)) {flag=1;break;}
              memset(color,-1,sizeof(color));
            }
            if(flag) printf("NO
    ");
            else     printf("YES
    ");
        }
        return 0;
    }
     
  • 相关阅读:
    PhpPresentation ppt 导出PPT
    Springcloudalibaba学习实践(1) nacos环境搭建&Eureka Server搭建
    Springcloudalibaba学习实践(2) nacos&Eureka Server服务注册实践
    xna 3.1 to xna 4.0 conversion cheat sheet.(转)
    二分查找的越界以及边界值初始化问题
    决策树知识梳理
    论文分类及写作基础
    Cypher 入门笔记
    python字符串拼接
    Spring、Hibernate 事务流程分析Session生命周期
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/5299477.html
Copyright © 2020-2023  润新知