[USACO05NOV]Asteroids G - 洛谷https://www.luogu.com.cn/problem/P7368题目让我们求最小代价,我们可以考虑那些点在同一行和同一列得,所以我们可以将这些点行列连边,然后求最小点覆盖,走一遍匈牙利就好。
int n, k;
int head[MAXN];
int nxt[MAXN];
int ver[MAXN];
int cnt;
int match[MAXN];
int vis[MAXN];
int id;
void add(int x, int y) {
ver[++cnt] = y;
nxt[cnt] = head[x];
head[x] = cnt;
}
bool dfs(int p) {
for (int i = head[p]; i; i = nxt[i]) {
int v = ver[i];
// printf("%d==\n", v);
// printf("%d %d\n", vis[v], id);
if (vis[v] - id) {
// printf("===\n");
vis[v] = id;
if (!match[v] || dfs(match[v])) {
match[v] = p;
return 1;
}
}
}
return 0;
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1, x, y; i <= k; i++) {
scanf("%d %d", &x, &y);
add(x, y + n);
// v.push_back(x);
}
int ans = 0;
for (int i = id = 1; i <= n; i++, id++) {
if (dfs(i))
ans++;
}
printf("%d", ans);
return 0;
}