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;
    }
  • 相关阅读:
    强化学习——Q-learning算法
    嵌入(embedding)层的理解
    福昕PDF电子文档处理套装软件中文企业版9.01
    奇异值分解(SVD)原理与在降维中的应用
    C++ Primer 笔记——迭代器
    Windows Internals 笔记——线程局部存储区
    C/C++中二进制与文本方式打开文件的区别
    C++ Primer 笔记——固有的不可移植的特性
    C++ Primer 笔记——union
    C++ Primer 笔记——嵌套类 局部类
  • 原文地址:https://www.cnblogs.com/hyfer/p/5998270.html
Copyright © 2011-2022 走看看