poj1904 King's Quest (tarjan求强连通)
来源:互联网 发布:各种旋转矩阵公式 编辑:程序博客网 时间:2024/05/16 03:26
King's Quest
Time Limit: 15000MS Memory Limit: 65536KTotal Submissions: 9509 Accepted: 3520Case 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.
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.
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
42 1 22 1 22 2 32 3 41 2 3 4
Sample Output
2 1 22 1 21 31 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
Northeastern Europe 2003
题意:国王有n个皇子,每个皇子喜欢ki个女孩,一共有n个女孩,国王想让他的儿子和他们自己喜欢的女孩在一起,并且不影响其他人和他们喜欢的人在一起。
其实就是求那个王子和他喜欢的女孩在一起不影响最大匹配数。
思路:设皇子所在集合为x,女孩所在集合为y, 假设xi->yi 如果xi 和xj在一起,为了不影响最大匹配数,则xj和yi应该在一起,也就是
xi->yj->xj->xi,,则最后一定会形成一个环,所以可以用强连通分量来求解即可
#include <cstdio>#include <stack>#include <vector>#include <cstring>#include <algorithm>using namespace std;const int maxn = 2017;int n, head[maxn*2], scc_cnt, dfs_clock, cnt, pre[maxn*2], sccno[maxn*2], lowlink[maxn*2];struct Edge{ int to,next;}edge[maxn*maxn*4];stack<int> S;vector<int> G;void add(int u, int v){ edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt ++;}void init(){ memset(head,-1,sizeof(head)); memset(sccno,0,sizeof(sccno)); memset(pre,0,sizeof(pre)); cnt = dfs_clock = scc_cnt = 0; for(int i = 1; i <= n; i ++){ int m, x; scanf("%d",&m); for(int j = 1; j <= m; j ++){ scanf("%d",&x); add(i, x + n); } } for(int i = 1; i <= n; i ++){ int x; scanf("%d",&x); add(x+n,i); }}void dfs(int u){ pre[u] = lowlink[u] = ++ dfs_clock; S.push(u); for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].to; if(!pre[v]){ dfs(v); lowlink[u] = min(lowlink[u], lowlink[v]); } else if(!sccno[v]) lowlink[u] = min(lowlink[u], pre[v]); } if(lowlink[u] == pre[u]){ ++ scc_cnt; for(;;){ int x = S.top(); S.pop(); sccno[x] = scc_cnt; if(x == u) break; } }}void solve(){ for(int i = 1; i <= n; i ++){ if(!pre[i]) dfs(i); } for(int i = 1; i <= n; i ++){ G.clear(); for(int j = head[i]; j != -1; j = edge[j].next){ int v = edge[j].to; if(sccno[i] == sccno[v] && v > n) G.push_back(v); } sort(G.begin(),G.end()); printf("%d",G.size()); for(int j = 0; j < G.size(); j ++){ printf(" %d",G[j] - n); } printf("\n"); }}int main(){ while(~scanf("%d",&n)){ init(); solve(); } return 0;}
阅读全文
0 0
- poj1904 King's Quest (tarjan求强连通)
- poj1904 King's Quest(强连通)
- POJ1904/ZOJ2470 King's Quest(tarjan判强连通分量)
- poj1904 King's Quest 强连通分量
- POJ1904:King's Quest(强连通 & 二分图)
- ZOJ2470 POJ1904 King's Quest,强连通分量
- [poj 1904]King's Quest[Tarjan强连通分量]
- King's Quest - POJ 1904 Tarjan强连通分量
- poj1904 - King's Quest
- King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
- POJ1904 King's Quest SCC
- 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 强连通
- poj 1904 King's Quest 强连通
- 人生的4个维度与价值观
- Facebook推出图像对比软件,限制“色情报复”内容传播
- 打响印度全民AI第一枪,英特尔为何启动“AI开发者教育计划”
- SpaceX回收火箭能省多少钱?
- 【解决方法】windows7出现无法定位程序输入点ucrtbase.terminate于动态链接库api-ms-win-crt-runtime-|1-1-0.dll
- poj1904 King's Quest (tarjan求强连通)
- HTML WebSocket笔记
- OpenCV 实践——人脸检测与人脸图像提取
- Oracle之获取SQL对应表的信息
- (转)用户变量和系统变量的区别
- CITE2017倒计时,开启智能时代
- 王思聪“包养”电竞后 万达觉得可以开攻游戏行业了
- Ubuntu Unity 已死,但 Ubuntu 在云端依然受欢迎
- 最美曲面屏手机 vivo Xplay6 面纱下全是黑科技