King's Quest
Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 9460   Accepted: 3497
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
题解这里
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
const int M=4e6+;
vector<int>sc[N];
vector<int>G[N];
int head[N],dfn[N],low[N],q[N];
bool instack[N],mp[][];
int tot,scnt,l,cnt;
struct node
{
int to,next;
} e[M];
void add(int u,int v)
{
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
void Tarjan(int u)
{
dfn[u]=low[u]=++cnt;
q[l++]=u;
instack[u]=;
for(int i=head[u]; ~i; i=e[i].next)
{
int v=e[i].to;
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v]&&dfn[v]<low[u]) low[u]=dfn[v];
}
if(low[u]==dfn[u])
{
++scnt;
int t;
do
{
t=q[--l];
sc[scnt].push_back(t);
instack[t]=;
}
while(t!=u);
}
}
int main()
{
int n,x,y;
while(scanf("%d",&n)!=EOF)
{
tot=scnt=cnt=l=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(mp,,sizeof(mp));
for(int i=; i<=n; ++i)
{
scanf("%d",&x);
while(x--)
{
scanf("%d",&y);
add(i,y+n);
G[i].clear();
sc[i].clear();
mp[i][y]=;
}
}
for(int i=; i<=n; ++i)
{
scanf("%d",&x);
add(x+n,i);
}
for(int i=; i<=n; ++i) if(!dfn[i]) Tarjan(i);
for(int i=; i<=scnt; ++i) sort(sc[i].begin(),sc[i].end());
for(int i=; i<=scnt; ++i)
{
int tc=upper_bound(sc[i].begin(),sc[i].end(),n)-sc[i].begin();
for(int j=; j<tc; ++j) for(int k=tc; k<(int)sc[i].size(); ++k) if(mp[sc[i][j]][sc[i][k]-n] ) G[sc[i][j]].push_back(sc[i][k]-n);
}
for(int i=; i<=n; ++i)
{
printf("%d",(int)G[i].size());
for(int j=; j<(int)G[i].size(); ++j) printf(" %d",G[i][j]);
puts("");
}
}
}

poj1904 完美匹配+Tarjan的更多相关文章

  1. poj1904 二分图匹配+强连通分量

    http://poj.org/problem?id=1904 Description Once upon a time there lived a king and he had N sons. An ...

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

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

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

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

  4. poj 1904(强连通分量+完美匹配)

    传送门:Problem 1904 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:http://www.cnblogs.co ...

  5. ZOJ-3933 Team Formation (二分图最佳完美匹配)

    题目大意:n个人,分为两个阵营.现在要组成由若干支队伍,每支队伍由两个人组成并且这两个人必须来自不同的阵营.同时,每个人都有m个厌恶的对象,并且厌恶是相互的.相互厌恶的人不能组成一支队伍.问最多能组成 ...

  6. UVA 11383 Golden Tiger Claw(最佳二分图完美匹配)

    题意:在一个N*N的方格中,各有一个整数w(i,j),现在要求给每行构造row(i),给每列构造col(j),使得任意w(i,j)<=row(i)+col(j),输出row(i)与col(j)之 ...

  7. UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design

    题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi  ...

  8. UVALive 2238 Fixed Partition Memory Management(二分完美匹配)

    题意:计算机中有一些固定大小的内存,内存越大,处理速度越快.对于一个程序,加入不同的内存空间,处理所需时间不同.现给出m个内存空间,n个程序,对于每个程序程序,有k组数据(s,t),分别表示当程序 i ...

  9. UVALive 4043 Ants(二分图完美匹配)

    题意:每个蚁群有自己的食物源(苹果树),已知蚂蚁靠气味辨别行进方向,所以蚁群之间的行动轨迹不能重叠.现在给出坐标系中n个蚁群和n棵果树的坐标,两两配对,实现以上要求.输出的第 i 行表示第 i 个蚁群 ...

随机推荐

  1. SpringCloud系列之集成Dubbo应用篇

    目录 前言 项目版本 项目说明 集成Dubbo 2.6.x 新项目模块 老项目模块 集成Dubbo 2.7.x 新项目模块 老项目模块 参考资料 系列文章 前言 SpringCloud系列开篇文章就说 ...

  2. 12.Python提供了哪些内建类型

    There are mutable and Immutable types of Pythons built in types Mutable built-in types: List Set Dic ...

  3. mac OS 配置Apache服务器

    Mac自带了Apache环境 查看Apache版本 sudo apachectl -v 在终端输入:sudo apachectl start 在浏览器输入"http://localhost& ...

  4. Netty(六):NioServerSocketChannel源码解析

    我们在Netty学习系列五的最后提出了一些问题还没得到回答,今天来通过学习NioServerSocketChannel的源码来帮我们找到之前问题的答案. 先看一下NioServerSocketChan ...

  5. 压缩工具gzip、bzip2、xz的使用

    2019独角兽企业重金招聘Python工程师标准>>> 本文使用 为了要压缩 常见压缩格式 压缩工具 gzip压缩工具 bz2压缩工具 xz压缩工具 为什么要压缩 为什么要压缩?文件 ...

  6. CF思维联系– Codeforces-987C - Three displays ( 动态规划)

    ACM思维题训练集合 It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in ...

  7. Codeforce1311B. WeirdSort (冒泡排序)

    You are given an array a of length n. You are also given a set of distinct positions p1,p2,-,pm, whe ...

  8. predixy源码学习--开篇

    最近开始研究predixy.predixy是一款高性能全功能redis代理 ,网上有的文章大部分都是功能上的介绍,很少有源码相关的分享. predixy的相关介绍在github: https://gi ...

  9. LeetCode 45. 跳跃游戏 II | Python

    45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...

  10. RocketMQ搭建全过程

    RocketMQ下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/4.3.0/rocketmq-all-4.3.0-bin-relea ...