Description
Input
Output
Sample Input
6 3 3 1 1 1 2 1 3 2 1 2 3 3 1 0
Sample Output
3
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> using namespace std; #define maxn 5005 #define inf 0x3f3f3f3f int first[maxn],nxt[maxn],to[maxn],e; int vis[maxn],linker[maxn]; int k,m,n; void addedge(int u,int v){ to[e]=v; nxt[e]=first[u]; first[u]=e++; } bool dfs(int u){ for(int i=first[u];~i;i=nxt[i]){ int v=to[i]; if(!vis[v]){ vis[v]=1; if(linker[v]==-1||dfs(linker[v])){ linker[v]=u; return true; } } } return false; } int match(){ memset(linker,-1,sizeof linker); int ans=0; for(int i=1;i<=n;i++){ memset(vis,0,sizeof vis); if(dfs(i))ans++; } return ans; } int main() { int u,v; //freopen("in.txt","r",stdin); while(~scanf("%d",&k)&&k){ scanf("%d%d",&m,&n); e=0; memset(first,-1,sizeof first); for(int i=1;i<=k;i++){ scanf("%d%d",&u,&v); // addedge(u,v); //从u向v连边,则在u这边开始挑选 addedge(v,u); <span style="font-family: Arial, Helvetica, sans-serif;">//从v向u连边,则在v这边开始挑选</span> } printf("%d\n",match()); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u013497977/article/details/46966831