传送门
同 HDOJ 1150 Machine Schedule、AcWing 376 机器任务
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> p;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int mod(1e9 + 7);
const int maxn(1e2 + 10);
bool g[maxn][maxn], vis[maxn];
int n, m, k, match[maxn];
template<typename T>
inline const T read()
{
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
template<typename T>
inline void write(T x, bool ln)
{
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10, false);
putchar(x % 10 + '0');
if (ln) putchar(10);
}
bool dfs(int u)
{
for (int v = 1; v < m; ++v) {
if (not vis[v] and g[u][v]) {
vis[v] = true;
if (not match[v] or dfs(match[v])) {
match[v] = u;
return true;
}
}
}
return false;
}
int hungary()
{
int res = 0;
for (int i = 1; i < n; ++i) {
memset(vis, false, sizeof vis);
res += dfs(i);
}
return res;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
while (true) {
memset(g, false, sizeof g);
memset(match, 0, sizeof match);
n = read<int>();
if (not n) break;
m = read<int>();
k = read<int>();
while (k--) {
int i = read<int>(), x = read<int>(), y = read<int>();
if (x and y) {
g[x][y] = true;
}
}
write(hungary(), true);
}
return 0;
}