#include<iostream> #include<stdio.h> #include<string> #define MAXN 105 using namespace std; struct unix { string s1; string s2; }; bool ck[MAXN]; int _m[MAXN][MAXN]; int match[MAXN]; string rec[MAXN]; unix dev[MAXN]; unix adap[MAXN]; bool mark_adap[MAXN]; int hungary(int a,int b); int a; int b; int c; void For_adap(int i); void make_map(); int main() { //freopen("acm.acm","r",stdin); int i; int j; int u; int v; int edge; cin>>a; for(i = 0; i < a; ++ i) { cin>>rec[i]; } cin>>b; for(i = 0; i < b; ++ i) { cin>>dev[i].s1; cin>>dev[i].s2; } cin>>c; for(i = 0; i < c; ++ i) { cin>>adap[i].s1; cin>>adap[i].s2; } memset(_m,0,sizeof(_m)); make_map(); //for(i = 0; i < b; ++ i) //{ // for(j = 0; j < a; ++ j) //. { // if(_m[i][j] == 1) // cout<<dev[i].s1<<"---->"<<rec[j]<<endl; // } //} cout<<b-hungary(b,a)<<endl; } void make_map() { int i; int j; for(i = 0; i < b; ++ i) { memset(mark_adap,false,sizeof(mark_adap)); for(j = 0; j < a; ++ j) { if(dev[i].s2 == rec[j]) _m[i][j] = 1; } For_adap(i); } } void For_adap(int i) { int j; int k; string tem; for(j = 0; j < c; ++ j) { if(dev[i].s2 == adap[j].s1 && !mark_adap[j]) { mark_adap[j] = true; for(k = 0; k < a; ++ k) { if(rec[k] == adap[j].s2) { _m[i][k] = 1; } } tem = dev[i].s2; dev[i].s2 = adap[j].s2; For_adap(i); dev[i].s2 = tem; } } } /////////////////////////////////////////////////// //hungary—by myself /////////////////////////////////////////////////// bool search(int a,int b,int x) { int i; int t; for ( i = 0 ; i < b ; i++) if (_m[x][i] && ! ck[i]) { ck[i] = true; t = match[i]; match[i] = x; if (t == -1 || search(a,b,t)) return true; match[i] = t; } return false; } int hungary(int a,int b) { memset(match,-1,sizeof(match)); int max_match = 0; int i; for ( i = 0 ; i < a ; i++) { memset(ck,false,sizeof(ck)); if (search(a,b,i)) max_match++; } return max_match; }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com