hdu The Accomodation of Students(二分匹配)

来源:互联网 发布:jquery源码 编辑:程序博客网 时间:2024/05/29 16:27

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 41 21 31 42 36 51 21 31 42 53 6
 

Sample Output
No3
 

Source
2008 Asia Harbin Regional Contest Online
 


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


#include<cstring>#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>#include<vector>#define N 222using 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;}


0 0