1. 程式人生 > >hdu 4685 Prince and Princess(最大匹配+強連通所點,5級)

hdu 4685 Prince and Princess(最大匹配+強連通所點,5級)

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