[POJ1904] King's Quest 強連通分量

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.

Source

Northeastern Europe 2003

 

思維難度:NOIP+

代碼難度:NOIP+

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#define ll long long
using namespace std;
const int Maxn=4005;
struct node{
	int v,next;
}e[Maxn*Maxn*2];
int cnt,n,N,h[Maxn],dfn[Maxn],ti,low[Maxn],ins[Maxn],col[Maxn],fir[Maxn],now[Maxn],mat[Maxn],vis[Maxn],ans[Maxn];
bool ok[Maxn][Maxn];
stack<int>Q;
inline int mn(int x,int y){
	return x<y?x:y; 
} 
inline int mx(int x,int y){
	return x>y?x:y;
}
void add(int u,int v){
	cnt++;
	e[cnt].v=v;
	e[cnt].next=h[u];
	h[u]=cnt;
}
void dfs(int u){
	dfn[u]=low[u]=++ti;
	ins[u]=1;
	Q.push(u);
	for(int i=h[u];i;i=e[i].next){
		int v=e[i].v;
		if(dfn[v]==0){
			dfs(v);
			low[u]=mn(low[u],low[v]);
		}
		else{
			if(ins[v]){
				low[u]=mn(low[u],dfn[v]);
			}
		}
	}
	if(low[u]==dfn[u]){
		int i;
		do{
			i=Q.top();
			Q.pop();
			col[i]=u;
			ins[i]=0;
		}while(i!=u);
	}
}
int next(int u){
	if(fir[u]==0){
		fir[u]=1;
		return h[u];
	}
	return e[now[u]].next;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int k,v;
		scanf("%d",&k);
		for(int j=1;j<=k;j++){
			scanf("%d",&v);
			add(i,v+n);
			N=mx(N,v+n);
			ok[i][v+n]=1;
		}
	}
	for(int i=1;i<=n;i++){
		int v;scanf("%d",&v);
		add(v+n,i);
	}
	for(int i=1;i<=n;i++){
		if(!dfn[i])dfs(i);
	}
	for(int i=1;i<=n;i++){
		int tot=0;
		for(int j=n+1;j<=N;j++){
			if(col[i]==col[j]&&ok[i][j]){
				ans[++tot]=j;
			}
		}
		printf("%d ",tot);
		sort(ans+1,ans+1+tot);
		for(int j=1;j<=tot;j++){
			printf("%d ",ans[j]-n);
		}
		printf("\n");
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章