Language:
King's Quest
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 |
国王给王子选老婆。。。每个王子有分别喜欢的姑娘。。且只能跟喜欢的妹子结婚
大臣给出了一个符合要求的匹配 但是国王想知道 王子可以跟哪些妹子结婚
分析:很好的图论题,把强连通分量和完美匹配结合起来了,记得多校的时候看到类似的题目(hdu 4685),但是不会做,还以为是二分匹配=_=
首先建图,如果王子u喜欢妹子v,则建一条边u指向v(u,v),对于大臣给出的初始完美匹配,如果王子u和妹子v结婚,则建一条边v指向u(v,u),然后求强连通分量,
对于每个王子和妹子,如果他们都在同一个强连通分量内,则他们可以结婚。
为什么呢?因为每个王子只能和喜欢的妹子结婚,初始完美匹配中的丈夫和妻子之间有两条方向不同的边可以互达,则同一个强连通分量中的王子数和妹子数一定是相等的,若王子x可以和另外的一个妹子a结婚,妹子a的原配王子y肯定能找到另外一个妹子b结婚,因为如果找不到的话,则x和a必不在同一个强连通分量中。
所以一个王子可以和所有与他同一强连通分量的妹子结婚,而这不会导致同一强连通分量中的其他王子找不到妹子结婚。
以上分析来自: 点击打开链接数据量比较大 不加优化 程序跑的时间还是挺长的 可以加上读入 输出优化
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
void read(int &x)
{
char ch;
x=0;
while(ch=getchar(),ch!=' '&&ch!='\n')
{
x=x*10+ch-'0';
}
}
void Print(int a) //Êä³öÍâ¹Ò
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
const int MAXN=4010;
const int MAXM=200000+4010;
struct Edge
{
int to,next;
}edge[MAXM];
int head[MAXN],tot;
int low[MAXN],dfn[MAXN],sta[MAXN],belong[MAXN];
int index,top;
int scc;
bool instack[MAXN];
int n;
void addedge(int u,int v)
{
edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++;
}
void Tarjan(int u)
{
int v;
low[u]=dfn[u]=++index;
sta[top++]=u;
instack[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[v],low[u]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
scc++;
do{
v=sta[--top];
instack[v]=0;
belong[v]=scc;
}while(u!=v);
}
}
void solve()
{
for(int i=1;i<=n*2;i++)
if(!dfn[i])
Tarjan(i);
}
void init()
{
MEM(head,-1);
tot=0;
MEM(dfn,0);
MEM(instack,0);
index=scc=top=0;
}
int ans[MAXN];
int main()
{
// fread;
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=1;i<=n;i++)
{
int num,v;
scanf("%d",&num);
for(int j=1;j<=num;j++)
{
scanf("%d",&v);
addedge(i,v+n);
}
}
for(int i=1;i<=n;i++)
{
int v;
scanf("%d",&v);
addedge(v+n,i);
}
solve();
for(int u=1;u<=n;u++)
{
int cnt=0;
for(int j=head[u];j!=-1;j=edge[j].next)
{
int v=edge[j].to;
// cout<<v<<endl;
// cout<<belong[u]<<" "<<belong[v]<<endl;
if(belong[u]==belong[v])
{
ans[cnt++]=v-n;
}
}
sort(ans,ans+cnt);
printf("%d",cnt);
for(int i=0;i<cnt;i++)
{
printf(" %d",ans[i]);
}
printf("\n");
}
}
return 0;
}