Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Created May 7, 2014 16:30
Show Gist options
  • Save KT-Yeh/923912d13d9f42cce9b9 to your computer and use it in GitHub Desktop.
Save KT-Yeh/923912d13d9f42cce9b9 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> edge[105];
int llink[105], rlink[105];
bool used[105];
bool dfs(int now)
{
for (int nxt : edge[now]) {
if (used[nxt]) continue;
used[nxt] = true;
if (rlink[nxt] == -1 || dfs(rlink[nxt]) == true) {
llink[now] = nxt;
rlink[nxt] = now;
return true;
}
}
return false;
}
int main()
{
int n, m, k;
while (scanf("%d", &n) && n) {
for (auto &v : edge) v.clear();
// Input
scanf("%d %d", &m, &k);
int i, x, y;
for (int j = 0; j < k; ++j) {
scanf("%d %d %d", &i, &x, &y);
if (!x || !y) continue; // mode 0直接忽略!
edge[x].push_back(y);
}
// Bipartite
int result = 0;
fill(begin(llink), end(llink), -1);
fill(begin(rlink), end(rlink), -1);
for (int i = 0; i < n; ++i) {
fill(begin(used), end(used), false);
if (dfs(i) == true) ++result;
}
// Output
printf("%d\n", result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment