建图+Dinic=Accept
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int sta,end;
int val[300][300];
int tag[300];
int min(int a,int b)
{
return a>b?b:a;
}
int bfs()
{
int u,v;
queue<int> q;
while (!q.empty())
q.pop();
q.push(sta);
memset(tag,-1,sizeof(tag));
tag[sta]=1;
while (!q.empty())
{
u=q.front();
q.pop();
for (v=1; v<=end; v++)
{
if (val[u][v] > 0 && tag[v] == -1)
{
tag[v]=tag[u]+1;
q.push(v);
}
}
}
return tag[end];
}
int dfs(int u,int flow)
{
int v,t;
if (u == end)
return flow;
for (v=sta; v<=end; v++)
{
if (val[u][v] > 0 && tag[u] == tag[v]-1 && (t=dfs(v,min(flow,val[u][v])) != 0))
{
val[u][v]-=t;
val[v][u]+=t;
return t;
}
}
return 0;
}
int dinic()
{
int res,ans;
ans=0;
while (bfs() != -1)
{
while (true)
{
res=dfs(sta,0x7fffffff);
if (res == 0)
break;
ans+=res;
}
}
return ans;
}
int main()
{
int t1,t2,t3,i,j,k,m,n;
while (true)
{
scanf("%d",&n);
if (n == 0)
break;
scanf("%d%d",&m,&k);
sta=1;
end=n+m+2;
memset(val,0,sizeof(val));
while (k--)
{
scanf("%d%d%d",&t1,&t2,&t3);
t2++;
t3++;
if (t2 == 1 || t3 == 1)
continue;
t2+=1;
t3+=(n+1);
val[sta][t2]=1;
val[t2][t3]=1;
val[t3][end]=1;
}
printf("%d\n",dinic());
}
}