POJ 1325 二分图最大匹配

根据题意,因为要求重启机器最少次数,转换过来就是图论中的最小顶点覆盖(用最少的点,让每条边都至少和其中一个点关联),而又由于二分图中的最大匹配数等于这个图中的最小点覆盖数,所以直接求最大匹配即可。

#include<iostream>
#include<cstring>
using namespace std;

const int maxn=105;

int e[maxn][maxn];
int book[maxn];
int match[maxn];
int n,m;

bool dfs(int u){
    for(int i=1;i<=m-1;i++){
        if(e[u][i]==1&&!book[i]){
            book[i]=1;
            if(match[i]==-1||dfs(match[i])){
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int k;
    while(cin>>n&&n){
        cin>>m>>k;
        memset(match,-1,sizeof(match));
        memset(e,0,sizeof(e));
        while(k--){
            int c,a,b;
            cin>>c>>a>>b;
            e[a][b]=1;
        }
        int ans=0;
        for(int i=1;i<=n-1;i++){
            memset(book,0,sizeof(book));
            if(dfs(i)) ans++;
        }
        cout<<ans<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40679299/article/details/81140112