代码看不懂求助
  • 板块学术版
  • 楼主不慕放糖
  • 当前回复1
  • 已保存回复1
  • 发布时间2021/11/27 13:41
  • 上次更新2023/11/3 23:28:52
查看原帖
代码看不懂求助
544113
不慕放糖楼主2021/11/27 13:41

二分图匈牙利算法

#include <cstdio>
#include <vector>

const int maxn = 1005;

int n, m, t;
int mch[maxn], vistime[maxn];

std::vector<int> e[maxn];

bool dfs(const int u, const int tag);

int main() {
  scanf("%d %d %d", &n, &m, &t);
  for (int u, v; t; --t) {
    scanf("%d %d", &u, &v);
    e[u].push_back(v);
  }//从这里开始
  int ans = 0;
  for (int i = 1; i <= n; ++i) if (dfs(i, i)) {
    ++ans;
  }
  printf("%d\n", ans);
}

bool dfs(const int u, const int tag) {
  if (vistime[u] == tag) return false;
  vistime[u] = tag;
  for (auto v : e[u]) if ((mch[v] == 0) || dfs(mch[v], tag)) {
    mch[v] = u;
    return true;
  }
  return false;
}
2021/11/27 13:41
加载中...