BZOJ1191 [HNOI2006]超级英雄Hero

Solution  

  裸的二分图最大匹配,不用多说。

  《论不认真读题的后果》。。。注意一但匹配失败就要break。。。

  Code

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
inline int read(){
    int x=0;char ch=0;
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return x;
}
bool vis[N],Link[N][N]; 
int n,m,match[N];
bool dfs(int x){
    for(int y=0;y<n;++y)
        if(!vis[y]&&Link[x][y]){
            vis[y]=true;
            if(!match[y]||dfs(match[y])){
                match[y]=x;
                return true;
            }
        }
    return false;
}
int hungary(){
    int res=0;
    for(int i=1;i<=m;++i){
        memset(vis,0,sizeof vis);
        if(dfs(i)) ++res;
        else break;
    }
    return res;
}
int main(){
    n=read(),m=read();
    for(int i=1;i<=m;++i){
        int x=read(),y=read();
        Link[i][x]=Link[i][y]=true;
    }
    cout<<hungary()<<endl;
    return 0;
} 
BZOJ1191

  

猜你喜欢

转载自www.cnblogs.com/gosick/p/11246459.html