Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 1728 Accepted Submission(s): 581
转自:http://blog.sina.com.cn/s/blog_68629c7701012lcn.html
大致题意:
1 #include <iostream> 2 #include <iomanip> 3 #include <fstream> 4 #include <sstream> 5 #include <algorithm> 6 #include <string> 7 #include <set> 8 #include <utility> 9 #include <queue> 10 #include <stack> 11 #include <list> 12 #include <vector> 13 #include <cstdio> 14 #include <cstdlib> 15 #include <cstring> 16 #include <cmath> 17 #include <ctime> 18 #include <ctype.h> 19 using namespace std; 20 21 #define MAXN 500+10 22 23 int graph[MAXN][MAXN]; 24 int path[MAXN]; 25 bool vst[MAXN]; 26 int c[MAXN][2],d[MAXN][2]; //构建正确的模型很重要 27 int cnt1,cnt2; 28 int n,m,p; 29 30 int dfs(int v) 31 { 32 for(int i=1;i<cnt2;i++) 33 if(!vst[i]&&graph[v][i]) 34 { 35 vst[i]=1; 36 if(path[i]==-1||dfs(path[i])) 37 { 38 path[i]=v; 39 return true; 40 } 41 } 42 return false; 43 } 44 45 int hungary() 46 { 47 int cnt=0; 48 memset(path,-1,sizeof(path)); 49 for(int i=1;i<cnt1;i++) 50 { 51 memset(vst,0,sizeof(vst)); 52 if(dfs(i)) 53 cnt++; 54 } 55 return cnt; 56 } 57 58 int main() 59 { 60 char ch1,ch2; 61 int a,b; 62 int ans; 63 int i,j; 64 while(~scanf("%d%d%d",&n,&m,&p)) 65 { 66 cnt1=cnt2=1; 67 memset(graph,0,sizeof(graph)); 68 for(i=1;i<=p;i++) 69 { 70 getchar(); 71 scanf("%c%d %c%d",&ch1,&a,&ch2,&b); 72 if(ch1=='C') 73 { 74 c[cnt1][0]=a; 75 c[cnt1++][1]=b; 76 } 77 else 78 { 79 d[cnt2][0]=a; 80 d[cnt2++][1]=b; 81 } 82 } 83 for(i=1;i<cnt1;i++) 84 for(j=1;j<cnt2;j++) 85 { 86 if(c[i][0]==d[j][1]||c[i][1]==d[j][0]) 87 graph[i][j]=1; 88 } 89 ans=hungary(); 90 printf("%d\n",p-ans); 91 } 92 return 0; 93 }