poj 1904

 

King's Quest
Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 3866   Accepted: 1289
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.

Source

Northeastern Europe 2003
分析:真的是强连通??我看着不像啊。。。拿来练强连通。。。参照大牛们的题解受益匪浅
//Kosaraju()
#include<algorithm> #include<iostream> #include <vector> using namespace std; const int maxn=4001; vector<int>map[maxn],rmap[maxn]; int t[maxn],belong[maxn],n,gg=0; bool flag[maxn]; void DFS1(int cur) { flag[cur]=0; for(int i=0;i<map[cur].size();++i) if(flag[map[cur][i]])DFS1(map[cur][i]); t[++t[0]]=cur; } void DFS2(int cur) { flag[cur]=0; belong[cur]=gg; for(int i=0;i<rmap[cur].size();++i) if(flag[rmap[cur][i]])DFS2(rmap[cur][i]); } void Kosaraju() { t[0]=0; memset(flag,1,sizeof(flag)); for(int i=1;i<=n;++i) if(flag[i])DFS1(i); memset(flag,1,sizeof(flag)); for(int i=t[0];i>0;--i) if(flag[t[i]])++gg,DFS2(t[i]); } int main() { //freopen("car.in","r",stdin); //freopen("car.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;++i) { int m; scanf("%d",&m); for(int j=0;j<m;++j) { int l; scanf("%d",&l); l+=n; map[i].push_back(l); rmap[l].push_back(i); } } for(int i=1;i<=n;++i) { int j; scanf("%d",&j); map[j+n].push_back(i); rmap[i].push_back(j+n); } Kosaraju(); for(int i=1;i<=n;++i) { int nt=0; for(int j=0;j<map[i].size();++j) if(belong[i]==belong[map[i][j]])t[nt++]=map[i][j]-n; sort(t,t+nt); printf("%d",nt); for(int j=0;j<nt;++j)printf(" %d",t[j]); putchar('/n'); } return 0; } 
//Tarjan()
#include<algorithm> #include<iostream> #include <vector> using namespace std; const int maxn=4001; const char NOTVIS = 0x00; const char VIS = 0x01; const char OVER = 0x02; vector<int>map[maxn]; int t[maxn],belong[maxn],stck[maxn],mlik[maxn],n,gg=0; char flag[maxn]; void DFS(int cur,int &sig,int &scc_num) { int i; stck[++stck[0]]=cur; flag[cur]=VIS; mlik[cur]=t[cur]=++sig; for(i=0;i<map[cur].size();++i) { if(NOTVIS==flag[map[cur][i]]) { DFS(map[cur][i],sig,scc_num); if(mlik[cur]>mlik[map[cur][i]]) mlik[cur]=mlik[map[cur][i]]; } else if(VIS==flag[map[cur][i]]) { if(mlik[cur]>t[map[cur][i]]) mlik[cur]=t[map[cur][i]]; } } if(mlik[cur]==t[cur]) { ++scc_num; do { belong[stck[stck[0]]]=scc_num; flag[stck[stck[0]]]=OVER; } while(stck[stck[0]--]!=cur); } } void Tarjan() { int i,sig,scc_num; memset(flag,NOTVIS,sizeof(flag)); sig=0;scc_num=0;stck[0]=0; for(i=1;i<=n;++i) if(NOTVIS==flag[i])DFS(i,sig,scc_num); } int main() { //freopen("car.in","r",stdin); //freopen("car.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;++i) { int m; scanf("%d",&m); for(int j=0;j<m;++j) { int l; scanf("%d",&l); l+=n; map[i].push_back(l); } } for(int i=1;i<=n;++i) { int j; scanf("%d",&j); map[j+n].push_back(i); } Tarjan(); for(int i=1;i<=n;++i) { int nt=0; for(int j=0;j<map[i].size();++j) if(belong[i]==belong[map[i][j]])t[nt++]=map[i][j]-n; sort(t,t+nt); printf("%d",nt); for(int j=0;j<nt;++j)printf(" %d",t[j]); putchar('/n'); } return 0; } 
//Gabow()
#include<stdio.h> #include<string.h> #include<algorithm> #include <vector> using namespace std; const int maxn=4001; vector<int>map[maxn]; int t[maxn],belong[maxn],stk1[maxn],stk2[maxn],n,gg=0,sig=0; void DFS(int cur) { t[cur]=++sig; stk1[++stk1[0]]=cur; stk2[++stk2[0]]=cur; for(int i=0;i<map[cur].size();++i) if(!t[map[cur][i]]) DFS(map[cur][i]); else if(!belong[map[cur][i]]) while(t[stk2[stk2[0]]]>t[map[cur][i]])--stk2[0]; if(stk2[stk2[0]]==cur) { --stk2[0];++gg; do { belong[stk1[stk1[0]]]=gg; } while(stk1[stk1[0]--]!=cur); } } int Gabow() { memset(belong,0,sizeof(belong)); memset(t,0,sizeof(t)); stk1[0]=0;stk2[0]=0; for(int i=1;i<=n;++i) if(!t[i])DFS(i); } int in() { char ch; int a=0; while((ch=getchar())==' ' || ch=='/n'); a*=10; a+=ch-'0'; while((ch=getchar())!=' ' && ch!='/n') { a*=10;a+=ch-'0'; } return a; } void out(int a) { if(a>=10)out(a/10); putchar(a%10+'0'); } int main() { //freopen("car.in","r",stdin); //freopen("car.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;++i) { int m; m=in(); for(int j=0;j<m;++j) { int l; l=in(); l+=n; map[i].push_back(l); } } for(int i=1;i<=n;++i) { int j; j=in(); map[j+n].push_back(i); } Gabow(); for(int i=1;i<=n;++i) { int nt=0; for(int j=0;j<map[i].size();++j) if(belong[i]==belong[map[i][j]])t[nt++]=map[i][j]-n; sort(t,t+nt); out(nt); for(int j=0;j<nt;++j) { putchar(' '); out(t[j]); } putchar('/n'); } return 0; } 
//大牛的代码
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; struct node{ int x,y,next; }; int e,n,res; int head[5000],co[5000],father[5000],link[5000],order[5000]; node p[1000000]; int in(){ char ch; int a=0; while((ch=getchar())==' ' || ch=='/n'); a*=10; a+=ch-'0'; while((ch=getchar())!=' ' && ch!='/n'){ a*=10;a+=ch-'0'; } return a; } void out(int a){ if(a>=10)out(a/10); putchar(a%10+'0'); } void addedge(int x,int y){ p[e].x=x;p[e].y=y;p[e].next=head[x]; head[x]=e++; } void init(){ int num,x; e=0; memset(head,-1,sizeof(head)); memset(link,0,sizeof(link)); memset(order,0,sizeof(order)); n=in(); for(int i=1;i<=n;i++){ num=in(); for(int j=1;j<=num;j++){ x=in(); addedge(i,x+n); } } for(int i=1;i<=n;i++){ x=in(); addedge(x+n,i); } } int Find(int x){ if(father[x]==x)return x; father[x]=Find(father[x]); return father[x]; } void DFS(int x){ for(int i=head[x];i!=-1;i=p[i].next){ if(order[p[i].y]==0){ order[p[i].y]=order[x]+1; DFS(p[i].y); if(order[Find(p[i].y)]<order[Find(x)])father[x]=father[p[i].y]; } else if(co[Find(p[i].y)]==0 && order[Find(p[i].y)]<order[Find(x)])father[x]=father[p[i].y]; } co[x]=1; } void Tarjan(){ for(int i=1;i<=n+n;i++)father[i]=i; for(int i=1;i<=n+n;i++){ if(order[i]==0){ order[i]=1; DFS(i); } } for(int i=1;i<=n+n;i++)Find(i); } int main() { init(); Tarjan(); for(int i=1;i<=n;i++){ res=0; for(int j=head[i];j!=-1;j=p[j].next){ if(father[i]==father[p[j].y])link[res++]=p[j].y-n; } out(res); sort(link,link+res); for(int j=0;j<res;j++){ putchar(' '); out(link[j]); } puts(""); } return 0; } 

 

你可能感兴趣的:(poj 1904)