poj 3041 Aeteroids (二分图最大匹配)

题解: 本题中的所有顶点可以分成水平方向和竖直方向的攻击选择两类,每颗小行星所对应的边都分别与一个水平方向和一个竖直方向的顶点相连,所以是一个二分图,本题正好是求得二分图的最大匹配。在图论中,图中两两不含公共顶点的边的集合M称为匹配,儿元素最多的M称为最大匹配。

/***********************************************
 * Author: fisty
 * Created Time: 2015-08-16 21:23:21
 * File Name   : poj3041.cpp
 *********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define lson l, m, k<<1
#define rson m+1, r, k<<1|1
#define MAX_N 11000
int N, K;
int R[MAX_N], C[MAX_N];
int V;
vector<int> G[MAX_N];
int match[MAX_N];
bool used[MAX_N];

void add_edge(int u, int v){
    G[u].push_back(v);
    G[v].push_back(u);
}
bool dfs(int v){
    used[v] = true;
    for(int i = 0;i < G[v].size(); i++){
        int u = G[v][i], w = match[u];
        if(w < 0 || !used[w] && dfs(w)){
            match[v] = u;
            match[u] = v;
            return true;
        }
    }
    return false;
}
int bipartite_matching(){
    int res = 0;
    memset(match, -1, sizeof(match));
    for(int v = 0;v < V; v++){
        if(match[v] < 0){
            memset(used, 0, sizeof(used));
            if(dfs(v)){
                res++;
            }
        }
    }
    return res;
}
void solve(){
    V = N * 2;
    for(int i = 0;i < K; i++){
        add_edge(R[i]-1, N + C[i]-1);
    }
    printf("%d\n", bipartite_matching());
}
int main() {
    //freopen("in.cpp", "r", stdin);
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    while(~scanf("%d%d", &N, &K)){
        for(int i = 0;i < K; i++){
            scanf("%d%d", &R[i], &C[i]);
        }
        solve();
    
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值