King's Quest
Time Limit: 15000MS | Memory Limit: 65536K | |
Total Submissions: 7590 | Accepted: 2753 | |
Case Time Limit: 2000MS |
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.
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.
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个公主,先给出每个王子都喜欢哪些公主,然后给你一种匹配方式,每个王子对应哪个公主。问每个王子可以娶哪些公主,使得其他人都也都可以娶到自己喜欢的公主。
思路:u表示王子,v表示公主,每个王子喜欢一个公主,对应一个单向边(u,v),给定的匹配方式,对应单向边(v,u),最后求强连通分量,在一个强连通分量里,王子可以娶任何她喜欢的公主,因为王子娶了别人后,原来对应的就会被其他王子娶,否则就不能构成强连通分量。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
int v,next;
}edge[300010];
int n,Head[4010],stack[4010],DFN[4010],Low[4010],tot,scnt,cnt,top,Belong[4010];
bool instack[4010];
int ans[4010];
void add(int u,int v)
{
edge[tot].v=v;
edge[tot].next=Head[u];
Head[u]=tot++;
}
void Tarjan(int u)
{
int i,j,k,v,ret;
DFN[u]=Low[u]=++cnt;
stack[top++]=u;
instack[u]=1;
for(i=Head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(!DFN[v])
{
Tarjan(v);
Low[u]=min(Low[u],Low[v]);
}
else if(instack[v])
Low[u]=min(Low[u],DFN[v]);
}
if(DFN[u]==Low[u])
{
scnt++;
do
{
v=stack[--top];
instack[v]=0;
Belong[v]=scnt;
}while(u!=v);
}
}
int main()
{
int i,j,k,u,v,ret;
memset(Head,-1,sizeof(Head));
scanf("%d",&n);
for(u=1;u<=n;u++)
{
scanf("%d",&k);
for(j=1;j<=k;j++)
{
scanf("%d",&v);
v+=n;
add(u,v);
}
}
for(u=1;u<=n;u++)
{
scanf("%d",&v);
v+=n;
add(v,u);
}
for(i=1;i<=n;i++)
if(!DFN[i])
Tarjan(i);
for(u=1;u<=n;u++)
{
ret=0;
for(i=Head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(Belong[u]==Belong[v])
ans[++ret]=v-n;
}
sort(ans+1,ans+1+ret);
printf("%d",ret);
for(i=1;i<=ret;i++)
printf(" %d",ans[i]);
printf("\n");
}
}