思路:模板题,注意是无向图,所以最后结果要除以2。点有1500个,邻接矩阵会超时,用了邻接表。
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<cstdlib> 7 #include<sstream> 8 #include<iomanip> 9 #include<vector> 10 #include<map> 11 #include<set> 12 #include<queue> 13 using namespace std; 14 const int MOD = 1e9 + 7; 15 16 typedef long long LL; 17 typedef unsigned long long ULL; 18 19 const int maxn = 1500 + 5; 20 vector<int> G[maxn]; 21 int link[maxn]; 22 bool check[maxn]; 23 int n; 24 25 bool dfs(int u){ 26 for(int i = 0; i < G[u].size(); ++i){ 27 if(!check[G[u][i]]){ 28 check[G[u][i]] = true; 29 if(link[G[u][i]] == -1 || dfs(link[G[u][i]])){ 30 link[G[u][i]] = u; 31 return true; 32 } 33 } 34 } 35 return false; 36 } 37 38 int hugarian(){ 39 int ans = 0; 40 memset(link,-1,sizeof(link)); 41 for(int i = 0; i < n; ++i){ 42 memset(check,0,sizeof(check)); 43 if(dfs(i)) ++ans; 44 } 45 return ans; 46 } 47 48 int main(){ 49 while(scanf("%d",&n) == 1){ 50 for(int i = 0; i < n; ++i){ 51 G[i].clear(); 52 } 53 for(int i = 0; i < n; ++i){ 54 int j,k; 55 scanf("%d:(%d)",&j,&k); // 这个地方一开始用char*读入再赋给j,k,就MLE了 56 while(k--){ 57 int x; 58 scanf("%d",&x); 59 G[j].push_back(x); 60 G[x].push_back(j); 61 } 62 } 63 printf("%d\n",hugarian()/2); 64 } 65 return 0; 66 }