POJ 1904 强连通

King's Quest
Time Limit: 15000MS Memory Limit: 65536K
Total Submissions: 3719 Accepted: 1251
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.

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

 

 

这个题看起来像是匹配,但是确是强连通的题目

 

Source Code

Problem: 1904 User: bingshen
Memory: 6576K Time: 5844MS
Language: C++ Result: Accepted
  • Source Code
    #include<stdio.h>
    #include<vector>
    #include<algorithm>
    #include<stack>
    #define maxn 4005
    
    using namespace std;
    
    vector<int>g[maxn];
    int belong[maxn],dfn[maxn],low[maxn];
    bool used[maxn],instack[maxn];
    bool net[2005][2005];
    stack<int>s;
    int n,m,cnt,index;
    
    void init()
    {
    	int i;
    	for(i=0;i<maxn;i++)
    		g[i].clear();
    	memset(belong,0,sizeof(belong));
    	memset(dfn,-1,sizeof(dfn));
    	memset(low,0,sizeof(low));
    	memset(used,0,sizeof(used));
    	memset(instack,0,sizeof(instack));
    	memset(net,0,sizeof(net));
    	cnt=0;
    	index=0;
    }
    
    int min(int a,int b)
    {
    	if(a>b)
    		return b;
    	else
    		return a;
    }
    
    void tarjan(int u)
    {
    	int i,v;
    	index++;
    	dfn[u]=index;
    	low[u]=index;
    	used[u]=true;
    	instack[u]=true;
    	s.push(u);
    	for(i=0;i<g[u].size();i++)
    	{
    		v=g[u][i];
    		if(!used[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])
    	{
    		cnt++;
    		do
    		{
    			v=s.top();
    			s.pop();
    			belong[v]=cnt;
    			instack[v]=false;
    		}
    		while(u!=v);
    	}
    }
    
    void slove()
    {
    	int i,j,size;
    	vector<int>temp[maxn];
    	for(i=1;i<=n;i++)
    	{
    		temp[i].clear();
    		for(j=0;j<g[i].size();j++)
    		{
    			if(belong[g[i][j]]==belong[i])
    				temp[i].push_back(g[i][j]-n);
    		}
    		sort(temp[i].begin(),temp[i].end());
    		size=temp[i].size();
    		printf("%d ",size);
    		for(j=0;j<size-1;j++)
    		{
    			printf("%d ",temp[i][j]);
    		}
    		printf("%d/n",temp[i][size-1]);
    	}
    }
    
    int main()
    {
    	int i,j,k,b;
    	init();
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		scanf("%d",&k);
    		for(j=1;j<=k;j++)
    		{
    			scanf("%d",&b);
    			g[i].push_back(b+n);
    		}
    	}
    	for(i=1;i<=n;i++)
    	{
    		scanf("%d",&b);
    		g[n+b].push_back(i);
    	}
    	for(i=1;i<=n;i++)
    	{
    		if(dfn[i]==-1)
    			tarjan(i);
    	}
    	slove();
    	return 0;
    }
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值