poj~1904
Description
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 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
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
这题加和不加输入输出挂差别是超级大的
下面是放的是加输入输出挂的代码
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std; const int maxn = 1e6 + ;
int n, m, u, v, tot, top, cnt, flag;
int Scan()
{
int res=,ch,flag=;
if((ch=getchar())=='-')
flag=;
else if(ch>=''&&ch<='')
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return flag?-res:res;
}
void Out(int a)
{
if(a>)
Out(a/);
putchar(a%+'');
}
struct node {
int v, next;
} edge[maxn];
int head[maxn], instack[maxn], s[maxn];
int dfn[maxn], low[maxn], belong[maxn];
void init() {
tot = cnt = top = flag = ;
memset(s, , sizeof(s));
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(instack, , sizeof(instack));
}
void add(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjin(int v) {
dfn[v] = low[v] = ++flag;
instack[v] = ;
s[top++] = v;
for (int i = head[v] ; i != - ; i = edge[i].next) {
int j = edge[i].v;
if (!dfn[j]) {
tarjin(j);
low[v] = min(low[v], low[j]);
} else if (instack[j]) low[v] = min(low[v], dfn[j]);
}
if (dfn[v] == low[v]) {
cnt++;
int t;
do {
t = s[--top];
instack[t] = ;
belong[t] = cnt;
} while(t != v) ;
}
}
void solve() {
for (int i = ; i <= n ; i++)
if (!dfn[i]) tarjin(i);
}
int ans[maxn];
int main() {
while(scanf("%d", &n) != EOF) {
init();
int num;
memset(ans,,sizeof(ans));
for (int i = ; i <= n ; i++) {
num=Scan();
while(num--) {
int v;
v=Scan();
add(i, n + v);
}
}
for (int i = ; i <= n ; i++) {
int v;
v=Scan();
add(n + v, i);
}
for (int i = ; i <= *n ; i++)
if (!dfn[i]) tarjin(i);
for (int i = ; i <= n ; i++) {
int k = ;
for (int j = head[i] ; ~j ; j = edge[j].next) {
if (belong[edge[j].v] == belong[i]) ans[k++] = edge[j].v - n;
}
sort(ans, ans + k);
Out(k);
for (int i = ; i < k ; i++){
putchar(' ');
Out(ans[i]);
}
putchar('\n');
}
}
return ;
}
poj~1904的更多相关文章
- Poj 1904 King's Quest 强连通分量
题目链接: http://poj.org/problem?id=1904 题意: 有n个王子和n个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可 ...
- poj 1904(强连通分量+输入输出外挂)
题目链接:http://poj.org/problem?id=1904 题意:有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国 ...
- 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 ...
- POJ 1904 HDU 4685
这两道题差不多,POJ这道我很久以前就做过,但是比赛的时候居然没想起来.. POJ 这道题的题意是,N个王子每个人都有喜欢的公主,当他们选定一个公主结婚时,必须是的剩下的人也能找到他喜欢的公主结婚. ...
- poj 1904(强连通分量+完美匹配)
传送门:Problem 1904 https://www.cnblogs.com/violet-acmer/p/9739990.html 参考资料: [1]:http://www.cnblogs.co ...
- POJ 1904 King's Quest ★(强连通分量:可行完美匹配边)
题意 有n个女生和n个男生,给定一些关系表示男生喜欢女生(即两个人可以结婚),再给定一个初始匹配,表示这个男生和哪个女生结婚,初始匹配必定是合法的.求每个男生可以和哪几个女生可以结婚且能与所有人不发生 ...
- poj 1904 强连通分量
思路:先有每个儿子向所有他喜欢的姑娘建边,对于最后给出的正确匹配,我们建由姑娘到相应王子的边.和某个王子在同一强连通分量,且王子喜欢的姑娘都是该王子能娶得.思想类似匈牙利算法求匹配的时候,总能找到增广 ...
- POJ 1904 King's Quest 强连通分量+二分图增广判定
http://www.cnblogs.com/zxndgv/archive/2011/08/06/2129333.html 这位神说的很好 #include <iostream> #inc ...
- poj 1904 King's Quest tarjan求二分图的所有可选最大匹配边
因为是完美匹配,所以每个点都已经匹配了,那么如果要选择一条别的边,增光路的最后必定找到原来所匹配的点,加上匹配的边,那么就是一个环.所以可选边在一个强连通分量里. #include <iostr ...
- poj 1904 King's Quest
King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每 ...
随机推荐
- spring boot + quartz 集群
spring boot bean配置: @Configuration public class QuartzConfig { @Value("${quartz.scheduler.insta ...
- iOS解决UITableView中Cell重用带来的问题
tableView的常规配置,当超出一屏的cell就会标上可重用的标识出列到可重用缓存池中,后面再根据可重用标识来到的可重的cell就会和前面显示同样内容. - (UITableViewCell *) ...
- java main方法执行sql语句
public static void main(String[] args) throws Exception{ String driver = "oracle.jdbc.driver.Or ...
- Python_PyMySQL数据库操作
连接数据库: conn=pymysql.connect(host=,user=',charset='utf8') 建立游标: cur = conn.cursor() 创建一个名字叫 lj 的数据库: ...
- IT轮子系列(一)——DropDownList 的绑定,你秒懂了吗
前言 最近猛然惊觉(说是猛然,是因为自己工作那么多年,居然不自知.不反省),在开发中,自己碰到一些常用的功能代码块,还是习惯性的baidu,然后copy....这样的操作,不知自己重复了多少遍.现在回 ...
- HTML 5核心内容
demo飞象 HTML 5 系列文章索引:http://www.cnblogs.com/webabcd/archive/2012/05/27/2520567.html demo飞象HTML 5 (11 ...
- jquery.js
/*! jQuery v1.10.2 | (c) 2005, 2013 jQuery Foundation, Inc. | jquery.org/license //@ sourceMappingUR ...
- ubuntu 13.10 install wireshark
ubuntu 13.10 install wireshark 今天在使用java jsoup操作remote server的是否,在本地执行可以成功返回内容,然后打成jar包,使用shell在 ser ...
- mysql高级之编程优化
★编程优化一.字符编码(mysql控制台乱码输出解决:character_set_results='gbk')表/列编码设置 列:alter table 表名 change 列名 列名 数据类型 c ...
- yii2实战之初见端倪
PHP框架大PK php框架有很多种,在国内应用较多的有:Thinkphp, Yii, Laravel, Codeigniter等.关于这些框架,孰优孰劣,是一个极具争议性的话题.各方支持者总能拿出自 ...