1.匈牙利算法
#include<bits/stdc++.h>
using namespace std;
bool g[200][200],used[200];
int ans,n,m,linker[300];
bool dfs(int u)
{
for(int v = 0;v < m; ++v)
{
if(g[u][v] && !used[v])
{
used[v] = 1;
if(linker[v] == -1 || dfs(linker[v]))
{
linker[v] = u;
return 1;
}
}
}
return 0;
}
int hungary()
{
int res = 0;
memset(linker,-1,sizeof(linker));
for(int u = m;u < m + n; u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
}
int main()
{
scanf("%d%d",&m,&n);
memset(g,0,sizeof(g));
int x,y;
while(~scanf("%d%d",&x,&y))
{
if(x == -1 && y == -1) break;
g[x - 1][y - 1] = g[y - 1][x - 1] = 1;
}
int ans = hungary();
if(!ans)
{
printf("No Solution!\n");
return 0;
}
printf("%d\n",ans);
for(int i = 0;i < m; ++i)
if(linker[i] != -1)
printf("%d %d\n",i + 1,linker[i] + 1);
return 0;
}