King's Quest
Time Limit: 15000MS | Memory Limit: 65536K | |
Total Submissions: 9753 | Accepted: 3591 | |
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
思路:首先建图,如果王子u喜欢女生v,则建一条边u指向v,对于大臣给出的初始完美匹配,如果王子u和女生v结婚,则建一条边v指向u然后求强连通分量。对于每个王子和女生,如果他们都在同一个强连通分量内,则他们可以结婚。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int N=4010;
const int inf=0x3f3f3f3f;
int dir[2][2]={1,0,0,1};
int dfn[N],low[N],belong[N];
bool instack[N];
int w[N];
int sw[N];
int head[N],hh[N];
int vis[N];
int dis[N];
int in[N],out[N];
int cnt,index,num;
vector<int>g[N];
stack<int>s;
int m,n;
void init()
{
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(belong,0,sizeof(belong));
memset(instack,false,sizeof(instack));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
num=cnt=index=0;
while(!s.empty())
s.pop();
for(int i=0;i<=2*n;i++)
{
g[i].clear();
}
}
void tarjan(int u)
{
dfn[u]=low[u]=++index;
instack[u]=true;
s.push(u);
int v;
for(int i=0;i<g[u].size();i++)
{
v=g[u][i];
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[v],low[u]);
}
else if(instack[v])
{
low[u]=min(dfn[v],low[u]);
}
}
if(dfn[u]==low[u])
{
cnt++;
do{
v=s.top();
s.pop();
belong[v]=cnt;
instack[v]=false;
}while(u!=v);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int i,j,k;
init();
int a,b;
for(i=1;i<=n;i++)
{
scanf("%d",&k);
for(j=1;j<=k;j++)
{
scanf("%d",&a);
g[i].push_back(a+n);
}
}
for(i=1;i<=n;i++)
{
scanf("%d",&a);
g[a+n].push_back(i);
}
for(i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i);
}
for(i=1;i<=n;i++)
{
memset(in,0,sizeof(in));
k=0;
for(j=0;j<g[i].size();j++)
{
int v=g[i][j];
if(belong[v]==belong[i])
{
in[k++]=v;
}
}
sort(in,in+k);
printf("%d",k);
for(j=0;j<k;j++)
{
printf(" %d",in[j]-n);
}
printf("\n");
}
}
return 0;
}