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 coshould marry her. and the king's wizard did it -- for each son the girl that he coshould 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 wowould 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 wowould 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
4. There are n princes and n beautiful women. A prince may like several beautiful women, but a beautiful woman can only marry one prince, A prince can only marry a beauty he or she likes. Q: for every prince, which of the following beautiful women can he marry and other princes can marry their favorite beauty. Solution: This question looks like a two-part diagram at the beginning, but I cannot find a solution based on this idea. It should be useful to think about a perfect match provided by the last line. Let's talk about how to create a graph: connect each Prince u to his favorite beauty v with a directed edge <u, v + n> (to avoid confusion of vertex sequence numbers, using the serial number + n of the beauty as the serial number of the beauty vertex), and finally, using a perfect match given in the input, connect the beauty v to the current husband u with a directed edge <v + n, u>. Then, use tarjan to calculate the strongly connected component. The example in the question is shown in the figure below. The black side represents the prince's beauty, and the red side represents a perfect match given in the input:
It can be seen that the number of vertices in each strongly connected component is an even number. Each prince can marry a beauty in the same strongly connected component as he or she prefers. For example, if 1 can marry 6, then 2 will get married to 5; if 1 and 5 get married, 2 will get married to 6; both methods do not affect 3 and 4. See the Code:
<SPAN style = "FONT-SIZE: 18px "> # include <iostream> # include <cstring> # include <string> # include <cmath> # include <cstdio> # include <queue> # include <algorithm> # include <set> # include <vector> # define mem (, b) memset (a, B, sizeof (a) using namespace std; const int MAXN = 4005; vector <int> vert [MAXN]; // vertex vector <int> fz [MAXN]; // records the vertex int ans [MAXN]; int n, m; bool vis [MAXN] in each strongly connected component. int dfn [MAXN]; int low [MAXN ]; Int id [MAXN]; int stap [MAXN]; int top; bool inq [MAXN]; int tmpdfn; int sumf; // record the number of strongly connected components bool bi [MAXN]; inline void RD (int & a) {a = 0; char t; do {t = getchar ();} while (t <'0' | t> '9'); a = t-'0'; while (t = getchar ()> = '0' & t <= '9') a = a * 10 + (t-'0');} inline void OT (int) {if (a> = 10) OT (a/10); putchar (a % 10 + '0');} void clr () {mem (vis, 0 ); mem (dfn, 0); mem (lo W, 0); mem (fz, 0); mem (inq, 0); mem (stap,-1); mem (id,-1); top =-1; tmpdfn = 0; sumf = 0; int I; for (I = 0; I <= n * 2; I ++) {vert [I]. clear (); fz [I]. clear () ;}} void tarjan (int u) {vis [u] = 1; dfn [u] = low [u] = ++ tmpdfn; stap [++ top] = u; inq [u] = true; int I; for (I = 0; I <vert [u]. size (); I ++) {int v = vert [u] [I]; if (! Vis [v]) {tarjan (v); low [u] = min (low [u], low [v]);} else if (inq [v]) low [u] = min (low [u], dfn [v]);} if (dfn [u] = low [u]) {int tmp; sumf ++; do {tmp = stap [top --]; inq [tmp] = false; id [tmp] = sumf; fz [sumf]. push_back (tmp);} while (tmp! = U) ;}} void init () {clr (); int I; int j; int B; for (I = 1; I <= n; I ++) {int k; RD (k); for (j = 0; j <k; j ++) {RD (B); vert [I]. push_back (B + n) ;}}for (I = 1; I <= n; I ++) {int B; RD (B); vert [B + n]. push_back (I) ;}} void solve () {int I; for (I = 1; I <= n; I ++) {if (! Vis [I]) {tarjan (I) ;}} int j; int k; for (I = 1; I <= n; I ++) {int x = id [I]; mem (bi, 0); int tp; for (j = 0; j <fz [x]. size (); j ++) {tp = fz [x] [j]; bi [tp] = true;} int p = 0; for (j = 0; j <vert [I]. size (); j ++) {tp = vert [I] [j]; if (bi [tp]) ans [p ++] = tp;} sort (ans, ans + p); OT (p); if (p> 0) putchar (''); for (j = 0; j <p; j ++) {OT (ans [j]-n); if (j <p-1) put Char ('');} putchar ('\ n') ;}} int main () {while (scanf (" % d ", & n )! = EOF) {getchar (); init (); solve () ;}return 0 ;}</SPAN>