Acdream 1129 Beloved Sons 二分图匹配

来源:互联网 发布:免费域名跳转 编辑:程序博客网 时间:2024/05/02 04:53

Beloved Sons

Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)     Special Judge
SubmitStatus

Problem Description

      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 could 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."

     Suddenly you recalled that not so long ago the king told you about each of his sons, so you knew how much he loves him. So you decided to please the king and make such a marriage list that the king would be most happy. You know that the happiness of the king will be proportional to the square root of the sum of the squares of his love to the sons that would marry the girls they like.

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

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 Ai ranging 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 of

Sample Input

41 3 2 44 1 2 3 42 1 42 1 42 1 4

Sample Output

2 1 0 4
按照价值排个序,然后用匈牙利算法从最大的开始挨个匹配就行了
#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <map>#include <set>#include <queue>#include <stack>#include <bitset> using namespace std; #define PB push_back#define MP make_pair#define REP(i,n) for(int i=0;i<(n);++i)#define FOR(i,l,h) for(int i=(l);i<=(h);++i)#define DWN(i,h,l) for(int i=(h);i>=(l);--i)#define CLR(vis,pos) memset(vis,pos,sizeof(vis))#define PI acos(-1.0)#define INF 0x3f3f3f3f#define LINF 1000000000000000000LL#define eps 1e-8 int n;int mp[444][444];int linkk[444],use[444];struct node{    int val;    int id;}a[444];int b[444]; bool dfs(int x){    int i;    for(i=1;i<=n;i++)    {        if (use[i]==0&&mp[x][i])        {            use[i]=1;            if (linkk[i]==-1||dfs(linkk[i]))            {                linkk[i]=x;                return true;            }        }    }    return false;} int hungary(){    int num=0;    int i;    for(i=1;i<=n;i++)    {       CLR(use,0);       int j=a[i].id;       if(dfs(j))          num++;    }    return num;} int cmp(node a,node b){    return a.val>b.val;} int main(){    cin>>n;    FOR(i,1,n){        scanf("%d",&a[i].val);        a[i].id=i;    }    sort(a+1,a+1+n,cmp);    /* FOR(i,1,n){        printf("%d\n",a[i].val);    }*/     CLR(mp,0);    int tmp;    int k;    FOR(i,1,n){        scanf("%d",&k);        REP(j,k){            scanf("%d",&tmp);            mp[i][tmp]=1;        }    }    CLR(linkk,-1);    hungary();    CLR(b,0);    FOR(i,1,n){        if(linkk[i]!=-1)            b[linkk[i]]=i;    }    FOR(i,1,n){        if(i>1)            printf(" ");        printf("%d",b[i]);    }    printf("\n");     return 0;}


0 0
原创粉丝点击