• poj--1904--King's Quest(scc建图)


    Time Limit: 15000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

    Status

    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.

    有n个王子n个女生,输入时第一个数字是n,然后n行,每一行第一个数字表示该王子喜欢几个人,然后接这几个女孩的编号,第n+1行表示第i个女孩喜欢ni王子

    将女孩虚拟为n+i节点,建图时i--j+n表示i王子喜欢女孩j,因为男女编号有重复,所以j+n,j+n--i表示j喜欢i,找出每一个scc里的点判断就好

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<algorithm>
    using namespace std;
    #define MAX 5000
    int low[MAX],dfn[MAX];
    int head[MAX],sccno[MAX],scc_cnt,dfs_clock;
    int cnt,m,n,map[MAX][MAX],rec[MAX];
    bool Instack[MAX];
    stack<int>s;
    vector<int>scc[MAX];
    struct node
    {
    	int u,v;
    	int next;
    }edge[MAX*100];
    void init()
    {
    	cnt=0;
    	memset(head,-1,sizeof(head));
    }
    void add(int u,int v)
    {
    	edge[cnt].u=u;
    	edge[cnt].v=v;
    	edge[cnt].next=head[u];
    	head[u]=cnt++;
    }
    void getmap()
    {
    	int num,a;
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%d",&num);
    		while(num--)
    		{
    			scanf("%d",&a);
    			add(i,a+n);
    			map[i][a]=1;
    		}
    	}
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%d",&a);
    		add(a+n,i);
    	}
    }
    void tarjan(int u,int fa)
    {
    	int v;
    	low[u]=dfn[u]=++dfs_clock;
    	s.push(u);
    	Instack[u]=true;
    	for(int i=head[u];i!=-1;i=edge[i].next)
    	{
    		v=edge[i].v;
    		if(!dfn[v])
    		{
    			tarjan(v,u);
    			low[u]=min(low[u],low[v]);
    		}
    		else if(Instack[v])
    		low[u]=min(low[u],dfn[v]);
    	}
    	if(low[u]==dfn[u])
    	{
    		++scc_cnt;
    		for(;;)
    		{
    			v=s.top();
    			s.pop();
    			Instack[v]=false;
    			sccno[v]=scc_cnt;
    			scc[scc_cnt].push_back(v);
    			if(v==u) break;
    		}
    	}
    }
    void find(int l,int r)
    {
    	memset(low,0,sizeof(low));
    	memset(dfn,0,sizeof(dfn));
    	memset(Instack,false,sizeof(Instack));
    	memset(sccno,0,sizeof(sccno));
    	dfs_clock=scc_cnt=0;
    	for(int i=l;i<=r;i++)
    	if(!dfn[i])
    	tarjan(i,-1);
    }
    void slove()
    {
    	for(int i=1;i<=n;i++)
    	{
    		int u=sccno[i];
    		int sum=0;
    		for(int j=0;j<scc[u].size();j++)
    		{
    			if(scc[u][j]>n&&map[i][scc[u][j]-n])
    			rec[sum++]=scc[u][j]-n;
    		}
    		sort(rec,rec+sum);
    		printf("%d",sum);
    		for(int j=0;j<sum;j++)
    		printf(" %d",rec[j]);
    		printf("
    ");
    	}
    }
    int main()
    {
    	while(scanf("%d",&n)!=EOF)
    	{
    		init();
    		getmap();
    		find(1,2*n);
    		slove();
    	}
    	return 0;
    }


  • 相关阅读:
    TransactionScop事务机制的使用
    MVC无刷新上传图片并显示
    WebClient和WebRequest获取html代码
    Web.config配置详解
    分类和扩展有什么区别?可以分别用来做什么?分类有哪些局限性?分类的结构体里面有哪些成员?
    有序二维数组的查找
    生成Excel.xlsx文件 iOS
    charles Https抓包
    https 通信流程和Charles 抓包原理
    fastlane自动化打包ipa并发布到firim或者蒲公英
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273656.html
Copyright © 2020-2023  润新知