poj 1904 King's Quest
2018-07-31 16:16
363 查看
King's Quest
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.
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.
Time Limit: 15000MS | Memory Limit: 65536K | |
Total Submissions: 10052 | Accepted: 3704 | |
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. 题目大意:有n个王子和n个妹子,每个王子有好几个喜欢的妹子,巫师给出了一个完美匹配,每个王子都可以和自己喜欢的妹子结婚。但是国王对这个不满意,国王想知道每个王子最多可以和几个妹子结婚,在每个王子都可以找到自己喜欢的妹子的前提下。 思路:二话不说,先建图,每个王子向每个喜欢的妹子连一条边,因为能结婚的第一个条件,王子喜欢她。再把给出的完美匹配,妹子向王子连一条边。强连通分量内部的王子和公主匹配,为什么呢? 首先,强连通分量内部的王子和妹子是可以相互联通的,所以可以结婚, 其次,王子和妹子个数一定相等,不可能王子比妹子少,否则与完美匹配只有一个矛盾,也不可能王子比妹子多,一来王子不会喜欢王子,二来还是完美匹配矛盾问题。 为什么王子不会和强连通分量外部的妹子结婚? 如果出现这种情况,比如王子x,y,妹子a,b,x,a是匹配的,y,b是匹配的,且不在一个联通分量里。若x与b匹配,则y与a匹配,与不在一个强连通分量矛盾。 被卡了10次的地方! 数组开小了! 读题读题读题!#include<cstdio> #include<cstring> #include<iostream> #include<cstdlib> #include<algorithm> #include<cmath> #include<vector> #include<stack> #include<map> using namespace std; #define mem(a,b) memset(a,b,sizeof(a)) #define ll long long #define inf 1000000000 #define maxn 5000 #define maxm 200000+2500 inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') {x=10*x+ch-'0';ch=getchar();} return x*f; } struct node { int to,next; } edge[maxm]; int n,m,head[maxn],vis[maxn],dfn[maxn],low[maxn],cdu[maxn],num[maxn],cnt,timi,stack1[maxn],top,cut,rdu[maxn]; void init() { memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); memset(cdu,0,sizeof(cdu)); memset(rdu,0,sizeof(rdu)); cnt=0; timi=1; top=0; cut=0; } void addedge(int u,int v) { edge[cnt].to=v; edge[cnt].next=head[u]; head[u]=cnt; cnt++; } int Stack[maxm]; void Tarjan(int u) //求强连通分量 { int i,v; low[u]=dfn[u]=timi++; Stack[++top]=u; vis[u]=1; for(i=head[u];i!=-1;i=edge[i].next) { v=edge[i].to; if(!dfn[v]) { Tarjan(v); low[u]=min(low[u],low[v]); } else if(vis[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { cut++; while(1) { v=Stack[top--]; vis[v]=false; num[v]=cut; //缩点 if(u==v) break; } } } int main() { int a; while(~scanf("%d",&n)) { init(); for(int i=1; i<=n; ++i) { int x=read(); while(x--) { a=read();addedge(i,a+n); } } for(int i=1;i<=n;++i) { a=read();addedge(a+n,i); } for(int i=1; i<=2*n; ++i) { if(!dfn[i]) Tarjan(i); } for(int i=1;i<=n;++i) { int ans[4005],tot=0; tot=0; for(int j=head[i];j!=-1;j=edge[j].next) { int v=edge[j].to; if(num[i]==num[v]) ans[++tot]=v-n; } sort(ans+1,ans+tot+1); printf("%d",tot); for(int j=1;j<=tot;++j) printf(" %d",ans[j]); printf("\n"); //printf("%d\n",ans[tot]); } } return 0; }View Code
相关文章推荐
- poj 1904 King's Quest tarjan求二分图的所有可选最大匹配边
- Poj 1904 King's Quest 强连通分量
- poj 1904 King's Quest 强连通
- POJ 1904 King's Quest【强连通分量】
- POJ 1904 King's Quest
- POJ-1904-King's Quest(强连通图)
- poj--1904--King's Quest(scc建图)
- POJ 1904 King's Quest(强连通)
- POJ 1904 King's Quest 强连通分量+二分图增广判定
- POJ1904(King's Quest)
- poj 1904 King's Quest(强连通分量)
- POJ 1904 King's Quest 强连通分量 +缩点+ 输入输出外挂 (非递归版trajan模板)
- poj 1904 King's Quest(强连通)
- POJ 1904 King's Quest(强连通分量+匹配)
- King's Quest - poj 1904(强连通分量+外挂输入输出)
- poj 1904 King's Quest(Tarjan+匹配)
- poj 1904 King's Quest 强连通分量
- POJ-1904-King's Quest(强连通分量)
- POJ 1904 King's Quest(强连通)
- poj 1904 King's Quest (SCC缩点)