强连通+二分匹配(hdu4685 Prince and Princess)

Prince and Princess

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

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

Source

2013 Multi-University Training Contest 8

题意:一个王国里有n个王子和m个公主,公主可以跟任何王子结婚,但是王子可以选择和他喜欢的公主结婚,喜欢的公主可以有多个,但是最终只能选择其中一个结婚,公主一样也必须只能和一个王子结婚,现在给出n和m,接下来n行,第i行首先有个数ki,然后k个数,代表第i个王子喜欢的公主编号,接下来输出n行,表示每个王子可以有多少公主可以选择,并从小大输出公主标号,并保证这些选择不会有冲突,且满足最大匹配

程序:

#include"cstdio"
#include"cstring"
#include"cstdlib"
#include"cmath"
#include"string"
#include"map"
#include"cstring"
#include"iostream"
#include"algorithm"
#include"queue"
#include"stack"
#define inf 0x3f3f3f3f
#define M 10009
#define eps 1e-8
#define INT int
#define LL __int64
using namespace std;
struct node
{
    int u,v,next;
}edge[300009];
int cmp(int a,int b)
{
    return a<b;
}
int h[M];
int x[M];
int y[M];
int head[M];
int dfn[M];
int belong[M];
int low[M];
int use[M];
int sum[M];
int in[M];
int out[M];
int t,indx,num,m,n;
stack<int>q;
void init()
{
    t=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
    edge[t].u=u;
    edge[t].v=v;
    edge[t].next=head[u];
    head[u]=t++;
}
int finde(int u)
{
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!use[v])
        {
            use[v]=1;
            if(!y[v]||finde(y[v]))
            {
                use[v]=1;
                y[v]=u;
                x[u]=v;
                return 1;
            }
        }
    }
    return 0;
}
void tarjan(int u,int id)
{
    dfn[u]=low[u]=++indx;
    q.push(u);
    use[u]=1;
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            tarjan(v,i);
            low[u]=min(low[u],low[v]);
        }
        else if(use[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        int p;
        num++;
        do
        {
            p=q.top();
            q.pop();
            use[p]=0;
            belong[p]=num;
            sum[num]++;

        }while(p!=u);
    }
}
int match(int nl)
{
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
    int ans=0;
    for(int i=1;i<=nl;i++)
    {
        if(!x[i])
        {
            memset(use,0,sizeof(use));
            ans+=finde(i);
        }
    }
    return ans;
}
void slove()
{
    int cnt=match(n);
    int all=m+n-cnt;
    for(int i=n+1;i<=all;i++)
    {
        for(int j=1;j<=all;j++)
            add(i,j+1000);
    }
    for(int i=1;i<=all;i++)
    {
        for(int j=m+1;j<=all;j++)
        {
            //if(x[i]==0)
            add(i,j+1000);
        }
    }
    match(all);
    for(int i=1;i<=all;i++)
    {
        if(x[i])
            add(x[i],i);
    }
    num=indx=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(use,0,sizeof(use));
    memset(sum,0,sizeof(sum));
    for(int i=1;i<=all;i++)
    {
        if(!dfn[i])
        {
            tarjan(i,-1);
        }
    }
    for(int u=1;u<=n;u++)
    {
        int fuck=0;
        for(int i=head[u];~i;i=edge[i].next)
        {
            int v=edge[i].v;
            if(v>n&&belong[u]==belong[v]&&v<=1000+m)
                h[fuck++]=v-1000;
        }
        sort(h,h+fuck,cmp);
        printf("%d",fuck);
        for(int i=0;i<fuck;i++)
        printf(" %d",h[i]);
        printf("\n");
    }
}
int main()
{
    int T,kk=1;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=n;i++)
        {
            int k,j;
            scanf("%d",&k);
            while(k--)
            {
                scanf("%d",&j);
                add(i,1000+j);
            }
        }
        printf("Case #%d:\n",kk++);
        slove();
    }
}

时间: 2024-10-12 13:49:04

强连通+二分匹配(hdu4685 Prince and Princess)的相关文章

HDU 3861 The King’s Problem (强连通+二分匹配)

题目地址:HDU 3861 这题虽然是两个算法结合起来的.但是感觉挺没意思的..结合的一点也不自然,,硬生生的揉在了一块...(出题者不要喷我QAQ.) 不过这题让我发现了我的二分匹配已经好长时间没用过了..都快忘了..正好在省赛之前又复习了一下. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm>

HDU4685 Prince and Princess 完美匹配+强连通

题意:现在有n个王子,m个公主.现在要给他们配对,王子会和他喜欢的一个人结婚,而公主不能做选择. 这题啃得好费劲,有个类似的题目poj1904,那个题目也是给王子与公主配对,但那个是王子公主各n个,且给定了一个完美匹配,然后求每个王子可以做出的选择且不影响最大匹配数目.那题是先建各条喜欢关系的边,然后在由被选择的公主连一条边到与之配对的王子,强连通之后如果一个王子和一个公主在一个强连通分量中,那么他们结合的话,他们的另一半也各自能在强连通中找到另外的匹配,就是符合题意的结果了. 这个题目算是升级

HDU4685 Prince and Princess 完美搭配+良好的沟通

意甲冠军:今天,有n王子,m公主.现在给他们配对,与王子会嫁给一个男人,他喜欢.公主无法做出选择. 这标题去咬硬,还有一类似的题目poj1904.那个题目也是给王子与公主配对,但那个是王子公主各n个,且给定了一个完美匹配,然后求每一个王子能够做出的选择且不影响最大匹配数目.那题是先建各条喜欢关系的边.然后在由被选择的公主连一条边到与之配对的王子.强连通之后假设一个王子和一个公主在一个强连通分量中,那么他们结合的话,他们的还有一半也各自能在强连通中找到另外的匹配,就是符合题意的结果了. 这个题目算

Prince and Princess HDU - 4685(匹配 + 强连通)

Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 2336    Accepted Submission(s): 695 Problem Description There are n princes and m princesses. Princess can marry any prince. B

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

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

UVa 10635 (LIS+二分) Prince and Princess

题目的本意是求LCS,但由于每个序列的元素各不相同,所以将A序列重新编号{1,2,,,p+1},将B序列重新编号,分别为B中的元素在A中对应出现的位置(没有的话就是0). 在样例中就是A = {1 7 5 4 8 3 9},B = {1 4 3 5 6 2 8 9} 重新编号以后: A = {1 2 3 4 5 6 7}, B = {1 4 6 3 0 0 5 7}(里面的0在求LIS时可以忽略) 这样求A.B的LCS就转变为求B的LIS 求LIS用二分优化,时间复杂度为O(nlogn) 第一次

【连通图|二分匹配+强连通分量】POJ-1904 King&#39;s Quest

King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon a time there lived a king and he had N sons. And there were N beautiful girls in the kingdom and the king knew about each of his sons which of those g

HDU 4685 Prince and Princess

Prince and Princess Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 468564-bit integer IO format: %I64d      Java class name: Main There are n princes and m princesses. Princess can marry any prince. But prin

uva 10635 Prince and Princess(LCS问题转化成LIS问题O(nlogn))

题目大意:有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数.两个序列的第一个元素均为1.求出A和B的最长公共子序列长度. 分析:本题是LCS问题,但是p*q<=62500,O(pq)的算法显然会LE.在这里有一个条件,每个序列中的各个元素互不相同,所以可以把A中元素重新编号为1~p+1.例如,样例中A={1,7,5,4,8,3,9},B={1,4,3,5,6,2,8,9},因此把A重新编号为{1,2,3,4,5,6,7},则B就是{1,4,6,3,0