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
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.
题意:每个王子有几个喜欢的女孩,和一个媒婆给出的配对(彼此喜欢),问每个王子能选哪些女孩,使每个王子都能选到自己喜欢的女孩
思路:每个王子和喜欢的女孩连边,配对的女孩和王子连边,然后跑tarjan,每个王子选和他在同一个强连通分量的女孩
#include <cstdio>
#include<iostream>
#include <cstring>
#include<vector>
#include <algorithm>
#include <stack>
using namespace std;
#define ll long long
const int maxn=5e3;
const int maxm=6e5;
stack<int>q;
int cnt,n,m,head[maxn],dfn[maxn],low[maxn],co[maxn],tot,id,out[maxn];
vector <int> a[maxn];
struct Edge
{
int to,next;
}e[maxm];
void add(int u,int v)
{
e[cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt++;
}
void init()
{
for(int i=0;i<maxn;i++) a[i].clear();
memset(out,0,sizeof(out));
memset(co,0,sizeof(co));
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
while(!q.empty()) q.pop();
cnt=0;id=0,tot=0;
}
void tarjan(int u)
{
q.push(u);
dfn[u]=low[u]=++tot;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!co[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
id++;
//a[id].clear();
while(q.top()!=u)
{
int x=q.top();
q.pop();
co[x]=id;
}
co[u]=id;
q.pop();
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
for(int j=1;j<=k;j++)
{
int x;
scanf("%d",&x);
add(i,n+x);
}
}
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
add(n+x,i);
}
for(int i=1;i<=2*n;i++)
{
if(dfn[i]==0) tarjan(i);
}
for(int i=1;i<=n;i++)
{
for(int j=head[i];j!=-1;j=e[j].next)
{
int v=e[j].to;
if(co[v]==co[i])
{
a[i].push_back(v-n);
}
}
int len=a[i].size();
printf("%d",len);
sort(a[i].begin(),a[i].end());
for(int j=0;j<len;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
}
}