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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值