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

73 篇文章 21 订阅
12 篇文章 0 订阅

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 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 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 10000

typedef 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 -1
vi 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值