POJ 1466
1 #include<iostream> 2 #include<stdio.h> 3 #define MAXN 505 4 using namespace std; 5 6 int edge[MAXN][MAXN]; 7 int match[MAXN]; 8 bool ck[MAXN]; 9 10 int hungary(int num1,int num2); 11 bool search_dfs(int num1,int num2,int value); 12 13 int main() 14 { 15 //freopen("acm.acm","r",stdin); 16 int num; 17 int i; 18 int j; 19 int u; 20 int v; 21 int s; 22 int index; 23 while(scanf("%d",&num) != EOF) 24 { 25 for(i = 0; i < num; ++ i) 26 { 27 edge[i][0] = -1; 28 } 29 for(i = 0; i < num; ++ i) 30 { 31 scanf("%d",&u); 32 getchar(); 33 getchar(); 34 getchar(); 35 scanf("%d",&s); 36 getchar(); 37 getchar(); 38 index = 0; 39 for(j = 0; j < s; ++ j) 40 { 41 scanf("%d",&v); 42 edge[u][index ++] = v; 43 edge[u][index] = -1; 44 } 45 } 46 cout<<num - hungary(num,num)/2<<endl; 47 } 48 } 49 50 int hungary(int num1,int num2) 51 { 52 memset(match,-1,sizeof(match)); 53 int i; 54 int sum = 0; 55 for(i = 0; i < num1; ++ i) 56 { 57 memset(ck,false,sizeof(ck)); 58 if(search_dfs(num1,num2,i)) 59 ++ sum; 60 } 61 return sum; 62 } 63 bool search_dfs(int num1,int num2,int value) 64 { 65 int i; 66 int t; 67 i = 0; 68 while(edge[value][i] != -1) 69 { 70 if(!ck[edge[value][i]]) 71 { 72 ck[edge[value][i]] = true; 73 t = match[edge[value][i]]; 74 match[edge[value][i]] = value; 75 if(t == -1 || search_dfs(num1,num2,t)) 76 return true; 77 match[edge[value][i]] = t; 78 } 79 ++ i; 80 } 81 return false; 82 }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com