[Codeforces Round49F] Session in BSU
阿新 • • 發佈:2018-08-19
hungary i++ sdi oid get match truct lse blank
[題目鏈接]
http://codeforces.com/contest/1027/problem/F
[算法]
二分圖匹配
[代碼]
#include<bits/stdc++.h> #pragma GOC optimize("Ofast") using namespace std; const int MAXN = 1e6 + 10; struct edge { int to,nxt; } e[MAXN << 2]; int n,q,len,ans,tot; int a[MAXN],b[MAXN],ta[MAXN],tb[MAXN],tmp[MAXN << 1],match[MAXN << 2],head[MAXN << 1],visited[MAXN << 2]; template <typename T> inline void read(T &x) { int f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘; x *= f; } inline void addedge(int u,int v) { tot++; e[tot] = (edge){v,head[u]}; head[u] = tot; } inline bool hungary(int u,int k) { int v; for (int i = head[u]; i; i = e[i].nxt) { v = e[i].to;if (visited[v] != k) { visited[v] = k; if (!match[v] || hungary(match[v],k)) { match[v] = u; return true; } } } return false; } int main() { read(n); for (int i = 1; i <= n; i++) { read(a[i]); read(b[i]); tmp[++len] = a[i]; tmp[++len] = b[i]; } sort(tmp + 1,tmp + len + 1); len = unique(tmp + 1,tmp + len + 1) - tmp - 1; for (int i = 1; i <= n; i++) { ta[i] = lower_bound(tmp + 1,tmp + len + 1,a[i]) - tmp; tb[i] = lower_bound(tmp + 1,tmp + len + 1,b[i]) - tmp; } for (int i = 1; i <= n; i++) { addedge(ta[i],len + i); addedge(tb[i],len + i); } for (int i = 1; i <= len; i++) { if (hungary(i,i)) { ans++; if (ans == n) { printf("%d\n",tmp[i]); return 0; } } } printf("-1\n"); return 0; }
[Codeforces Round49F] Session in BSU