The Accomodation of Students - HDU 2444 - 二分图匹配

140 篇文章 0 订阅
13 篇文章 0 订阅

链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=2444


题目:

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

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


题意:

  给你n条边m个点,让你先判断是不是二分图,如果不是输出No,如果是就求这个图的最大匹配。


思路:

  模板题。


实现:

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
#include <functional>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#define read read()
#define edl putchar('\n')
#define clr(a,b) memset(a,b,sizeof a)
inline int read{ int x=0;char c=getchar();while(c<'0' || c>'9')c=getchar();while(c>='0' && c<='9'){ x=x*10+c-'0';c=getchar(); }return x;}
inline void write(int x){ int y=10,len=1;while(y<=x)    {y*=10;len++;}while(len--){y/=10;putchar(x/y+48);x%=y;}}
using namespace std;
const int maxn = 207;
int n,m;

vector<int> edge[maxn];
void addedge (int u, int v) {
    edge[u].push_back(v);
    edge[v].push_back(u);
}

bool col[maxn];
bool color (int u) {
    for (auto v : edge[u]) {
        if (!col[v]) {
            col[v] = !col[u];
            if (!color(v)) return false;
        } else if (col[v] == col[u]) return false;
    }
    return true;
}

bool vis[maxn];
int link[maxn];
bool dfs(int u) {
    for(auto v = edge[u].begin() ; v!=edge[u].end() ; v++) {
        if(!vis[*v]) {
            vis[*v] = true;
            if(link[*v] == -1 || dfs(link[*v])) {
                link[*v] = u;
                link[u] = *v;
                return true;
            }
        }
    }
    return false;
}

int match() {
    int ans{};
    clr(link, -1);
    for (int i = 1; i <= n; i++)
        if (link[i] == -1) {
            clr(vis, 0);
            if (dfs(i)) ans++;
        }
    return ans;
}

void init () {
    clr(col,0);
    for(int i=1 ; i<=n ; i++) edge[i].clear();
}

int main() {
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    while(cin >> n >> m) {
        if(n == 1) {
            puts("No");
            continue;
        }
        init();
        for(int i=0, u, v ; i<m ; i++) {
            u = read, v = read;
            addedge(u,v);
        }
        col[1] = true;
        if(!color(1)) {
            puts("No");
            continue;
        }
        write(match()),edl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值