King's Quest
Time Limit: 15000MS | Memory Limit: 65536K | |
Total Submissions: 5953 | Accepted: 2074 | |
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
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.
Source
题意:给你N个王子和N个美女,事先知道每个王子喜欢那些美女,以及事先匹配好的婚姻,但是要求每个王子有可能匹配的美女,当然某个王子与他的那些有可能匹配的美女结婚后,还能保证其他王子都能有美女。
思路:很像二分匹配,但是求的的确不一样。可以那么想,如果王子i有a,b,c,d四个美女可以选,王子j只有d可以选,那么王子i只能舍弃d,很明显的,d只能和j在一起,所以我们可以从王子u到美女v连接一条(u,v+n)的有向边,然后对于最后规定的婚姻,从美女v到王子u连接一条(v+n,u)的有向边,求出所建图的连通分量,相同连通分量内的王子和美女可以随意匹配。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=4005;
const int maxe=204005;
struct Edge
{
int to,next;
} e[maxe];
int n;
int head[maxn];
bool instack[maxn];
int dfn[maxn],low[maxn],stap[maxn],match[maxn];
int belong[maxn],cnt,ccnt,stop,edge,girl[maxn],gnum;
inline void add(int u,int v)
{
e[edge].to=v,e[edge].next=head[u],head[u]=edge++;
}
void input()
{
int k,v;
stop=ccnt=cnt=edge=0;
memset(belong,0,sizeof(belong));
memset(dfn,0,sizeof(dfn));
memset(head,-1,sizeof(head));
for(int i=1; i<=n; i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d",&v);
add(i,v+n);
}
}
for(int i=1; i<=n; i++)
{
scanf("%d",&v);
add(v+n,i);
}
}
void dfs(int i)
{
int v,u=i;
dfn[u]=low[u]=++cnt;
instack[i]=true;
stap[stop++]=u;
for(int j=head[u]; ~j; j=e[j].next)
{
if(!dfn[v=e[j].to])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
ccnt++;
while(true)
{
v=stap[--stop];
belong[v]=ccnt;
instack[v]=false;
if(v==u) break;
}
}
}
void tarjan()
{
for(int i=1; i<=n; i++)
if(!dfn[i])
dfs(i);
}
void output()
{
int u,v;
for(int i=1; i<=n; i++)
{
gnum=0;
u=belong[i];
for(int j=head[i]; ~j; j=e[j].next)
if(belong[v=e[j].to]==u)
girl[gnum++]=v;
sort(girl,girl+gnum);
printf("%d",gnum);
for(int j=0; j<gnum; ++j)
printf(" %d",girl[j]-n);
printf("\n");
}
}
int main()
{
while(~scanf("%d",&n))
{
input();
tarjan();
output();
}
return 0;
}