码迷,mamicode.com
首页 > 其他好文 > 详细

hdu The Accomodation of Students(二分匹配)

时间:2015-05-15 09:10:40      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:hdu the accomodation   二分匹配   

The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3136    Accepted Submission(s): 1466


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
 


题意:给你一些n个点,m条双向边,判断图是不是二分图,是的话求出最大匹配。


#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#define N 222

using namespace std;

int n,m;
vector<int>G[N];
int linker[N];
bool used[N];
int c[N];
int maze[N][N];

bool dfs(int u) {
    int v;
    for(v=0; v<G[u].size(); v++) {
        int i=G[u][v];
        if(!used[i]) {
            used[i]=true;
            if(linker[i]==-1||dfs(linker[i])) {
                linker[i]=u;
                return true;
            }
        }
    }
    return false;
}

int hungary() {
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=1; u<=n; u++) {
        memset(used,0,sizeof(used));
        if(dfs(u))  res++;
    }
    return res;
}

bool dfs_c(int pos,int col) {
    for(int i=0; i<G[pos].size(); i++) {
        int v=G[pos][i];
        if(c[v]==c[pos])
            return false;
        if(!c[v]) {
            c[v]=-col;
            if(!dfs_c(v,-col))return false;
        }
    }
    return true;
}
///染色法判断二分图
bool color() {
    memset(c,0,sizeof c);
    int col=1;
    for(int i=1; i<=n; i++) {
        if(c[i]!=0)continue;
        c[i]=col;
        if(!dfs_c(i,col)) {
            return false;
        }
    }
    return true;
}
int main() {
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)) {
        for(int i=0; i<N; i++)G[i].clear();
        int x,y;
        for(int i=0; i<m; i++) {
            scanf("%d%d",&x,&y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        if(!color()) {
            printf("No\n");
            continue;
        }
        printf("%d\n", hungary()/2);
    }
    return 0;
}


hdu The Accomodation of Students(二分匹配)

标签:hdu the accomodation   二分匹配   

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/45727947

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!