无向图匈牙利算法求地的值除以2才是答案,因为每个边都匹配了两次,有向图算法得出的就是答案。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int maps[50][50],match[50],vis[50],n; bool Find(int u) { cout<<"the fa = "<<u<<endl; for(int i = 1; i <= n; i++) { if(maps[u][i]) { if(!vis[i]) { vis[i] = 1; if(match[i] == -1 || Find(match[i])) { cout<<"the son = "<<i<<endl; match[i] = u; return true; } else cout<<"don't find his son"<<endl; } } } return false; } int slove() { memset(match,-1,sizeof(match)); int ans = 0; for(int i = 1; i <= n; i++) { memset(vis,0,sizeof(vis)); if(Find(i)) ans++; } return ans; } int main() { int m,a,b,t; ///freopen("Ain.txt","r",stdin); cin>>t; while(t--) { memset(maps,0,sizeof(maps)); cin>>n>>m; while(m--) { cin>>a>>b; maps[b][a] = 1; maps[a][b] = 1; } int ans = slove(); cout<<"ans = "<<ans/2<<endl; } return 0; }