# Zoj 2362 beloved sons [binary match]

Source: Internet
Author: User
Tags integer numbers
Question: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 2361 Source: http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 26760 # Problem/B Beloved sons

Time Limit: 2 seconds memory limit: 65536 kb Special Judge

Once upon a time there lived a king and he had n sons. and the king wanted to marry his beloved sons on the girls that they did love. so one day the king asked his sons to come to his
Room and tell him whom do they love.

But the sons of the King were all young men so they cocould not tell exactly whom they did love. Instead of that they just told him the names of the girls that seemed beautiful to them,
But since they were all different, their choices of beautiful girls also did not match exactly.

The king was wise. He did write down the information that the children have provided him with and called you, his main wizard.

"I want all my kids to be happy, you know," he told you, "but since it might be impossible, I want at least some of them to marry the girl they like. so please, prepare the marriage list."

"I want all my kids to be happy, you know," he told you, "but since it might be impossible, I want at least some of them to marry the girl they like. so please, prepare the marriage list."

So, go on, make a list to maximize the king's happiness.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the Problem description. There is a blank
Line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input

The first line of the input file contains N-the number of king's sons (1 <= n <= 400 ). the second line contains N integer numbers airanging from 1 to 1000-the measures
Of King's love to each of his sons.

Next n lines contain lists of king's sons 'preferences-first Ki-the number of the girls the I-th son of the king likes, and then Ki integer numbers-the girls
He likes (all potentially beautiful girls in the kingdom were numbered from 1 to n, you know, beautiful girls were rare in those days ).

Output

Output n numbers-for each son output the number of the beautiful girl he must marry or 0 if he must not marry the girl he likes.

Denote the set of sons that marry a girl they like by L, then you must maximize the value

Sample Input

1

4
1 3 2 4
4 1 2 3 4
2 1 4
2 1 4
2 1 4

Sample output

2 1 0 4

Author: Andrew stankevich
Source: Andrew stankevich's contest #3
Question: it is a matching problem. The king has n sons and n beautiful girls matching them. But it cannot satisfy every son's needs to marry a girl, so he must disappoint the least son to the maximum extent. First, enter multiple groups of test data. T then for a test data, input n to indicate that there are several sons. The following row contains N numbers, indicating the king's liking for the Son Number. [This is tricky. The king must be the most fond of his favorite son, so he will satisfy his favorite son first]
The remaining n rows indicate the first son to the nth son's preferences. the first data in each row indicates the number of girls who like to change.
Finally, let you output the number of the girl from the first son to the last son.
Note: The princes have the same degree of liking for the girls they like. If they do not have a girl they like, they will not marry. The output is 0.
Algorithm: binary matching idea: the original binary matching template cannot be fully applied. You need to modify it yourself. First, sort the princes by their liking (from big to small). First, record the prince's number, map [I] [J] = 0 indicates that prince I does not like girl J; Map [I] [J] = 1 indicates that he liked it and there is nothing to say. Then, the matching results are sorted in order. Note the final output: Match [I] = index; indicates that the girl matches the prince, and the question requires that the number of the girl matched by the Prince be output. just adjust it by yourself.
Original binary match template: [all vertices must be matched]
`#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 410;int map[maxn][maxn];int match[maxn];bool vis[maxn];int uN, vN;bool dfs(int u){    for(int v = 1; v <= vN; v++)    {        if(!vis[v] && map[u][v])        {            vis[v] = true;            if(match[v] == -1 || dfs(match[v]))            {                match[v] = u;                return true;            }        }    }   return false;}bool hungary(){    int sum = 0;    memset(match, -1, sizeof(match));    for(int j = 1 ; j <= uN; j++)    {        memset(vis, false, sizeof(vis));        if(dfs(i)) sum++;    }    if(sum == uN) return true;    else return false;}`

Recommended questions: poj 1469 courses [the maximum matching template Question of the Two-Point Graph for getting started with Hungary algorithms]

`# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 410; int map [maxn] [maxn]; int match [maxn]; bool vis [maxn]; int UN, vn; struct son {int index; int A;} son [maxn]; bool DFS (int u) {for (INT v = 1; v <= Vn; V ++) {If (! Vis [v] & map [u] [v]) {vis [v] = true; if (Match [v] =-1 | DFS (Match [v]) {match [v] = u; return true ;}} return false ;} void Hungary () {int sum = 0; memset (match,-1, sizeof (MATCH); For (Int J = 1; j <= UN; j ++) // match {int I = Son [J] According to the King's liking. index; memset (VIS, false, sizeof (VIS); DFS (I );}}`

 B Accepted 844 KB 260 MS C ++ (G ++ 4.4.5)

`# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 410; int map [maxn] [maxn]; int match [maxn]; bool vis [maxn]; int UN, vn; struct son {int index; // number int A; // King's liking} son [maxn]; int ans [maxn]; bool CMP (son, son B) {return. a> = B. a;} bool DFS (int u) {for (INT v = 1; v <= Vn; V ++) {If (! Vis [v] & map [u] [v]) {vis [v] = true; if (Match [v] =-1 | DFS (Match [v]) {match [v] = u; return true ;}} return false ;} void Hungary () {int sum = 0; memset (match,-1, sizeof (MATCH); For (Int J = 1; j <= UN; j ++) // match {int I = Son [J] According to the King's liking. index; memset (VIS, false, sizeof (VIS); DFS (I) ;}} int main () {int N; int t; scanf ("% d ", & T); While (t --) {scanf ("% d", & N); UN = Vn = N; memset (MAP, 0, S Izeof (MAP); For (INT I = 1; I <= N; I ++) {scanf ("% d", & Son [I]. a); son [I]. index = I;} Sort (son + 1, son + (n + 1), CMP); // sort int num by degree of liking; For (INT I = 1; I <= N; I ++) // simple graph {scanf ("% d", & num); int index; For (Int J = 1; j <= num; j ++) {scanf ("% d", & Index); map [I] [Index] = 1 ;}} Hungary (); memset (ANS, 0, sizeof (ANS); For (INT I = 1; I <= N; I ++) // adjust the result map [I] indicates the prince number matched by girl I {If (Match [I]! =-1) ans [Match [I] = I ;}for (INT I = 1; I <= N; I ++) {if (I = 1) printf ("% d", ANS [I]); else printf ("% d", ANS [I]);} printf ("\ n ");} return 0 ;}`
Related Keywords:

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.