POJ 1904 King ' s Quest (strong connectivity)

Source: Internet
Author: User
Tags integer numbers

Language:DefaultKing ' s Quest
Time Limit: 15000MS Memory Limit: 65536K
Total Submissions: 7635 Accepted: 2769
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 on each of his sons which of those girls he does like. The sons of the king were young and light-headed, so it is possible for a 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 is chosen, so that he liked this girl and, of C Ourse, 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 that you had 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 is able to choose the girl he likes to M Arry. "

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 the king's sons contain the list of the girls he likes:first Ki-the number of those girls, and th En Ki different integer numbers, ranging from 1 to N denoting the girls. The sum of all Ki does is 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, and the list is correct, which 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, after his marriage it I s possible to marry each of the other King ' s sons. After the print Li different integer numbers denoting those girls, in ascending order.

Sample Input

42 1 22 1 22 2 32 3 41 2 3 4

Sample Output

2 1 22 1 21 31 4

Hint

This problem have huge input and output data,use scanf () and printf () instead of CIN and cout to read data to avoid time Li MIT Exceed.

Source

Northeastern Europe 2003


Reference Link: http://blog.csdn.net/l04205613/article/details/6654820


Test instructions is, n boys and N Girls, tells you each boy's favorite girl number, and then gives an initial match (this initial match is a complete match), and then asks for all possible complete matches, in ascending order. Of course, if it's a riot (I haven't tried it, of course), 2000 Boys and 2000 girls, up to 20W with a straight edge.

Read a report from a god cow and turn this into a strong connectivity problem:

First, according to the given side of the building map, and then according to the final complete match in the graph to add the opposite edge (that is, according to the perfect match with girls to boys side), then in this diagram, belong to the same strong connected point pair must be a legitimate point pair. Sort them out.


Because boys do not love boys, so if it is strong connectivity, then the boys must love this strong connected component of all the girls


#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8typedef __ Int64 ll; #define FRE (i,a,b) for (i = A; I <b; i++) #define FREE (i,b,a) for (i = b; I >= a;i--) #define MEM (T, v) MEMS ET ((t), V, sizeof (t)) #define SSF (n) scanf ("%s", N) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf (          "%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF Printf#define Bug PF ("hi\n") using namespace std, #define INF 0x3f3f3f3f#define n 20000int ans[n],time_num,time[n],low[n],type[n],cnt ; int instack[n];int n;vector<int>g[n];stack<int>q;void Tarjan (int x) {int i,j;time[x]=low[x]=++time_num; Instack[x]=1;q.push (x); Fre (I,0,g[x].size ()) {int to=g[x][i];if (time[to]==0) {Tarjan (To), if (Low[to]<low[x]) low[x]=low[to];}    else if (Instack[to]&&low[x]>low[to]) low[x]=low[to];}    int to;     if (Time[x]==low[x]) {cnt++; Do{to=q.top (); Q.pop (); type[to]=cnt; instack[to]=0; }while (to!=x);}} void Solve () {int i,j;mem (time,0); mem (low,0); mem (instack,0); Time_num=0;int k;while (!q.empty ()) Q.pop (); mem (type,0); Cnt=0;fre (i,1,n*2+1) if (time[i]==0) Tarjan (i); Fre (i,1,n+1) {K=0;fre (J,0,g[i].size ()) if (Type[i]==type[g[i][j]]) ans    [K++]=g[i][j]-n;    Sort (ans,ans+k);    PF ("%d", k);    Fre (j,0,k) pf ("%d", ans[j]); PF ("\ n");}} int main () {int i,j;while (~SF (n)) {fre (i,1,n+n+1) g[i].clear (); int K,x;fre (i,1,n+1) {SF (k); while (k--) {SF (x); G[i].push   _back (N+X);        Boys Love Girls}} fre (i,1,n+1) {SF (x);     G[x+n].push_back (i);    Girls Love boys, if this kind of love relationship is a strong connection, then the boys can choose the girls inside} solve ();} return 0;}


POJ 1904 King ' s Quest (strong connectivity)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.