HDU 2444 The Accomodation of Students 判断二分图 + 最大匹配

来源:互联网 发布:js定义字符串数组 编辑:程序博客网 时间:2024/06/05 17:05

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.

Outpub:

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
Output:

No

3

解法 :  用DFS或BFS进行染色,如果相邻的两个节点颜色相同,则返回假,并直接输出NO。    算最大匹配时直接套模板就OK 了。

#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>#include <iostream>#include <cmath>#include <algorithm>#include <numeric>#include <utility>#include <cstring>#include <vector>#include <queue>#include <stack>#include <map>#include <string>#include <memory.h>using namespace std;#define inf 0x3f3f3f3f#define MAXN 305#define clr(x,k) memset((x),(k),sizeof(x))#define cpy(x,k) memcpy((x),(k),sizeof(x))#define Base 10000typedef vector<int> vi;typedef vector<string> vs;#define sz(a) int((a).size())#define pb push_back#define all(c) (c).begin(),(c).end()#define rep(i,n) for(int i = 0;i < n;++i)#define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))int n,m;#define White 1#define Black 0#define None -1vi Map[MAXN];int Color[MAXN];int flag;int xM[MAXN],yM[MAXN];int chk[MAXN];void init(){    for(int i = 0 ;i <= n;i++) Map[i].clear();    for(int i = 0,a,b;i < m;i++){        scanf("%d %d",&a,&b);        Map[a].push_back(b);        Map[b].push_back(a);    }    clr(Color,None);}void dfs(int cur,int color){    int oColor = (color==White?Black:White);    Color[cur] = color;    foreach(it,Map[cur]){        if(None == Color[*it]){            dfs(*it,oColor);        }else{            if(Color[*it] == Color[cur])                flag = true;        }    }}bool dfsTwoMap(){    flag = false;    for(int i = 1;i <= n && !flag;i++){        if(None == Color[i]){            dfs(i,Black);        }    }    return flag;}bool SearchPath(int u){    foreach(it,Map[u]){        if(!chk[*it]){            chk[*it] = 1;            if(yM[*it]==-1 || SearchPath(yM[*it])){                yM[*it]=u;xM[u]=*it;                return true;            }        }    }    return false;  //一开始Wa这里了。。真的找了好久的错。。。。。}int MaxMatch(){    int u,ret = 0;    clr(xM,-1);clr(yM,-1);    for(u = 1;u <= n;u++){        if(xM[u]==-1){            clr(chk,0);            if(SearchPath(u))                ret++;        }    }    return ret;}int main(){    freopen("2444.in","r",stdin);    while(scanf("%d %d",&n,&m) != EOF){        init();        if(dfsTwoMap()){   // 不是二分图的话            printf("No\n");            continue;        }        else printf("%d\n",MaxMatch()/2);    }    return 0;}