[UOJ79]一般图最大匹配

带花树,那是啥?

每次复杂度 \(O(nm)\) 随机5次似乎就卡不掉(起码现在uoj上没有能卡掉这个的

#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  return x;
}
#define pb push_back
#define lo1(i,a) for(int i = 1; i <= a; ++i)
vector<int>G[505];
int mat[505], Ans[505], ans, n = read(), m = read(), vis[503], idx;
bool dfs(int u) {
  vis[u] = idx; random_shuffle(G[u].begin(), G[u].end());
  for (auto v : G[u]) {
    int v1 = mat[v]; 
    if (vis[v1] == idx) continue;
    mat[v1] = 0, mat[u] = v, mat[v] = u;
    if (!v1 || dfs(v1)) return 1;
    mat[v1] = v, mat[u] = 0, mat[v] = v1;
  }
  return 0;
}

inline void add(int u, int v) {G[u].pb(v), G[v].pb(u);}

int main() {
  while (m--) add(read(), read());
  lo1(K, 3) {
    lo1(i, n) if (!mat[i]) ++idx, dfs(i);
    int tmp = 0;
    lo1(i, n) tmp += (mat[i] != 0);
    if (tmp > ans) ans = tmp, copy(mat + 1, mat + 1 + n, Ans + 1);
  }
  cout << ans / 2 << '\n';
  lo1(i, n) cout << Ans[i] << ' ';
  return 0;
}

猜你喜欢

转载自www.cnblogs.com/storz/p/10472759.html