【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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章