King's Quest【POJ 1904】【Tarjan强连通分量】

版权声明:https://blog.csdn.net/qq_41730082 https://blog.csdn.net/qq_41730082/article/details/86984144

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

Hint

This problem has huge input and output data,use scanf() and printf() instead of cin and cout to read data to avoid time limit exceed.


  记得写成强连通分量,但是忘记了另一件事,不是在一个强连通分量里的都是可以的,别忘了还要是王子自己喜欢的人才行,可能同一个强连通图里,有该王子不喜欢的人呢。


后面有一组很有用的测试样例。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 2005;
const int maxM = 2e5 + 7;
int N, head[maxN<<1], cnt, ans[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxM + maxN];
void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
int dfn[maxN<<1], low[maxN<<1], _Index, Stap[maxN<<1], Stop, Belong[maxN<<1], Bcnt;
bool instack[maxN<<1];
void tarjan(int u)
{
    int v;
    dfn[u] = low[u] = ++_Index;
    instack[u] = true;
    Stap[++Stop] = u;
    for(int i=head[u]; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            if(low[v] < low[u]) low[u] = low[v];
        }
        else if(instack[v] && dfn[v] < low[u]) low[u] = dfn[v];
    }
    if(dfn[u] == low[u])
    {
        Bcnt++;
        do
        {
            v = Stap[Stop--];
            Belong[v] = Bcnt;
            instack[v] = false;
        } while (u != v);
    }
}
inline void init()
{
    memset(head, -1, sizeof(head));
    cnt = _Index = Stop = Bcnt = 0;
    memset(instack, false, sizeof(instack));
    memset(dfn, 0, sizeof(dfn));
}
int main()
{
    while(scanf("%d", &N)!=EOF)
    {
        init();
        for(int i=1; i<=N; i++)
        {
            int k;  scanf("%d", &k);
            while(k--)
            {
                int e1; scanf("%d", &e1);
                addEddge(i, e1+N);
            }
        }
        for(int i=1; i<=N; i++)
        {
            int e1; scanf("%d", &e1);
            addEddge(e1+N, i);
        }
        for(int i=1; i<=N; i++) if(!dfn[i]) tarjan(i);
        for(int u=1; u<=N; u++)
        {
            int len = 0;
            for(int i=head[u]; ~i; i=edge[i].nex)
            {
                int v = edge[i].to;
                if(Belong[u] == Belong[v]) ans[++len] = v - N;
            }
            printf("%d", len);
            sort(ans + 1, ans + len + 1);
            for(int i=1; i<=len; i++) printf(" %d", ans[i]);
            printf("\n");
        }
    }
    return 0;
}
/*
3
2 1 2
3 1 2 3
2 2 3
1 2 3
ans:
2 1 2
3 1 2 3
2 2 3
*/

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/86984144