hdu 4685 Prince and Princess(最大匹配+强连通)

本文详细阐述了解决匹配问题的算法实现过程,包括输入数据处理、最大匹配查找、构造虚拟人并求强连通分量等步骤。通过实例演示了如何通过构建有向图、求最大匹配和缩点求强连通分量来解决实际问题,最终输出每个王子能够与哪些公主匹配结婚,同时确保不会影响原先的最大匹配数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Prince and Princess

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 263    Accepted Submission(s): 82


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 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个公主,每个王子都喜欢若干个公主,王子只能和他喜欢的公主结婚,而公主可以和任一个王子结婚。给出每个王子喜欢的公主,这样可以得到一个最大配对数。对于每个王子,现在要分别输出每个王子能和哪些公主结婚,要求王子和这些公主中任一个结婚后,不会影响原先的最大配对数。
思路:这道题和poj1904 (poj1904的解法)很相似,解法也是差不多的,但是这道题王子和公主的数量不一样,而且不一定有完美匹配。所以我们要虚拟出一些人出来构成完美匹配。先将每个王子与他喜欢的女人连一条有向边,求一次最大匹配。
            对于每个没有匹配到的王子,虚拟出一个公主,这个公主被所有王子喜欢,所以所有的王子都要连一条有向边到这个虚拟的公主。
            对于每个没有匹配到的公主,虚拟出一个王子,这个王子喜欢所有公主,所以这个王子要连一条有向边到所有的公主。
           最后对于每个公主(包括虚拟的),都要连一条有向边到与她配对的王子。构图完成后,缩点求强连通分量,每个王子都可以与和他在同一个强连通分量里的所有公主结婚,当然不能是虚拟的公主。

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <map>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;

const int maxn=2005;
struct node
{
    int v,next;
} edge[maxn*maxn];
int G[maxn],matchn[maxn],matchm[maxn],ans[maxn];
int low[maxn],dfn[maxn],scc[maxn],stack[maxn];
bool vis[maxn],ins[maxn];
int n,m,num,nn,top,snum,cnt,tot;
void init()
{
    memset(G,-1,sizeof(G));
    num=0;
}
void add(int u,int v)
{
    edge[num].v=v;
    edge[num].next=G[u];
    G[u]=num++;
}
void input()
{
    int k,v;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&k);
        while(k--)
        {
            scanf("%d",&v);
            add(i,v+n);
        }
    }
}
bool find(int u)
{
    for(int i=G[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(!vis[v])
        {
            vis[v]=true;
            if(matchm[v]==-1||find(matchm[v]))
            {
                matchm[v]=u;
                matchn[u]=v;
                return true;
            }
        }
    }
    return false;
}
void MMG()
{
    int ans=0;
    memset(matchn,-1,sizeof(matchn));
    memset(matchm,-1,sizeof(matchm));
    for(int i=1; i<=n; i++)
    {
        memset(vis,false,sizeof(vis));
        if(find(i)) ans++;
    }
}
void make_graph()
{
    tot=n+m;
    for(int i=1; i<=n; i++)
        if(matchn[i]==-1)
        {
            ++tot;
            matchn[i]=tot;
            matchm[tot]=i;
            for(int k=1; k<=n; k++)
                add(k,tot);
        }
    for(int i=n+1; i<=n+m; i++)
        if(matchm[i]==-1)
        {
            ++tot;
            matchn[tot]=i;
            matchm[i]=tot;
            for(int k=n+1; k<=n+m; k++) add(tot,k);
        }
    for(int i=1; i<=tot; i++)
        if(matchm[i]!=-1) add(i,matchm[i]);
}
void dfs(int u)
{
    int x;
    low[u]=dfn[u]=++cnt;
    stack[top++]=u;
    ins[u]=true;
    for(int i=G[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            dfs(v);
            low[u]=min(low[u],low[v]);
        }
        else if(ins[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        snum++;
        do
        {
            x=stack[--top];
            ins[x]=false;
            scc[x]=snum;
        }
        while(x!=u);
    }
}
void tarjan()
{
    memset(dfn,0,sizeof(dfn));
    memset(ins,false,sizeof(ins));
    top=cnt=snum=0;
    for(int i=1; i<=tot; i++)
        if(!dfn[i]) dfs(i);
}
void solve()
{
    for(int u=1; u<=n; u++)
    {
        int cc=0;
        for(int i=G[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            if(scc[u]==scc[v]&&v<=n+m)
                ans[cc++]=v-n;
        }
        sort(ans,ans+cc);
        printf("%d",cc);
        for(int i=0; i<cc; i++)
            printf(" %d",ans[i]);
        printf("\n");
    }
}
int main()
{
    //freopen("1","r",stdin);
    int t,c=0;
    scanf("%d",&t);
    while(t--)
    {
        init();
        input();
        MMG();
        make_graph();
        tarjan();
        printf("Case #%d:\n",++c);
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值