hdu 4685 Prince and Princess(Tarjan+二分匹配)

Prince and Princess

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


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
 

Source
 

题意与上一篇的题目差不多  但是没有给出一个匹配 所以相对来说 更难了

所以需要先进行二分匹配 构造出完美匹配

由于n m的值可能不想等 所以 可能会出现 王子或公主单身的情况

这样就要给这些人安排对象了 但是 对象不能是已有的人

所以需要虚拟一些人出来  

假设王子有剩下的,那么每个剩下的王子就连一个虚拟的公主,这个公主被所有的王子都喜欢。

假设公主有剩下的,那么每个剩下的公主就连一个虚拟的王子,这个王子喜欢所有的公主

然后就是缩点 求联通分量

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' || ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

const int MAXN=2010;

struct Edge
{
    int to,next;
}edge[200*MAXN],arc[260000];
int head[MAXN],tot;
int low[MAXN],dfn[MAXN],sta[MAXN],belong[MAXN];
int index,top;
int scc;
bool instack[MAXN];
int n,m,maxnum;
int vis[MAXN],lx[MAXN],ly[MAXN];
int first[MAXN],cnt;
int sum;

void addarc(int u,int v)
{
    arc[cnt].to=v; arc[cnt].next=first[u]; first[u]=cnt++;
}

int dfs(int u)
{
    for(int i=first[u];i!=-1;i=edge[i].next)
    {
        int v=arc[i].to;
        if(!vis[v])
        {
            vis[v]=1;
            if(ly[v]==0||dfs(ly[v]))
            {
                ly[v]=u;
                lx[u]=v;
                return 1;
            }
        }
    }
    return 0;
}

int hungary()
{
    int ans=0;
    MEM(ly,0);  MEM(lx,0);
    for(int i=1;i<=maxnum;i++)
    {
        MEM(vis,0);
        if(dfs(i))
            ans++;
    }
    return ans;
}

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

void Tarjan(int u)
{
    int v;
    low[u]=dfn[u]=++index;
    sta[top++]=u;
    instack[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].to;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[v],low[u]);
        }
        else if(instack[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        scc++;
        do{
            v=sta[--top];
            instack[v]=0;
            belong[v]=scc;
        }while(u!=v);
    }
}


void solve()
{
    for(int i=1;i<=sum;i++)
        if(!dfn[i])
            Tarjan(i);
}

void init()
{
    MEM(head,-1);
    tot=0;
    MEM(dfn,0);
    MEM(instack,0);
    index=scc=top=0;
    MEM(first,-1);
    cnt=0;
}

int res[MAXN];

int main()
{
//    fread;
    int tc;
    scanf("%d",&tc);
    int cs=1;
    while(tc--)
    {
        scanf("%d%d",&n,&m);
        init();
        maxnum=max(n,m);
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            for(int j=0;j<x;j++)
            {
                int v;
                scanf("%d",&v);
                addedge(i,v+maxnum);
                addarc(i,v+maxnum);
            }
        }
        hungary();
        sum=maxnum*2;
        for(int i=1;i<=maxnum;i++)
        {
            if(lx[i]==0)//剩下的王子
            {
                sum++;
                lx[i]=sum;
                ly[sum]=i;
                for(int j=1;j<=maxnum;j++)
                    addedge(j,sum);//添加虚拟的
            }
        }
        for(int i=1;i<=maxnum;i++)
        {
            if(ly[i+maxnum]==0)//剩下的公主
            {
                sum++;
                ly[i+maxnum]=sum;
                lx[sum]=i+maxnum;
                for(int j=1;j<=maxnum;j++)
                    addedge(sum,j+maxnum);
            }
        }
        for(int i=1;i<=sum;i++)
            if(lx[i])
                addedge(lx[i],i);
        solve();
        printf("Case #%d:\n",cs++);
        for(int u=1;u<=n;u++)
        {
            int x=0;
            for(int j=head[u];j!=-1;j=edge[j].next)
            {
                int v=edge[j].to;
//                cout<<v<<endl;
//                cout<<belong[u]<<"  "<<belong[v]<<endl;
                if(belong[u]==belong[v])
                {
                    if(v-maxnum<=m)
                        res[x++]=v-maxnum;
                }
            }
            sort(res,res+x);
            printf("%d",x);
            for(int i=0;i<x;i++)
            {
                printf(" %d",res[i]);
            }
            printf("\n");
        }
    }
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值