您的位置:首页 > 其它

hdu 4685 Prince and Princess(最大匹配+强连通)

2013-08-16 14:23 483 查看


Prince and Princess

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 263 Accepted Submission(s): 82



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


题意:有n个王子和m个公主,每个王子都喜欢若干个公主,王子只能和他喜欢的公主结婚,而公主可以和任一个王子结婚。给出每个王子喜欢的公主,这样可以得到一个最大配对数。对于每个王子,现在要分别输出每个王子能和哪些公主结婚,要求王子和这些公主中任一个结婚后,不会影响原先的最大配对数。
思路:这道题和poj1904(poj1904的解法)很相似,解法也是差不多的,但是这道题王子和公主的数量不一样,而且不一定有完美匹配。所以我们要虚拟出一些人出来构成完美匹配。先将每个王子与他喜欢的女人连一条有向边,求一次最大匹配。
对于每个没有匹配到的王子,虚拟出一个公主,这个公主被所有王子喜欢,所以所有的王子都要连一条有向边到这个虚拟的公主。
对于每个没有匹配到的公主,虚拟出一个王子,这个王子喜欢所有公主,所以这个王子要连一条有向边到所有的公主。
最后对于每个公主(包括虚拟的),都要连一条有向边到与她配对的王子。构图完成后,缩点求强连通分量,每个王子都可以与和他在同一个强连通分量里的所有公主结婚,当然不能是虚拟的公主。

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <map>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;

const int maxn=2005;
struct node
{
int v,next;
} edge[maxn*maxn];
int G[maxn],matchn[maxn],matchm[maxn],ans[maxn];
int low[maxn],dfn[maxn],scc[maxn],stack[maxn];
bool vis[maxn],ins[maxn];
int n,m,num,nn,top,snum,cnt,tot;
void init()
{
memset(G,-1,sizeof(G));
num=0;
}
void add(int u,int v)
{
edge[num].v=v;
edge[num].next=G[u];
G[u]=num++;
}
void input()
{
int k,v;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d",&v);
add(i,v+n);
}
}
}
bool find(int u)
{
for(int i=G[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
vis[v]=true;
if(matchm[v]==-1||find(matchm[v]))
{
matchm[v]=u;
matchn[u]=v;
return true;
}
}
}
return false;
}
void MMG()
{
int ans=0;
memset(matchn,-1,sizeof(matchn));
memset(matchm,-1,sizeof(matchm));
for(int i=1; i<=n; i++)
{
memset(vis,false,sizeof(vis));
if(find(i)) ans++;
}
}
void make_graph()
{
tot=n+m;
for(int i=1; i<=n; i++)
if(matchn[i]==-1)
{
++tot;
matchn[i]=tot;
matchm[tot]=i;
for(int k=1; k<=n; k++)
add(k,tot);
}
for(int i=n+1; i<=n+m; i++)
if(matchm[i]==-1)
{
++tot;
matchn[tot]=i;
matchm[i]=tot;
for(int k=n+1; k<=n+m; k++) add(tot,k);
}
for(int i=1; i<=tot; i++)
if(matchm[i]!=-1) add(i,matchm[i]);
}
void dfs(int u)
{
int x;
low[u]=dfn[u]=++cnt;
stack[top++]=u;
ins[u]=true;
for(int i=G[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
snum++;
do
{
x=stack[--top];
ins[x]=false;
scc[x]=snum;
}
while(x!=u);
}
}
void tarjan()
{
memset(dfn,0,sizeof(dfn));
memset(ins,false,sizeof(ins));
top=cnt=snum=0;
for(int i=1; i<=tot; i++)
if(!dfn[i]) dfs(i);
}
void solve()
{
for(int u=1; u<=n; u++)
{
int cc=0;
for(int i=G[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(scc[u]==scc[v]&&v<=n+m)
ans[cc++]=v-n;
}
sort(ans,ans+cc);
printf("%d",cc);
for(int i=0; i<cc; i++)
printf(" %d",ans[i]);
printf("\n");
}
}
int main()
{
//freopen("1","r",stdin);
int t,c=0;
scanf("%d",&t);
while(t--)
{
init();
input();
MMG();
make_graph();
tarjan();
printf("Case #%d:\n",++c);
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: