King's Quest
Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 8311   Accepted: 3017
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
题意:一个国王有N个王子。一共有N个女孩,每个王子可以喜欢多个女孩,但只能取一个女孩。给定一个参考结婚列表,问每个王子可分别与哪几个女孩结婚。
思路:王子与女孩之间建立有向图,再根据参考结婚列表建立反向边,那么与王子处于同一个连通分量的女孩且是王子喜欢的可以和王子结婚。
附输入输出挂
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
struct Edge{
int to,next;
}es[];
int V;
void Scan(int &val)
{
char ch;
int x=;
bool flag=true;
ch=getchar();
if(ch=='-') flag=false;
else if(''<=ch&&ch<='') x=(ch-'');
while((ch=getchar())&&''<=ch&&ch<='')
x=x*+ch-'';
val=(flag==true)?x:-x;
}
void Print(int x)
{
if(x>) Print(x/);
putchar(x%+'');
}
int head[MAXN],tot;
void add_edge(int u,int v)
{
es[tot].to=v;
es[tot].next=head[u];
head[u]=tot++;
}
int index;
int dfn[MAXN],low[MAXN];
int stack[MAXN],top;
int cpnt[MAXN],cnt;
bool instack[MAXN];
void tarjan(int u)
{
instack[u]=true;
stack[top++]=u;
dfn[u]=low[u]=++index;
for(int i=head[u];i!=-;i=es[i].next)
{
int v=es[i].to;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int v;
cnt++;
do{
v=stack[--top];
instack[v]=false;
cpnt[v]=cnt;
}while(u!=v);
}
}
int ans[MAXN];
void solve()
{
memset(ans,,sizeof(ans));
for(int i=;i<=V+V;i++)
if(!dfn[i]) tarjan(i); for(int i=;i<=V;i++)
{
int counter=;
for(int j=head[i];j!=-;j=es[j].next)
{
int v=es[j].to;
if(cpnt[v]==cpnt[i]) ans[counter++]=v-V;
}
sort(ans,ans+counter);
Print(counter);
for(int j=;j<counter;j++) putchar(' '),Print(ans[j]);
putchar('\n');
}
} int main()
{
while(scanf("%d",&V)!=EOF)
{
tot=index=top=cnt=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(instack,false,sizeof(instack));
memset(cpnt,,sizeof(cpnt));
for(int i=;i<=V;i++)
{
int k;
Scan(k);
while(k--)
{
int v;
Scan(v);
add_edge(i,V+v);
}
}
for(int i=;i<=V;i++)
{
int v;
Scan(v);
add_edge(V+v,i);
}
solve();
}
return ;
}
kosaraju算法——有向图缩点利器
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"vector"
using namespace std;
const int MAXN=;
vector<int> G[MAXN];
vector<int> rG[MAXN];
vector<int> vs;
int V,E;
void add_edge(int u,int v)
{
G[u].push_back(v);
rG[v].push_back(u);
}
int vis[MAXN];
int cpnt[MAXN];
void dfs(int u)
{
vis[u]=;
for(int i=;i<G[u].size();i++)
if(!vis[G[u][i]]) dfs(G[u][i]);
vs.push_back(u);
}
void rdfs(int u,int k)
{
vis[u]=;
cpnt[u]=k;
for(int i=;i<rG[u].size();i++)
if(!vis[rG[u][i]]) rdfs(rG[u][i],k);
}
void scc()
{
vs.clear();
memset(vis,,sizeof(vis));
for(int i=;i<=V+V;i++)
if(!vis[i]) dfs(i);
memset(vis,,sizeof(vis));
int k=;
for(int i=vs.size()-;i>=;i--)
if(!vis[vs[i]]) rdfs(vs[i],k++);
}
int ans[MAXN];
void solve()
{
memset(ans,,sizeof(ans));
scc();
for(int i=;i<=V;i++)
{
int counter=;
for(int j=;j<G[i].size();j++)
{
int v=G[i][j];
if(cpnt[i]==cpnt[v]) ans[counter++]=v-V;
}
sort(ans,ans+counter);
printf("%d",counter);
for(int i=;i<counter;i++) printf(" %d",ans[i]);
printf("\n");
}
}
int main()
{
while(scanf("%d",&V)!=EOF)
{
for(int i=;i<=V+V;i++)
{
G[i].clear();
rG[i].clear();
} for(int i=;i<=V;i++)
{ int k;
scanf("%d",&k);
while(k--)
{
int v;
scanf("%d",&v);
add_edge(i,v+V);
}
}
for(int i=;i<=V;i++)
{
int v;
scanf("%d",&v);
add_edge(v+V,i);
} solve();
}
return ;
}
												

