POJ 1904 King's Quest tarjan

King's Quest

题目连接:

http://poj.org/problem?id=1904

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

题意

每个王子喜欢ki个公主,现在把每个王子喜欢的公主数量都给了出来

现在大臣公布了一个表,表示这个网址和这个公主结婚,但是国王并不满意这个表

于是叫大臣重新制作一张表,输出每个王子和这个公主结婚之后,满足不会影响别人结婚的条件

题解:

题目已经给了你一个完全匹配了,我们按照完全匹配所给的,建立反边

这样我们跑一遍tarjan之后,我们就可以发现,在同一个强连通内的点关系,可以互换的

就像匈牙利的match暴力找下一个一样,只要在一个强连通,那么就一定可以使得这个强连通的人都满足条件。

这样我们就可以做了

代码

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 5e3+6;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,k;
int dfn[maxn],low[maxn],_clock=0;
int sta[maxn],top;
bool in_sta[maxn];
int changed[maxn],scc,num[maxn];
vector<int> E[maxn];
void tarjan(int x)
{
dfn[x]=low[x]=++_clock;
sta[++top]=x;
in_sta[x]=1;
for(int i=0;i<E[x].size();i++)
{
int v = E[x][i];
if(!dfn[v])
tarjan(v),low[x]=min(low[x],low[v]);
else if(in_sta[v])
low[x]=min(low[x],dfn[v]);
}
if(dfn[x]==low[x])
{
int temp;
++scc;
do{
temp = sta[top--];
in_sta[temp]=0;
changed[temp]=scc;
++num[scc];
}while(temp!=x);
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
for(int j=1;j<=k;j++)
{
int x=read();
E[i].push_back(x+n);
}
}
for(int i=1;i<=n;i++)
{
int x=read();
E[x+n].push_back(i);
}
for(int i=1;i<=2*n;i++)
if(!dfn[i])tarjan(i);
for(int i=1;i<=n;i++)
{
vector<int> ans;
for(int j=0;j<E[i].size();j++)
if(changed[i]==changed[E[i][j]])
ans.push_back(E[i][j]-n);
sort(ans.begin(),ans.end());
printf("%d",ans.size());
for(int j=0;j<ans.size();j++)
printf(" %d",ans[j]);
printf("\n");
}
}
上一篇:(2)hashlib模块(加密算法模块)


下一篇:[poj 1904]King's Quest[Tarjan强连通分量]