用二分图的最大匹配来求解题目,关于其思想详见:
http://blog.csdn.net/xinhanggebuguake/article/details/6668071
//#include<fstream>
#include<iostream>
using namespace std;
const int MAX = 102;
bool linkMap[MAX][MAX];
int crossPath[MAX];
bool used[MAX];
int n, m;
bool search(int u)
{
for (int i=1;i<m;i++)
{
if (linkMap[u][i]&&!used[i])
{
used[i]=1;//保证路径上无重复点出现
if (crossPath[i]==-1||search(crossPath[i]))
{
crossPath[i]=u;//①增广路径的取反
return true;
}
}
}
return false;
}
int hungary()
{
int cnt = 0;
memset(crossPath, -1, sizeof(crossPath));
for(int i= 1; i<n; i++)
{
memset(used,0, sizeof(used));
if(search(i))
cnt++;
}
return cnt;
}
int main()
{
//ifstream cin("Machine Schedule.txt");
int k;
while(cin>>n,n)
{
cin>>m>>k;
memset(linkMap,false, sizeof(linkMap));
for(int i =0; i < k; i++)
{
int v1, v2;
cin>>v1>>v1>>v2;
if(v1&&v2)
linkMap[v1][v2] = true;
}
cout<<hungary()<<endl;
}
return 0;
}