zoukankan      html  css  js  c++  java
  • POJ

    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.

    题解:

    这题主要是建图建对就好做了。根据王子的喜好建立王子到公主的单向边,再根据最后给出的完美匹配结果建立公主到王子的单向边,然后跑一边Tarjan,然后跟王子在一个强联通分量里的公主都能娶。具体的可以看这位大佬:https://www.cnblogs.com/frog112111/p/3384261.html

    代码:

    不加输入输出外挂:11094ms

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    const int MAXN = 4005;
    
    bool Map[MAXN][MAXN];
    bool is_instack[MAXN];//记录节点是否在栈中 
    int stack[MAXN],top;
    int DFN[MAXN];//记录节点第一次被访问时的时间
    int LOW[MAXN];//记录节点与节点的子树节点中最早的步数 
    int time; 
    int Belong[MAXN];//记录每个节点属于的强连通分量编号 
    int N,cnt;//N是点数M是边数,cnt是强连通分量编号。
    
    int Tarjan(int x){
        DFN[x] = LOW[x] = ++time;
        is_instack[x] = true;
        stack[++top] = x;
        for(int i=1 ; i<=N*2 ; ++i){
            if(Map[x][i] == false)continue;
            if(!DFN[i]){
                Tarjan(i);
                if(LOW[i]<LOW[x]){
                    LOW[x] = LOW[i];
                }
            }
            else if(is_instack[i]){ //这里注意不能直接else,因为DFN[i]!=0还有可能是横叉边。
                 LOW[x] = min(LOW[x],DFN[i]);
            }
        }
        if(DFN[x] == LOW[x]){
            ++cnt;
            int mid;
            do{
                mid = stack[top--];
                is_instack[mid] = false;
                Belong[mid] = cnt;
            }while(mid != x);
        }
    }
    
    inline void init(){
    	memset(Map,false,sizeof Map);
    	memset(DFN,0,sizeof(DFN));
    	memset(LOW,0,sizeof(LOW));
    	memset(is_instack,false,sizeof is_instack);
    	top = time = cnt = 0;
    }
    
    int main(){
    	
    	while(scanf("%d",&N)!=EOF){
    		int K;
    		init();
    		for(int i=1 ; i<=N ; ++i){
    			scanf("%d",&K);
    			int t;
    			while(K--){
    				scanf("%d",&t);
    				Map[i][t+N] = true;
    			}
    		}
    		for(int i=1 ; i<=N ; ++i){
    			int t;
    			scanf("%d",&t);
    			Map[t+N][i] = true;
    		}
    		for(int i=1 ; i<=N ; ++i){
    			if(!DFN[i])Tarjan(i);
    		}
    		for(int i=1 ; i<=N ; ++i){
    			int sum = 0;
    			for(int j=N+1 ; j<=N*2 ; ++j){
    				if(Map[i][j]){
    					if(Belong[i] == Belong[j])++sum;
    				}
    			}
    			printf("%d",sum);
    			for(int j=N+1 ; j<=N*2 ; ++j){
    				if(Map[i][j]){
    					if(Belong[i] == Belong[j])printf(" %d",j-N);
    				}
    			}
    			printf("
    ");
    		}
    	}
    	
    	return 0;
    } 

    加输入输出外挂:7688ms

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    const int MAXN = 4005;
    
    bool Map[MAXN][MAXN];
    bool is_instack[MAXN];//记录节点是否在栈中 
    int stack[MAXN],top;
    int DFN[MAXN];//记录节点第一次被访问时的时间
    int LOW[MAXN];//记录节点与节点的子树节点中最早的步数 
    int time; 
    int Belong[MAXN];//记录每个节点属于的强连通分量编号 
    int N,cnt;//N是点数M是边数,cnt是强连通分量编号。
    
    int Tarjan(int x){
        DFN[x] = LOW[x] = ++time;
        is_instack[x] = true;
        stack[++top] = x;
        for(int i=1 ; i<=N*2 ; ++i){
            if(Map[x][i] == false)continue;
            if(!DFN[i]){
                Tarjan(i);
                if(LOW[i]<LOW[x]){
                    LOW[x] = LOW[i];
                }
            }
            else if(is_instack[i]){ //这里注意不能直接else,因为DFN[i]!=0还有可能是横叉边。
                 LOW[x] = min(LOW[x],DFN[i]);
            }
        }
        if(DFN[x] == LOW[x]){
            ++cnt;
            int mid;
            do{
                mid = stack[top--];
                is_instack[mid] = false;
                Belong[mid] = cnt;
            }while(mid != x);
        }
    }
    
    inline void init(){
    	memset(Map,false,sizeof Map);
    	memset(DFN,0,sizeof(DFN));
    	memset(LOW,0,sizeof(LOW));
    	memset(is_instack,false,sizeof is_instack);
    	top = time = cnt = 0;
    }
    
    void Out(int a)    //输出外挂
    {
        if(a>9)
            Out(a/10);
        putchar(a%10+'0');
    }
    
    int Scan()     //输入外挂
    {
        int res=0,ch,flag=0;
        if((ch=getchar())=='-')
            flag=1;
        else if(ch>='0'&&ch<='9')
            res=ch-'0';
        while((ch=getchar())>='0'&&ch<='9')
            res=res*10+ch-'0';
        return flag?-res:res;
    }
    
    int main(){
    	
    	while(scanf("%d",&N)!=EOF){
    		int K;
    		init();
    		for(int i=1 ; i<=N ; ++i){
    			//scanf("%d",&K);
    			K = Scan();
    			int t;
    			while(K--){
    				//scanf("%d",&t);
    				t = Scan();
    				Map[i][t+N] = true;
    			}
    		}
    		for(int i=1 ; i<=N ; ++i){
    			int t;
    			//scanf("%d",&t);
    			t = Scan();
    			Map[t+N][i] = true;
    		}
    		for(int i=1 ; i<=N ; ++i){
    			if(!DFN[i])Tarjan(i);
    		}
    		for(int i=1 ; i<=N ; ++i){
    			int sum = 0;
    			for(int j=N+1 ; j<=N*2 ; ++j){
    				if(Map[i][j]){
    					if(Belong[i] == Belong[j])++sum;
    				}
    			}
    			//printf("%d",sum);
    			Out(sum);
    			for(int j=N+1 ; j<=N*2 ; ++j){
    				if(Map[i][j]){
    					if(Belong[i] == Belong[j])printf(" "),Out(j-N);
    				}
    			}
    			printf("
    ");
    		}
    	}
    	
    	return 0;
    } 
  • 相关阅读:
    TCP四次握手断开连接(十一)
    Go-函数
    Go-数据类型以及变量,常量
    GO语言介绍以及开发环境配置
    Socket与WebSocket以及http与https重新总结
    希尔排序
    第19课
    第18课
    外传篇3 动态内存申请的结果
    外传篇2 函数的异常规格说明
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514053.html
Copyright © 2011-2022 走看看