Language:
Default
King'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 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


借鉴链接: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-8
typedef __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 20000 int 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;
}

POJ 1904 King&#39;s Quest(强连通)的更多相关文章

  1. Poj 1904 King's Quest 强连通分量

    题目链接: http://poj.org/problem?id=1904 题意: 有n个王子和n个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可 ...

  2. POJ 1904 King's Quest ★(强连通分量:可行完美匹配边)

    题意 有n个女生和n个男生,给定一些关系表示男生喜欢女生(即两个人可以结婚),再给定一个初始匹配,表示这个男生和哪个女生结婚,初始匹配必定是合法的.求每个男生可以和哪几个女生可以结婚且能与所有人不发生 ...

  3. [poj 1904]King's Quest[Tarjan强连通分量]

    题意:(当时没看懂...) N个王子和N个女孩, 每个王子喜欢若干女孩. 给出每个王子喜欢的女孩编号, 再给出一种王子和女孩的完美匹配. 求每个王子分别可以和那些女孩结婚可以满足最终每个王子都能找到一 ...

  4. POJ 1904 King's Quest (强连通分量+完美匹配)

    <题目链接> 题目大意: 有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国王不满意,他要求大臣给他另一个表,每个王 ...

  5. POJ - 1904 King's Quest (强连通)

    题意:有N个王子,每个王子有任意个喜欢的妹子,巫师会给出一个方案:每个妹子都嫁给一个王子.但是国王希望知道:每个王子能在哪些妹子中择偶而不影响其他王子择偶. 分析:设王子为x部,妹子为y部,假设有匹配 ...

  6. POJ 1904 King's Quest 强连通分量+二分图增广判定

    http://www.cnblogs.com/zxndgv/archive/2011/08/06/2129333.html 这位神说的很好 #include <iostream> #inc ...

  7. POJ 1904 King's Quest tarjan

    King's Quest 题目连接: http://poj.org/problem?id=1904 Description Once upon a time there lived a king an ...

  8. poj 1904 King's Quest

    King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每 ...

  9. POJ 1904 King's Quest(SCC的巧妙应用,思维题!!!,经典题)

    King's Quest Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 10305   Accepted: 3798 Ca ...

随机推荐

  1. Java获取系统日期时间

    方法一:利用Calendar类来获取当前日期和时间,代码如下: /** * 获取系统时间 * @return */ public String getDate(){ Calendar calendar ...

  2. UDP中使用bind和connect的作用

    1:UDP中可以使用connect系统调用 2:UDP中connect操作与TCP中connect操作有着本质区别. TCP中调用connect会引起三次握手,client与server建立连结.UD ...

  3. JAVA4种线程池的使用

    Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程.newFixe ...

  4. vue_axios请求封装、异常拦截统一处理

    1.前端网络请求封装.异常统一处理 vue中采用axios处理网络请求,避免请求接口重复代码,以及各种网络情况造成的异常情况的判断,采用axios请求封装和异常拦截操作: axios 请求封装 // ...

  5. plsql连接oracle数据库,不用配置任何东西(转)

    在软件开发的过程中,对于使用oracle的朋友们来说,使用plsql工具操作oracle数据库是非常方便的,可是plsql连接oracle数据库的方式有很多种,今天就给大家介绍一种最简单的连接方式,只 ...

  6. zeromq学习笔记2——简单的客户端和服务端测试程序

    1.前言 zeromq提供了guide,http://zguide.zeromq.org/,可以帮助新手快速上手,提供了C\C++\PHP等多种语言. 2.测试程序 使用zeromq给的hwserve ...

  7. .NET Core Entity使用Entity Framework Core链接数据库

    首先安装Nuget包 Install-package Microsoft.EntityFrameworkCore Install-package Microsoft.EntityFrameworkCo ...

  8. 我是陌生人 Java中导入、导出Excel

    我是陌生人 Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是: ...

  9. 【tp5】ThinkCMF5框架,配置使其支持不同终端PC/WAP/Wechat能加载不同配置和视图

    1.版本 5.0.18 2.在data/conf/ 新增config.php文件,内容如下: <?php //ThinkCMF5区别不同客户端加载不同配置文件和模块.视图 $default_mo ...

  10. 腾讯企业邮箱设置发送邮件的配置(针对smtp)

    QQ邮箱也是如下配置,不过需要进行开启smtp