POJ1904(有向图缩点+输入输出挂参考)的更多相关文章

  1. 【输入输出挂】【Uva11462】Age Sort

    例题17  年龄排序(Age Sort, UVa 11462)照从小到大的顺序输出. [输入格式] 输入包含多组测试数据.每组数据的第一行为整数n(0<n≤2 000 000),即居民总数:下一 ...

  2. hdu 3072 有向图缩点成最小树形图计算最小权

    题意,从0点出发,遍历所有点,遍历边时候要付出代价,在一个SCC中的边不要付费.求最小费用. 有向图缩点(无需建立新图,,n<=50000,建则超时),遍历边,若不在一个SCC中,用一个数组更新 ...

  3. HDU1269(有向图缩点模板题)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. POJ2553( 有向图缩点)

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9779   Accepted:  ...

  5. POJ2186(有向图缩点)

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28379   Accepted: 11488 De ...

  6. hdu 1827 有向图缩点看度数

    题意:给一个有向图,选最少的点(同时最小价值),从这些点出发可以遍历所有. 思路:先有向图缩点,成有向树,找入度为0的点即可. 下面给出有向图缩点方法: 用一个数组SCC记录即可,重新编号,1.... ...

  7. HDU 4635 (完全图 和 有向图缩点)

    题目链接:HDU  4635 题目大意: 给你一个有向图,加有向边,使得这个图是简单有向图.问你最多加多少条有向边. 简单有向图: 1.不存在有向重边. 2.不存在图循环.(注意是不存在 “图” 循环 ...

  8. poj 2823 Sliding Windows (单调队列+输入输出挂)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 73426   Accepted: 20849 ...

  9. 对Tarjan——有向图缩点算法的理解

    开始学tarjan的时候,有关无向图的割点.桥.点双边双缩点都比较容易地理解了,唯独对有向图的缩点操作不甚明了.通过对luoguP2656_采蘑菇一题的解决,大致搞清了tarjan算法的正确性. 首先 ...

随机推荐

  1. Android自己定义ViewGroup打造各种风格的SlidingMenu

    看鸿洋大大的QQ5.0側滑菜单的视频课程,对于側滑的时的动画效果的实现有了新的认识,似乎打通了任督二脉.眼下能够实现随意效果的側滑菜单了.感谢鸿洋大大!! 鸿洋大大用的是HorizontalScrol ...

  2. 怎样使用Entityframework.Extended

    这个插件真的非常有用,我们能够使用下面语法来简化我们的工作,下面不过演示样例: Deleting <strong>//delete all users where FirstName ma ...

  3. byte 单位换算

    1G就1GB啦,平时人们说1G只是简洁来说而已. bit(位).B(字节).K(千).M(兆).G(吉咖).T(太拉) B(Byte).KB(KiloByte).MB(MegaByte).GB(Gig ...

  4. Hnu 11187 Emoticons :-) (ac自己主动机+贪心)

    题目大意: 破坏文本串.使之没有没有出现表情.破坏就是用空格替换.问最少须要破坏多少个字符. 思路分析: 初看跟Hdu 2457 没什么差别,事实上Hdu2457是要求将字符替换成ACGT,而这个仅仅 ...

  5. iOS设计模式 - (2)UML类间关系精解

    在正式讲设计模式之前, 介绍一下UML类图之间的关系还是非常有必要的, 由于一些教程, 书籍, 包含我之后的文章, 都会大量使用类图, 去描写叙述各个类之间的关系.这是一种非常直观, 简约的方式. 当 ...

  6. 每天进步一点点——mysql——Percona XtraBackup(innobackupex)

    一.  简单介绍 Percona XtraBackup是开源免费的MySQL数据库热备份软件,它能对InnoDB和XtraDB存储引擎的数据库非堵塞地备份(对于MyISAM的备份相同须要加表锁).Xt ...

  7. Python--数据类型整理

      数据类型整理-------------------------------------------------------------------------------------------- ...

  8. Kubernetes调度之亲和与反亲和

    系列目录 部署pod时,大多数情况下kubernetes的调度程序能将pod调度到集群中合适的节点上.但有些情况下用户需要对pod调度到哪个节点上施加更多控制,比如将特定pod部署到拥有SSD存储节点 ...

  9. 关于 angular cookie 设置的坑

    初识Angular,才知道掉进了这么一个各种大坑的坑. 先说下对于$cookie.put 这几个方法,只有1.4以上版本才可以用,其余低于版本请使用 $cookieStore: 下面举例下使用方法: ...

  10. 侧边打赏-html

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...