现在的位置: 首页 > 综合 > 正文

hdu 4685 Prince and Princess(最大匹配+强连通所点,5级)

2013年12月09日 ⁄ 综合 ⁄ 共 3295字 ⁄ 字号 评论关闭

Prince and Princess

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

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
 

Recommend
zhuyuanchen520
思路:先来次最大匹配,然后如果有未匹配的点,那么将对立的虚节点对应个。这虚节点连所有对方点。在来次最大匹配,这一定是个完全匹配。
          之后,就将匹配的公主向,王子喜欢的公主连边,强连通缩点判断就OK了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=1050;
int cx[mm],cy[mm],n,m;
bool g[mm][mm];
bool vis[mm];
class Edge
{public:int v,next;
}e[mm*mm*2];
int dfn[mm],bcc_no,dfs_block,e_to[mm];
int stak[mm],top,sub;
int head[mm],edge;
void data()
{
  clr(head,-1);edge=0;
}
void add(int u,int v)
{
  e[edge].v=v;e[edge].next=head[u];head[u]=edge++;
}
bool find(int u)
{ int v;
  for(int i=head[u];~i;i=e[i].next)
  {
    v=e[i].v-sub;
    if(vis[v])continue;
    vis[v]=1;
    if(cy[v]==-1||find(cy[v]))
    {
      cy[v]=u;cx[u]=v;
      return 1;
    }
  }
  return 0;
}
int match(int N)
{ clr(cx,-1);clr(cy,-1);
  int ret=0;
  FOR(i,1,N)
  {
    if(cx[i]==-1)
      {clr(vis,0);
       if(find(i))++ret;
      }
  }
  return ret;
}
int tarjan(int u)
{
  int v;
  int lowv,lowu;stak[top++]=u;
  lowu=dfn[u]=++dfs_block;
  for(int i=head[u];~i;i=e[i].next)
  { v=e[i].v;
    if(!dfn[v])
    {
      lowv=tarjan(v);
      lowu=min(lowu,lowv);
    }
    else if(e_to[v]==-1)
      lowu=min(lowu,dfn[v]);
  }
  if(lowu==dfn[u])
  {
    ++bcc_no;
    do
    {
      v=stak[--top];
      e_to[v]=bcc_no;
    }while(v!=u);
  }
  return lowu;
}
void get_bcc(int N)
{
  bcc_no=dfs_block=0;top=0;
  clr(dfn,0);clr(e_to,-1);
  FOR(i,0,N)
  if(!dfn[i])
    tarjan(i);
}
int ans[mm],pos;
int main()
{
  int cas,zzz,x,N,M;
  while(~scanf("%d",&cas))
  {
    FOR(ca,1,cas)
    {
      printf("Case #%d:\n",ca);
      scanf("%d%d",&n,&m);
      clr(g,0);data();
      FOR(i,1,n)
      {
        scanf("%d",&zzz);
        FOR(j,1,zzz)
        {
          scanf("%d",&x);g[i][x]=1;
          add(i,x+n);
        }
      }   sub=n;N=n;M=m;
      int ret=match(N); ///原始的最大匹配
      data(); ///清
     // cout<<ret<<endl;
      M=N=n+m-ret;///新的匹配点数
      sub=N; ///u->v 建u->v+N 加边数

      FOR(i,1,n)FOR(j,1,m) ///补原图
      if(g[i][j])add(i,j+N);

      FOR(i,n+1,N)FOR(j,1,M) ///加虚节点
      add(i,j+N),g[i][j]=1;

      FOR(i,1,N)FOR(j,m+1,M)
      add(i,j+N),g[i][j]=1;

      match(N); //匹配
      data();
      FOR(i,1,N)FOR(j,1,M)
      if(g[i][j]&&cx[i]!=j)//i王子选的不是j但喜欢j
        add(cx[i],j);

      get_bcc(N);//缩点
      FOR(i,1,n)
      {
        pos=0;
        FOR(j,1,m)
        if(g[i][j]&&e_to[cx[i]]==e_to[j])
        {
          ans[pos++]=j;
        }
        printf("%d",pos);
        FOR(j,0,pos-1)
        printf(" %d",ans[j]);printf("\n");
      }
    }
  }
  return 0;
}

抱歉!评论已关闭.