// // main.cpp // hdu3829 // // Created by Fangpin on 15/5/29. // Copyright (c) 2015年 FangPin. All rights reserved. // #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; const int MAXN=1505; int link[MAXN],n,m,p; vector<int> g[MAXN]; bool vis[MAXN]; char a[MAXN][100],b[MAXN][100]; bool dfs(int x){ for(int i=0;i<g[x].size();++i){ int y=g[x][i]; if(vis[y]) continue; vis[y]=true; if(link[y]==-1 || dfs(link[y])){ link[y]=x; return true; } } return false; } int hungry(){ memset(link,-1,sizeof(link)); int ans=0; for(int i=1;i<=p;++i){ memset(vis,false,sizeof(vis)); if(dfs(i)) ++ans; } return ans; } int main(int argc, const char * argv[]) { while(~scanf("%d%d%d",&n,&m,&p)){ for(int i=0;i<=p;++i) g[i].clear(); for(int i=1;i<=p;++i){ scanf("%s%s",a[i],b[i]); } for(int i=1;i<=p;++i){ for(int j=i+1;j<=p;++j){ if(strcmp(a[i],b[j])==0 || strcmp(b[i],a[j])==0){ g[i].push_back(j); g[j].push_back(i); } } } cout<<p-hungry()/2<<endl; } return 0; }
原文地址:http://blog.csdn.net/fangpinlei/article/details/46239885