HDU 4685 Prince and Princess(二分图 + 强连通)

本文介绍了一种解决二分图最大匹配问题的方法,并进一步通过构造辅助图来找出每个节点在保持最大匹配不变的情况下可能的匹配对象。通过匈牙利算法求解初始的最大匹配后,利用强连通分量的概念处理复杂匹配限制问题。

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

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
 

参考博客:  http://www.cnblogs.com/frog112111/p/3387173.html


感觉参考博客写的听仔细的



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 20005

int boy[N],girl[N],vis[N];  //   二分图
int time[N],low[N],cnt,time_num,instack[N],type[N]; //强连通
int n,m,mn;
vector<int>g[N];
stack<int>q;

bool dfs(int x)
{
    int i,j;

    fre(i,0,g[x].size())
    {
        int to=g[x][i];

        if(vis[to]) continue;

        vis[to]=1;

        if(girl[to]==0||dfs(girl[to]))
        {
            boy[x]=to;
            girl[to]=x;
            return true;
        }
    }
   return false;
}

void xiong()
{
    int i,j;
    mem(boy,0);
    mem(girl,0);

    fre(i,1,mn+1)
    {
        mem(vis,0);
        dfs(i);
    }
}


void leave()
{
    int i,j;
    int all=2*mn;

    fre(i,1,mn+1)
    {
        if(boy[i]==0)
        {
           all++;
           fre(j,1,mn+1)
             g[j].push_back(all);

          boy[i]=all;
          girl[all]=i;
        }
    }

   fre(i,mn+1,mn*2+1)
    if(girl[i]==0)
    {
        all++;

        fre(j,mn+1,mn*2+1)
         g[all].push_back(j);

        boy[all]=i;
        girl[i]=all;
    }

   fre(i,1,all+1)
   {
      if(boy[i])
        g[boy[i]].push_back(i);
   }
}

void tarjan(int x)
{
    int i,j;
    time[x]=low[x]=++time_num;

    instack[x]=1;
    q.push(x);

    fre(i,0,g[x].size())
    {
        int to=g[x][i];

        if(time[to]==0)
        {
            tarjan(to);

            if(low[to]<low[x])
                low[x]=low[to];
        }
       else
         if(instack[to]&&time[to]<low[x])
          low[x]=time[to];
    }

    int to;

    if(time[x]==low[x])
    {
        cnt++;

        do{
           to=q.top();
           q.pop();
            instack[to]=0;
           type[to]=cnt;
        }while(to!=x);

    }

}

void solve()
{
    int i,j;
    mem(time,0);
    mem(low,0);
    mem(instack,0);
    mem(type,0);

    cnt=time_num=0;

    while(!q.empty()) q.pop();

    fre(i,1,mn+1)
        if(time[i]==0)
            tarjan(i);

    int ans[N],k;

    fre(i,1,n+1)
    {
        k=0;

        fre(j,0,g[i].size())
        {
            int to=g[i][j];

            if(type[i]!=type[to]) continue;

            if(to-mn<=m)
                 ans[k++]=to-mn;


        }
        sort(ans,ans+k);

        pf("%d",k);
        fre(j,0,k)
          pf(" %d",ans[j]);
        pf("\n");
    }

}

int main()
{
    int i,j,t,ca=0;
    sf(t);
    while(t--)
    {
        sff(n,m);
        mn=max(n,m);

        fre(i,1,mn*4)
            g[i].clear();

        fre(i,1,n+1)
         {
            int k,x;
            sf(k);
            while(k--)
            {
                sf(x);
                g[i].push_back(x+mn);
            }
         }

       pf("Case #%d:\n",++ca);

       xiong();

       leave();
       solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值