码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-21 09:54:02      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

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;
}


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

标签:

原文地址:http://blog.csdn.net/u014737310/article/details/44498997

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!