终于来写了这题的解题报告,没有在昨天A出来有点遗憾,不得不说数组开大开小真的是阻碍人类进步的一大天坑。
题目大意:给出n个王子,m个公主,只要王子喜欢,公主就得嫁(这个王子当得好霸道),求在最大匹配数的情况下,每个王子能和哪些公主匹配。
题解:这题做过了弱化版的(poj_1904),在之前的博客也有提及。解法就是跑完hungey
之后,为每个单身的王子都虚拟一个公主,并且每个王子都喜欢这个公主;为每个单身的公主都虚拟一个王子,并且这个王子喜欢每个公主。然后公主连反向边给王子,接下来就是Tarjan
缩点,把一个强连通分量的公主王子输出即可,注意判断编号的合理性(虚拟的就不可输出)。
细节:因为n,m<=500
,所以各种虚拟最多也就是4*500=2000个点,于是乎,我边集数组又开小了,只开了20w,最后开到100w过的,不得不说边数还是真的多,也确实2000个点20w的边数是少了。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define clr(a,b) memset((a),(b),sizeof(a))
using namespace std;
const int NN = 5005;
const int M = 1e6 + 16;
struct Edge
{
int nxt, u, v;
};
Edge edge[M];
int ecnt, head[NN];
int low[NN], dfn[NN], sta[NN], col[NN];
int top, sum, dep;
bool vis[NN], _vis[NN];
int N;
int n, m;
int fr1[NN], fr2[NN];
int ans[NN];
//fr1 princesss fr2 prince
void init()
{
dep = top = sum = ecnt = 0;
clr(head,-1);
clr(vis,0);
clr(col,0);
clr(dfn,0);
clr(low,0);
clr(sta,0);
}
void _add( int a, int b )
{
edge[ecnt].u = a;
edge[ecnt].v = b;
edge[ecnt].nxt = head[a];
head[a] = ecnt ++;
}
void tarjan( int u )
{
sta[++top] = u;
vis[u] = 1;
low[u] = dfn[u] = ++dep;
for ( int i = head[u]; i+1; i = edge[i].nxt )
{
int v = edge[i].v;
if ( !dfn[v] )
{
tarjan(v);
low[u] = min( low[u], low[v] );
}
else if ( vis[v] )
low[u] = min( low[u], low[v] );
}
if ( low[u] == dfn[u] )
{
col[u] = ++sum;
vis[u] = 0;
while ( sta[top] != u )
{
col[sta[top]] = sum;
vis[sta[top--]] = 0;
}
top --;
}
}
bool find( int u )
{
for ( int i = head[u]; i+1; i = edge[i].nxt )
{
int v = edge[i].v;
if ( !_vis[v] )
{
_vis[v] = 1;
if ( fr1[v] == -1 || find(fr1[v]) )
{
fr1[v] = u;
fr2[u] = v;
return 1;
}
}
}
return 0;
}
void hungry()
{
clr(fr1,-1);
clr(fr2,-1);
for ( int i = 1; i <= n; i ++ )
{
clr(_vis,0);
find(i);
}
}
int main()
{
int T;
scanf("%d", &T);
for ( int cas = 1; cas <= T; cas ++ )
{
init();
scanf("%d%d", &n, &m);
N = max( n, m );
for ( int i = 1; i <= n; i ++ )
{
int p, q;
scanf("%d", &p);
while ( p -- )
{
scanf("%d", &q);
_add(i,q+N);
}
}
hungry();
int peo = 2*N;
int tmp = 2*N;
//fake princess
for ( int i = 1; i <= n; i ++ )
{
if ( fr2[i] == -1 )
{
peo ++;
for ( int j = 1; j <= N; j ++ )
_add( j, peo );
fr2[i] = peo;
fr1[peo] = i;
}
}
//fake prince
for ( int i = N+1; i <= tmp; i ++ )
{
if ( fr1[i] == -1 )
{
peo ++;
for ( int j = N+1; j <= tmp; j ++ )
_add( peo, j );
fr1[i] = peo;
fr2[peo] = i;
}
}
for ( int i = 1; i <= peo; i ++ )
if ( fr2[i] != -1 )
_add( fr2[i], i );
for ( int i = 1; i <= n; i ++ )
if ( !dfn[i] )
tarjan(i);
printf("Case #%d:
", cas);
for ( int i = 1; i <= n; i ++ )
{
int cnt = 0;
for ( int j = head[i]; j+1; j = edge[j].nxt )
{
int v = edge[j].v;
if ( col[i] == col[v] )
{
if ( v - N <= m )
ans[cnt++] = v - N;
}
}
sort(ans,ans+cnt);
printf("%d", cnt);
for ( int k = 0; k < cnt; k ++ )
printf(" %d", ans[k]);
printf("
");
}
}
return 0;
}