Topic Link: Click to open the link
Idea: Because each of the matching points on the left has a priority, we sort by the priority of the points on the left, so that the previous points are prioritized. The reason is simple, we just need to know how the Hungarian algorithm is done: We enumerate each left point in turn, for a left point x, find a right point y, if y is not matched, then (x, y) is a pair of new matches, if Y has matched X ', then we try to find a horse for X ' Matching points, if found then (x, y) is also a pair of new matches. So, for an x on the left, if it had already been matched, then whatever it was, it would have been matched. So there is nothing to say about this problem.
See the code for details:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long long ll;typedef long double ld;const double eps = 1e-6;const double PI = ACOs (-1); const INT mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;const int seed = 131;const ll INF64 = ll (1e18); const int MAXN = 500;int T,N,M,USE[MAXN], from[maxn], tot = 0;struct node {int id, V; BOOL operator < (const node& RHS) Const {return v > rhs.v; }}a[maxn];vector<int> g[maxn];bool Match (int x) {int len = g[x].size (); for (int i = 0; i < len; i++) if (!use[g[x][i]]) {Use[g[x][i]] = true; if (from[g[x][i]] ==-1 | | Match (From[g[x][i]])) {From[g[x][i]] = x; return true; }} return false;} int Hungary (int n) {tot = 0; Memset (from,-1, sizeof (from)); for (int i = 1; I <= n; i++) {memset (use, 0, sizeof (use)); if (Match (a[i].id)) ++tot; } return tot;} int Ans[maxn];int K, v;int main () {while (~SCANF ("%d", &n)} {for (int i = 1; I <= n; i++) {SCA NF ("%d", &A[I].V); Ans[i] = 0; A[i].id = i; } for (int i = 1; I <= n; i++) {scanf ("%d", &k); G[i].clear (); for (int j = 1; j <= K; j + +) {scanf ("%d", &v); G[i].push_back (v); }} sort (a+1, a+n+1); int cur = Hungary (n); for (int i = 1; I <= n; i++) {if (from[i] = = 1); else {Ans[from[i]] = i; }} for (int i = 1; I <= n; i++) {printf ("%d%c ", ans[i], i = = n? ' \ n ': '); }} return 0;}
SGU 210. Beloved Sons (binary image matching)