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

*/