zoukankan      html  css  js  c++  java
  • HDU 2444 The Accomodation of Students (二部图+染色)

    The Accomodation of Students

    Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 1   Accepted Submission(s) : 1

    Font: Times New Roman | Verdana | Georgia

    Font Size:  

    Problem Description

    There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.
    Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.
    Calculate the maximum number of pairs that can be arranged into these double rooms.

    Input

    For each data set: The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.
    Proceed to the end of file.

    Output

    If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.

    Sample Input

    4 4
    1 2
    1 3
    1 4
    2 3
    6 5
    1 2
    1 3
    1 4
    2 5
    3 6

    Sample Output

    No
    3

    Source

    2008 Asia Harbin Regional Contest Online
     
     题目大意:输入有n个人和m对关系,要求把他们分成两组,每组的人中不能有互相认识的,如果不可以分成这样的两组,输出NO,否则输出这两组中最多人的人数。 这个也是二分最大匹配,判断能否分成两组,这个用染色法,黑白染色,如果有矛盾则不能分成两组,否则可以二分匹配,每对关系就是边的关系,然后一个二分匹配算法求最大匹配就是答案了! 
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    const int N=220;
    
    struct Edge{
        int to,nxt;
    }edge[N*N];
    
    int n,m,cnt,head[N];
    int linker[N],vis[N],color[N],vis1[N];
    
    void addedge(int cu,int cv){
        edge[cnt].to=cv;    edge[cnt].nxt=head[cu];     head[cu]=cnt++;
        edge[cnt].to=cu;    edge[cnt].nxt=head[cv];     head[cv]=cnt++;
    }
    
    int AddColor(){     //进行染色,判断是否能按照题目意思分成两组(即每组之间没有人互相认识),如果可以的话,进行二分匹配,否则输出No
        queue<int> q;
        while(!q.empty())
            q.pop();
        vis1[1]=1;
        q.push(1);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            for(int i=head[u];i!=-1;i=edge[i].nxt){
                int v=edge[i].to;
                if(!vis1[v]){
                    vis1[v]=1;
                    color[v]=!color[u];
                    q.push(v);
                }else if(color[v]==color[u])
                    return 0;
            }
        }
        return 1;
    }
    
    int DFS(int u){
        int v;
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            v=edge[i].to;
            if(!vis[v]){
                vis[v]=1;
                if(linker[v]==-1 || DFS(linker[v])){
                    linker[v]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    
    int Hungary(){
        int u,ans=0;
        memset(linker,-1,sizeof(linker));
        for(u=1;u<=n;u++){
            memset(vis,0,sizeof(vis));
            if(DFS(u))
                ans++;
        }
        return ans;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d%d",&n,&m)){
            cnt=0;
            memset(head,-1,sizeof(head));
            int u,v;
            while(m--){
                scanf("%d%d",&u,&v);
                addedge(u,v);
            }
            memset(vis1,0,sizeof(vis1));
            if(AddColor()==0){
                printf("No\n");
                continue;
            }
            int ans=Hungary();
            printf("%d\n",ans/2);
        }
        return 0;
    }
     
  • 相关阅读:
    Smartform中表(table)的行间距设置
    ◆◆0Smartform中如何设置背景阴影色(Shading)
    ◆◆0如何在Smartforms中设置左右对齐
    如何在smartform中设置行间距
    ◆◆0如何在smartform中的table节点插入分页
    ◆◆0如何翻译smartform中的Text module
    如何在smartforms中插入复选框(checkbox)
    ◆◆0选择屏幕-SELECTION-SCREEN(一)
    科研呢喃-2
    遇到杠精,浪费时间
  • 原文地址:https://www.cnblogs.com/jackge/p/2962518.html
Copyright © 2011-2022 走看看