hdu 4685 Prince and Princess (二分图匹配+tarjan)

There are n princes and m princesses. Princess can marry any prince. But prince can only marry the princess they DO love.
For all princes,give all the princesses that they love. So, there is a maximum number of pairs of prince and princess that can marry.
Now for each prince, your task is to output all the princesses he can marry. Of course if a prince wants to marry one of those princesses,the maximum number of marriage pairs of the rest princes and princesses cannot change.

Input

The first line of the input contains an integer T(T<=25) which means the number of test cases.
For each test case, the first line contains two integers n and m (1<=n,m<=500), means the number of prince and princess.
Then n lines for each prince contain the list of the princess he loves. Each line starts with a integer k i(0<=k i<=m), and then k i different integers, ranging from 1 to m denoting the princesses.

Output

For each test case, first output "Case #x:" in a line, where x indicates the case number between 1 and T.
Then output n lines. For each prince, first print li, the number of different princess he can marry so that the rest princes and princesses can still get the maximum marriage number.
After that print li different integers denoting those princesses,in ascending order.

Sample Input

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

Sample Output

Case #1:
2 1 2
2 1 2
1 3
1 4
Case #2:
2 1 2

题意:有n个皇子,m个姑娘,皇子所喜欢的姑娘都已经给出,问在最大匹配数不变的情况下,每个皇子能选择那几个姑娘。

思路:先用二分图匹配求出完美匹配,因为题目给出的数据中可能会有皇子数量和姑娘数量不相等的情况,所以给每个没有匹配到的皇子建造一个虚拟姑娘,让所有皇子都喜欢该姑娘,给每个没有皇子的姑娘建造一个皇子,该皇子喜欢所有姑娘。跑一遍tarjan,在一个强连通中的皇子和姑娘,皇子选择该姑娘,最大匹配数不会改变。(POJ - 1904 的深入题)。

#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<stack>
#define M 2010
using namespace std;
struct path
{
    int to,nextt;
}A[2000010];
stack<int>q;
int book[M],DFN[M],LOW[M],head[M],rex[M],rey[M],used[M],re[M],an[M];
int tot,t,n,m,x,y,k,carry,indox,ph=0;
void init()
{
    tot=carry=indox=0;
    memset(DFN,-1,sizeof(DFN));
    memset(LOW,-1,sizeof(LOW));
    memset(rex,-1,sizeof(rex));
    memset(rey,-1,sizeof(rey));
    memset(head,-1,sizeof(head));
    memset(book,0,sizeof(book));
}
void add(int x,int y)
{
    A[tot].to=y;
    A[tot].nextt=head[x];
    head[x]=tot++;
}
int dfs(int u)
{
    int tem;
    for(int i=head[u];i!=-1;i=A[i].nextt)
    {
        tem=A[i].to;
        if(!used[tem])
        {
            used[tem]=1;
            if(rey[tem]==-1||dfs(rey[tem]))
            {
                rey[tem]=u;
                rex[u]=tem;
                return 1;
            }
        }
    }
    return 0;
}
int hungary(int tem)
{
    int ans=0;
    for(int i=1;i<=tem;i++)
    {
        memset(used,0,sizeof(used));
        ans+=dfs(i);
    }
    return ans;
}
void tarjan(int u)
{
    DFN[u]=LOW[u]=++indox;
    q.push(u);
    book[u]=1;
    int tem;
    for(int i=head[u];i!=-1;i=A[i].nextt)
    {
        tem=A[i].to;
        if(DFN[tem]==-1)
        {
            tarjan(tem);
            LOW[u]=min(LOW[u],LOW[tem]);
        }
        else if(book[tem])
        {
            LOW[u]=min(LOW[u],DFN[tem]);
        }
    }
    if(DFN[u]==LOW[u])
    {
        ++carry;
        do
        {
            tem=q.top();
            q.pop();
            book[tem]=0;
            re[tem]=carry;
        }while(tem!=u);
    }
}
void build()
{
    int ans=hungary(n);
    int all=n+m;
    for(int i=1;i<=n;i++)
    {
        if(rex[i]==-1)//给没有匹配姑娘的皇子建造一个虚拟姑娘
        {
            ++all;
            rex[i]=all;
            rey[all]=i;
            for(int j=1;j<=n;j++)
            {
                add(j,all);
            }
        }
    }
    for(int i=n+1;i<=n+m;i++)
    {
        if(rey[i]==-1)//给没有匹配姑娘的皇子建造虚拟皇子
        {
            ++all;
            rey[i]=all;
            rex[all]=i;
            for(int j=n+1;j<=n+m;j++)
            {
                add(all,j);
            }
        }
    }
    for(int i=1;i<=all;i++)//连接姑娘匹配到的皇子
    {
        if(rey[i]!=-1)
        {
            add(i,rey[i]);
        }
    }
    for(int i=1;i<=all;i++)
    {
        if(DFN[i]==-1)
        {
            tarjan(i);
        }
    }
    printf("Case #%d:\n",++ph);
    for(int i=1;i<=n;i++)
    {
        k=0;
        for(int j=head[i];j!=-1;j=A[j].nextt)
        {
            y=A[j].to;
            if(re[i]==re[y]&&y>n&&y<=m+n)//如果是在一个强连通中
            {
                an[k++]=y-n;
            }
        }
        sort(an,an+k);
        printf("%d",k);
        for(int j=0;j<k;j++)
        {
            printf(" %d",an[j]);
        }
        printf("\n");
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(x=1;x<=n;x++)
        {
            scanf("%d",&k);
            for(int i=1;i<=k;i++)
            {
                scanf("%d",&y);
                add(x,y+n);
            }
        }
        build();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值