zoukankan      html  css  js  c++  java
  • hdu4751 Divide Groups

    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<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #define ll long long
    using namespace std;
    const int maxn = 105;
    int n,col[maxn];
    bool kw[maxn][maxn];
    vector<int> g[maxn];
    bool dfs(int x,int c){
        col[x] = c;
        int to;
        for(int i = 0;i < g[x].size();i++){
            to = g[x][i];
            if(col[to] == c) return false;
            if(!col[to] && !dfs(to,3-c)) return false;
        }
        return true;
    }
    void get_ans(){
        for(int i = 1;i <= n;i++){
            if(!col[i]){
                if(!dfs(i,1)){
                    printf("NO
    ");
                    return;
                }
            }
        }
        printf("YES
    ");
    }
    int main(){
        int to;
        while(scanf("%d",&n) == 1){
            for(int i = 1;i <= n;i++) g[i].clear();
            memset(col,0,sizeof(col));
            memset(kw,0,sizeof(kw));
            for(int i = 1;i <= n;i++){
                while(scanf("%d",&to) == 1&&to){
                    kw[i][to] = true;
                }
            }
            for(int i = 1;i <= n;i++){
                for(int j = i + 1;j <= n;j++){
                    if(!kw[i][j] || !kw[j][i]){
                        g[i].push_back(j);
                        g[j].push_back(i);
                    }
                }
            }
            get_ans();
        }
        return 0;
    }
  • 相关阅读:
    第二次结对编程作业
    团队项目-选题报告
    第一次结对编程作业
    第一次个人编程作业
    软件工程实践作业(1)
    C博客作业01--分支丶顺序结构
    C博客作业00--我的第一篇博客
    C博客作业
    C博客作业01--分支、顺序结构
    一条简单select经过了多少路程
  • 原文地址:https://www.cnblogs.com/hyfer/p/5998270.html
Copyright © 2011-2022 走看看