【C++】「Northeastern_Europe-2003」King s Quest

【来源】

Northeastern_Europe-2003
POJ-1904
OpenJ_Bailian-1904
UVA-1327
LA-2966
ZOJ-2470
vjudge

某些OJ题目大意相同,但输入输出格式不同。本题代码以ZOJ为准。

【题目描述】

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.

【输入格式】

The input consists of several test cases. The first line of each case 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 200 000.

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.

【输出格式】

For each test case 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 non-decreasing order. Print a blank line after each test case.

【样例输出】

4
2 1 2
2 1 2
2 2 3
2 3 4
1 2 3 4

【样例输出】

2 1 2
2 1 2
1 3
1 4

【题目大意】

有n个王子,每个王子都有k个喜欢的姑娘,每个王子只能和喜欢的姑娘结婚,大臣给出一个匹配表,每个王子都和一个姑娘结婚,但是国王不满意,他要求大臣给他另一个表,每个王子可以和几个姑娘结婚,按序号升序输出姑娘的编号,这个表应满足所有的王子最终都有姑娘和他结婚。

【解析】

匹配思想与强连通分量的转化。

建图时,王子u喜欢女孩v,则u到v建一条边。
对于给出的初始完美匹配,王子u与姑娘v匹配,则v到u连一条边。然后求scc。
显然对于同一个scc中王子数目和女孩数目是相等的,并且从某个王子出发能够到达所有女孩。
这样,所以一个王子可以和所有与他同一个scc的姑娘结婚,而这不会导致同一个scc中的其他王子找不到姑娘结婚。

【代码】

#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3,"Ofast","inline")

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>

#define RI                 register int
#define re(i,a,b)          for(RI i=a; i<=b; i++)
#define ms(i,a)            memset(a,i,sizeof(a))
#define MAX(a,b)           (((a)>(b)) ? (a):(b))
#define MIN(a,b)           (((a)<(b)) ? (a):(b))

using namespace std;

typedef long long LL;

const int N=2005;
const int inf=1e9;

struct Edge {
    
    
	int to,nt;
} edge[N*105];

int n,m,cnt,nedge,scc;
int h[N<<1],belong[N<<1],low[N<<1],dfn[N<<1];
bool like[N][N];

stack<int> s;
vector<int> d1,d2;
vector<int> e[N<<1],ans[N];

inline void add(int a,int b) {
    
    
	edge[nedge].to=b;
	edge[nedge].nt=h[a];
	h[a]=nedge++;
}

void dfs(int k) {
    
    
	low[k]=dfn[k]=++cnt;
	s.push(k);
	for(int i=h[k]; i!=-1; i=edge[i].nt) {
    
    
		int v=edge[i].to;
		if(!dfn[v]) {
    
    
			dfs(v);
			low[k]=MIN(low[k],low[v]);
		} else if(!belong[v]) low[k]=MIN(low[k],dfn[v]);;
	}
	if(low[k]==dfn[k]) {
    
    
		scc++;
		for(;;) {
    
    
			int x=s.top();
			s.pop();
			belong[x]=scc;
			if(x==k) break;
		}
	}
}

int main() {
    
    
	while(scanf("%d",&n)!=EOF) {
    
    
		memset(h,-1,sizeof(h));
		memset(low,0,sizeof(low));
		memset(dfn,0,sizeof(dfn));
		memset(belong,0,sizeof(belong));
		memset(like,0,sizeof(like)); 
		cnt=scc=nedge=0;
		for(int i=1; i<=n; i++) {
    
    
			int k;
			scanf("%d",&k);
			for(int j=1; j<=k; j++) {
    
    
				int p;
				scanf("%d",&p);
				add(i,n+p);
				like[i][p]=1;
			}
		}
		for(int i=1; i<=n; i++) {
    
    
			int p;
			scanf("%d",&p);
			add(p+n,i);
		}
		for(int i=1; i<=n; i++) 
			if(!dfn[i]) dfs(i);
		for(int i=1; i<=scc; i++) e[i].clear();
		for(int i=1; i<=n; i++) ans[i].clear();
		for(int i=1; i<=(n<<1); i++) {
    
    
			int x=belong[i];
			e[x].push_back(i);
		}
		for(int i=1; i<=scc; i++) {
    
    
			d1.clear();
			d2.clear();
			for(int j=0; j<e[i].size(); j++) {
    
    
				int x=e[i][j];
				if(x<=n) d1.push_back(x);
					else d2.push_back(x);
			}
			sort(d2.begin(),d2.end());
			for(int j=0; j<d1.size(); j++) 
				for(int k=0; k<d2.size(); k++) 
					if(like[d1[j]][d2[k]-n])
						ans[d1[j]].push_back(d2[k]-n);
		}
		for(int i=1; i<=n; i++) {
    
    
			printf("%d",(int)ans[i].size());
			for(int j=0; j<ans[i].size(); j++) 
				printf(" %d",ans[i][j]);
			printf("\n");
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Ljnoit/article/details/106336741