[题解] poj 1904 King's Quest (tarjan强连通分量)

- 传送门 -

 http://poj.org/problem?id=1904

#King's Quest

| Time Limit: 15000MS |   | Memory Limit: 65536K |
| Total Submissions: 9328 |   | Accepted: 3441 |
| Case Time Limit: 2000MS |

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.

[Submit]   [Status]   [Discuss]

 

- 题意 -

 一个糟糕的国家的 n 个糟糕王子, 每个王子都喜欢一群姑娘, 每个王子也有巫师指定的原配(一定是他喜欢的之一), 姑娘一共有 n 个, 王子不一定要娶原配, 请给出每个王子能娶的姑娘的名单, 要求是: 名单上的姑娘一定是王子喜欢的, 无论王子娶自己名单上的谁, 其他王子都一定能娶到一个自己喜欢的姑娘.
 举个栗子, 王子 1 喜欢姑娘1, 2, 王子 2 喜欢姑娘 2 , 那么王子 1 就不能娶姑娘 2 , 否则王子 2 就将面临孤独终老了.
 

- 思路 -

 将王子对其喜欢的姑娘连边, 姑娘对原配王子连边.
 tarjan找强连通分量.
 同一强连通分量中的姑娘王子随便配对.
 但注意同一强连通分量中的姑娘王子不一定喜欢.
 
 细节见代码.
 
 PS:
 1. 打死我也想不到这题可以强连通搞啊...
 2. 听说这题可以快速读入输出快如闪电???
  

- 代码 -

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

const int N = 4000 + 5;
const int M = 4002000 + 5;

int STK[N];
int TO[M], NXT[M], HD[M];
int DFN[N], LOW[N], BL[N];
int n, m, cnt, sz, tot, tp;

vector<int> ANS[N];

void add(int x, int y) {
	TO[++sz] = y; NXT[sz] = HD[x]; HD[x] = sz;
}

void tarjan(int x) {
	int v;
	DFN[x] = LOW[x] = ++tot;
	STK[++tp] = x;
	for (int i = HD[x]; i; i = NXT[i]) {
		v = TO[i];
		if (!DFN[v]) { tarjan(v); LOW[x] = min(LOW[x], LOW[v]); }
		else if (!BL[v]) LOW[x] = min(LOW[x], DFN[v]);
	}
	if (DFN[x] == LOW[x]) {
		cnt ++;
		do {
			v = STK[tp--];
			BL[v] = cnt;
		}while (v != x);
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1, x, a; i <= n; ++i) {
		scanf("%d", &x);
		while (x--) {
			scanf("%d", &a);
			add(i, a + n);
		}
	}
	for (int i = 1, x, a; i <= n; ++i) {
		scanf("%d", &x);
		add(x + n, i);
	}
	for (int i = 1, x, a; i <= (n << 1); ++i)
		if (!DFN[i]) tarjan(i);
	for (int i = 1; i <= n; ++i) {
		for (int j = HD[i]; j; j = NXT[j]) {
			int v = TO[j];
			if (BL[i] == BL[v]) ANS[i].push_back(v);
		}
		sort(ANS[i].begin(), ANS[i].end());
		int len = ANS[i].size();
		printf("%d", len);
		for (int j = 0; j < len; ++j)
			printf(" %d", ANS[i][j] - n);
		printf("
");
	}
	return 0;
}

原文地址:https://www.cnblogs.com/Anding-16/p/7350730.html