POJ - 1904 King's Quest (Tarjan+完美匹配)

本文介绍了一种使用Tarjan算法求解强连通分量的方法来解决复杂的婚姻配对问题。该问题涉及到国王的儿子们与王国中的美丽女子之间的配对,确保每位儿子能够与自己喜欢的女孩结婚的同时,每个女孩也只能嫁给一位王子。通过建立合适的图模型,并应用Tarjan算法,我们能够找出所有可能的配对方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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("\n");
		}
	}
	
	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("\n");
		}
	}
	
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值