1. 程式人生 > >hdu 4685 Prince and Princess (二分圖匹配+tarjan)

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();
    }
}