• 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;
    }
  • 相关阅读:
    前端利用百度开发文档给的web服务接口实现对某个区域周边配套的检索
    libevent源码学习(13):事件主循环event_base_loop
    libevent源码学习(11):超时管理之min_heap
    libevent源码学习(10):min_heap数据结构解析
    libevent源码学习(8):event_signal_map解析
    libevent源码学习(9):事件event
    libevent源码学习(6):事件处理基础——event_base的创建
    libevent源码学习(5):TAILQ_QUEUE解析
    仿Neo4j里的知识图谱,利用d3+vue开发的一个网络拓扑图
    element表格内每一行删除提示el-popover的使用要点
  • 原文地址:https://www.cnblogs.com/hyfer/p/5998270.html
Copyright © 2020-2023  润新知