1. 程式人生 > >hdu 4685 Prince and Princess 【匈牙利演算法-匹配、強連通分量-Tarjan-縮點】

hdu 4685 Prince and Princess 【匈牙利演算法-匹配、強連通分量-Tarjan-縮點】

                                Prince and Princess
    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Problem Description
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 ki(0<=ki<=m), and then ki 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-縮點

AC程式碼:

# include <cstdio>
# include <cstring>
# include <algorithm>

using namespace std;

# define MAXN 2005

struct EDGE
{
    int v;
    int next;
}edge[MAXN * 200];

int tot;
int top;
int index;
int scc;
int head[MAXN];
int low[MAXN];
int dfn[MAXN];
int instack[MAXN];
int stack[MAXN];
int belong[MAXN];
int result[MAXN];
int used[MAXN];
int match[MAXN];
int rematch[MAXN];

/**********************************************/
//匈牙利演算法:求最大匹配數
int Dfs(int u)
{
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if (!used[v])
        {
            used[v] = 1;
            if (-1 == match[v] || Dfs(match[v]))
            {
                match[v] = u;
                rematch[u] = v;
                return 1;
            }
        }
    }
    return 0;
}

int Hungary(int n)
{
    memset(match, -1, sizeof(match));
    memset(rematch, -1, sizeof(rematch));
    int res = 0;
    for (int u = 1; u <= n; u++)
    {
        memset(used, 0, sizeof(used));
        res += Dfs(u);
    }
    return res;
}
/**********************************************/

void Init()
{
    tot = 0;
    top = 0;
    index = 0;
    scc = 0;
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(instack, 0, sizeof(instack));
}

void Addedge(int u, int v)
{
    edge[tot].v = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}


/**********************************************/
//Tarjan演算法:求強連通分量,此題中用於縮點
void Tarjan(int u)
{
    int v;
    low[u] = dfn[u] = ++index;
    instack[u] = 1;
    stack[top++] = u;

    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].v;
        if (!dfn[v])
        {
            Tarjan(v);
            if (low[v] < low[u])
            {
                low[u] = low[v];
            }
        }
        else if (instack[v] && dfn[v] < low[u])
        {
            low[u] = dfn[v];
        }
    }
    if (dfn[u] == low[u])
    {
        scc++;
        do
        {
            v = stack[--top];
            instack[v] = 0;
            belong[v] = scc;
        } while (v != u);
    }
}
/**********************************************/

void Solve(int maxn, int tn, int tm)
{
    int i, j;
    int cnt = tn + tm - Hungary(maxn);
    //printf("cnt = %d\n", cnt);
    int all = 2 * maxn;

    //王子有剩餘
    for (i = 1; i <= maxn; i++)
    {
        if (-1 == rematch[i])
        {
            all++; //虛擬出的公主編號
            rematch[i] = all;
            match[all] = i;
            for (j = 1; j <= maxn; j++)
            {
                Addedge(j, all); //虛擬出的公主可以和所有王子結婚
            }
        }
    }

    //公主有剩餘
    for (i = 1; i <= maxn; i++)
    {
        if (-1 == match[i + maxn])
        {
            all++; //虛擬出的王子編號
            rematch[all] = i + maxn;
            match[i + maxn] = all;
            for (j = 1; j <= maxn; j++)
            {
                Addedge(all, j + maxn); //虛擬出的王子喜歡所有的公主
            }
        }
    }

    for (i = 1; i <= all; i++)
    {
        if (-1 != rematch[i])
        {
            Addedge(rematch[i], i); //把能夠組合的公主與王子連在一起
        }
    }

    for (i = 1; i <= all; i++)
    {
        if (!dfn[i])
        {
            Tarjan(i);
        }
    }

    for (int u = 1; u <= tn; u++)
    {
        int k = 0;
        for (i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].v;
            if (belong[u] == belong[v]) //當公主和王子在同一強連通分量中,說明他們可以進行匹配
            {
                if (v - maxn <= tm)
                {
                    result[k++] = v - maxn;
                }
            }
        }
        sort(result, result + k);
        printf("%d", k);
        for (i = 0; i < k; i++)
        {
            printf(" %d", result[i]);
        }
        printf("\n");
    }
}

int main(void)
{
    int t;
    int casecount = 1;
    scanf("%d", &t);
    while (t--)
    {
        Init();
        int n, m;
        int i, j, k, ps;
        printf("Case #%d:\n", casecount ++);
        scanf("%d %d", &n, &m);

        int max = n > m ? n : m;
        for (i = 1; i <= n; i++)
        {
            scanf("%d", &k);
            for (j = 1; j <= k; j++)
            {
                scanf("%d", &ps);
                Addedge(i, ps + max);
            }
        }
        Solve(max, n, m);
    }
    return 0;
}