King's Quest

Description
Once upon a time there lived a king and he had N sons. And there were N beautiful girls in the kingdom and the king knew about each of his sons which of those girls he did like. The sons of the king were young and light-headed, so it was possible for one son to like several girls.

So the king asked his wizard to find for each of his sons the girl he liked, so that he could marry her. And the king's wizard did it -- for each son the girl that he could marry was chosen, so that he liked this girl and, of course, each beautiful girl had to marry only one of the king's sons.

However, the king looked at the list and said: "I like the list you have made, but I am not completely satisfied. For each son I would like to know all the girls that he can marry. Of course, after he marries any of those girls, for each other son you must still be able to choose the girl he likes to marry."

The problem the king wanted the wizard to solve had become too hard for him. You must save wizard's head by solving this problem.

Input
The first line of the input contains N -- the number of king's sons (1 <= N <= 2000). Next N lines for each of king's sons contain the list of the girls he likes: first Ki -- the number of those girls, and then Ki different integer numbers, ranging from 1 to N denoting the girls. The sum of all Ki does not exceed 200000.

The last line of the case contains the original list the wizard had made -- N different integer numbers: for each son the number of the girl he would marry in compliance with this list. It is guaranteed that the list is correct, that is, each son likes the girl he must marry according to this list.

Output
Output N lines.For each king's son first print Li -- the number of different girls he likes and can marry so that after his marriage it is possible to marry each of the other king's sons. After that print Li different integer numbers denoting those girls, in ascending order.

Sample Input

4
2 1 2
2 1 2
2 2 3
2 3 4
1 2 3 4

Sample Output

2 1 2
2 1 2
1 3
1 4

题意:给出一个n对n的二分图以及它的一个最大匹配M,问对一个点,可以选什么点,使得其他点仍然可以得到最大匹配。
对于xi喜欢的mm,连一条单向边,对于给出的匹配,从mm连向那个男生。
分析:对于给出的匹配中X集合,从xi出发看看是否有其他交错路径,使得xi可以选其他女人。对于给出的匹配xi->yi,现在尝试从xi出发选别的女孩xi->Y-yi,看最后能否达到终点yi,形成一个环。如果存在这样的路径,则路径会经过一样多的x点跟y点,使得一一对应,这条路径中分为两条交错路径,一个是原来的匹配,一个是另一个可行替换匹配,通过女人走到男人的是给定的M中的边,从男人走到女人的是非匹配的边。那一个出发点xi可以存在多个这样的环,问题就成为找强连通分量,xi可以配对的女人就在那连通分量里。
#include <iostream>
#include 
<stdlib.h>
#include 
<stdio.h>
#define maxn 4100 
#define Min(a, b) a < b ? a : b
using namespace std;
struct T
{
    
int v, next;
}fn[maxn 
* maxn];
int g[maxn], ans[maxn];
int stack[maxn], visit[maxn], scc[maxn], dfn[maxn], low[maxn], instack[maxn];
int n, th, top, id, time;
int cmp(const void * a, const void * b)
{
    
return *((int*)a) - *((int*)b);
}
void set()
{
    
int i;
    th 
= 1;
    
for (i = 0; i <= 2 * n; i++)
    {
        g[i] 
= -1;
    }
}
void add(int u, int v)
{
    fn[th].v 
= v, fn[th].next = g[u], g[u] = th++;
}
void dfs(int u)
{
    
int v, k;
    dfn[u] 
= low[u] = ++time;
    stack[
++top] = u;//栈从1开始
    instack[u] = 1;//标记进栈
    for (k = g[u]; k != -1; k = fn[k].next)
    {
        v 
= fn[k].v;
        
if (dfn[v] == 0)//dfn是时间戳,都初始化为0,被访问过的店带有时间
        {
            dfs(v);
            low[u] 
= Min(low[u], low[v]);
        }
        
else if (instack[v])//如果在栈中,则表示该点没被归入任何一个scc中,还在当前搜索树中
        {
            low[u] 
= Min(low[u], low[v]);
        }
    }
    
if (dfn[u] == low[u])
    {
        id
++;
        
do
        {
            v 
= stack[top--];
            instack[v] 
= 0;//防止交叉边长时进行low[u] = Min(low[u], low[v]).因为v已经属于一个连通分量
            scc[v] = id;
        }
while (v != u);
    }
}
void tarjan()
{
    id 
= 0, top = 0, time = 0;
    
int t = 0, i;
    
for (i = 1; i <= 2 * n; i++)
    {
        dfn[i] 
= 0;//要设为0
        scc[i] = i;
        instack[i] 
= 0;//要设为0
    }
    
for (i = 1; i <= 2 * n; i++)
    {
        
if (!dfn[i])
        {
            dfs(i);
        }
    }
}
int main()
{
    
int i, j, k, m, v;
    
while (scanf("%d"&n) - EOF)
    {
        
set();
        
for (i = 1; i <= n; i++)
        {
            scanf(
"%d"&m);
            
while (m--)
            {
                scanf(
"%d"&v);
                add(i, v 
+ n);
            }
        }
        
for (i = 1; i <= n; i++)
        {
            scanf(
"%d"&v);
            add(v 
+ n, i);
        }
        tarjan();
        
for (i = 1; i <= n; i++)
        {
            
for (j = g[i], k = 0; j != -1; j = fn[j].next)
            {
                v 
= fn[j].v;
                
if (scc[i] == scc[v])
                {
                    ans[k
++= v - n;
                }
            }                
            qsort(ans, k, 
sizeof(int), cmp);
            
for (printf("%d", k), j = 0; j < k; j++)
            {
                printf(
" %d", ans[j]);
            }printf(
"\n");
        }
    }
    
return 0;
}
/*
3
2 1 2
3 1 2 3
2 2 3
1 2 3

2
2 1 2
2 1 2
1 2

input : 
5
3 3 4 5
2 4 5
1 3
3 1 2 3
3 1 4 5
5 4 3 2 1

output : 
2 4 5
2 4 5
1 3
1 2
1 1

*/