HDU 2236 no question II (Bipartite Graph Matching + Bipartite Graph Matching)

Source: Internet
Author: User
HDU 2236 no question II

Question Link

Idea: Only one row and column can be created. Think of a bipartite graph, then the length of the Bipartite interval, and the lower limit of enumeration, you can find out which sides are usable, and then create a graph to run the bipartite graph, if the maximum match is n

Code:

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int N = 105;int t, n, x[N][N], have[N], hn;int vis[N], left[N];vector<int> g[N];bool dfs(int u) {for (int i = 0; i < g[u].size(); i++) {int v = g[u][i];if (vis[v]) continue;vis[v] = 1;if (left[v] == -1 || dfs(left[v])) {left[v] = u;return true;}}return false;}int hungary() {int ans = 0;memset(left, -1, sizeof(left));for (int i = 0; i < n; i++) {memset(vis, 0, sizeof(vis));if (dfs(i)) ans++;}return ans;}bool judge(int len) {for (int i = 0; i < hn; i++) {for (int j = 0; j < n; j++) g[j].clear();int down = have[i], up = have[i] + len;for (int u = 0; u < n; u++)for (int v = 0; v < n; v++)if (x[u][v] >= down && x[u][v] <= up)g[u].push_back(v);if (hungary() == n) return true;}return false;}int main() {scanf("%d", &t);while (t--) {scanf("%d", &n);memset(vis, 0, sizeof(vis));hn = 0;for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) {scanf("%d", &x[i][j]);if (vis[x[i][j]]) continue;vis[x[i][j]] = 1;have[hn++] = x[i][j];}int l = 0, r = 101;while (l < r) {int mid = (l + r) / 2;if (judge(mid)) r = mid;else l = mid + 1;}printf("%d\n", l);}return 0;}


HDU 2236 no question II (Bipartite Graph Matching + Bipartite Graph Matching)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.