POJ 1904 King's Quest(强连通)

来源:互联网 发布:手机音乐软件 编辑:程序博客网 时间:2024/05/19 05:05

Language:
King's Quest
Time Limit: 15000MS Memory Limit: 65536KTotal Submissions: 7635 Accepted: 2769Case 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

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

Sample Output

2 1 22 1 21 31 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


借鉴链接:http://blog.csdn.net/l04205613/article/details/6654820


题意是,N个男生和N个女生,告诉你每个男生喜欢的女生编号,然后给出一个初始匹配(这个初始匹配是完备匹配),然后求所有可能的完备匹配,按升序输出。当然,如果暴整的话(当然我没试过),2000个男生+2000个女生,最多有20W条有向边

看了一个神牛的报告,把这个转化成强连通问题:

首先按照给出的有向边建图,然后根据最后的那个完备匹配在图中加入反向边(就是根据那个完备匹配连 女生 到 男生 的边),那么在这个图中,属于同一个强连通的点对一定是合法点对。把他们排序输出即可。


因为男生是不会爱男生的,所以如果是强连通,那么男生肯定是爱这个强连通分量中的所有女生的


#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)   memset ((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);   //男生爱女生}}       fre(i,1,n+1)        {        sf(x);        g[x+n].push_back(i);     //女生爱男生,如果这一种爱的关系是一种强连通,那么男生都可以选里面的女生        }       solve();}    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 坐多了屁股痛怎么办 上班坐的屁股痛怎么办 膝关节手术后康复膝盖疼怎么办 上下楼膝关节疼膝盖疼怎么办 跑步后关节疼该怎么办 后滚翻翻不过去怎么办 走太多路小腿疼怎么办 踢毽子以后期盖右腿内彻疼怎么办 大学体育选修课挂了怎么办 当天贴的砖踩了怎么办 刚贴的瓷砖踩了怎么办 长胶底板太轻怎么办 乒乓球拍胶皮不粘了怎么办 乒乓球拍子胶片太滑怎么办 新买的包黏黏的怎么办 卫星锅收不到台怎么办 养殖厂被卫星拍住怎么办 中六卫星无信号怎么办 晒出成片的斑怎么办 太阳晒出胳膊上长斑怎么办 宇航员在太空死后怎么办 太阳暴晒起的斑怎么办 太阳晒出来的斑怎么办 被认定D级危房怎么办 突然发现两个关系遥远怎么办 如果没有地球人类会怎么办 小锅盖被屏蔽了怎么办 美的冰箱故障通讯不合格怎么办 美的冰箱通讯不合格怎么办 文明6金币降到0怎么办 紫癜肾炎长期尿潜血怎么办 肾移植后血压高怎么办 尿道长了个肿瘤怎么办 吃了有病的鹅怎么办 狗狗得了乳腺瘤怎么办 孕28周还是臀位怎么办 怀孕五个月胎位不正怎么办 33周了胎位不正怎么办 足月胎儿不足5斤怎么办 绒癌观察期怀孕怎么办 宝宝囱门闭合晚怎么办