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)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每 ...
随机推荐
- rails应用ajax之一:使用纯js方法
考虑如下需求: 1. 用户输入一个用户名,当焦点跳出文本框时,检查该用户名是否有效 2. 动态更新检查的结果 我们使用ajax的方式来实现这个简单的功能,首先建立view:check.html.erb ...
- 实现Android Native端爆破源码
尝试在移动端so侧做一些内存修改,使之走向不通的逻辑,一下为将要爆破的APP源码 JAVA侧: package com.example.grady.sectestone; import android ...
- twisted高并发库transport函数处理数据包的些许问题
还是在学校时间比较多, 能够把时间更多的花在学习上, 尽管工作对人的提升更大, 但是总是没什么时间学习, 而且工作的气氛总是很紧凑, 忙碌, 少了些许激情吧.适应就好了.延续着之前对twisted高并 ...
- Salesforce的数据权限机制
本文主要介绍了 Salesforce 对于系统中数据的访问控制是如何设计的,然后也了解了下 Alfresco 和 Oracle VPD 的数据权限机制.希望对一些业务系统的数据权限的访问控制设计能有所 ...
- IEEE发布2017年编程语言排行榜:Python高居首位
https://news.cnblogs.com/n/574248 编者按:本文由微信公众号“机器之心”(ID:almosthuman2014)编译,机器之心专注生产 AI 领域专业性内容.本文作者: ...
- ImageMagick 使用经验
from:http://community.itbbs.cn/thread/20402/ 1.如何用ImageMagic水平或垂直拼接图片 因为是分片下载的,现在只能用montage拼接图片列阵,但如 ...
- 给你的网页添加一个随机的BGM
大晚上的,突然想到,我这么喜欢听歌的人,博客里怎么能少了BGM呢,说干就干. 首先,给博客侧边栏加一个空div:<div id="music"></div> ...
- php获取指定目录下的所有文件列表
在我们实际的开发需求中,经常用到操作文件,今天就讲一下关于获取指定目录下的所有文件的几种常用方法: 1.scandir()函数 scandir() 函数返回指定目录中的文件和目录的数组. scandi ...
- Go 语言集成开发环境 GoLand 更新至 2018.1.3 版本
GoLand 2018.1.3 已发布,这是一个全新的更新,尽管没有带来明显的针对 Go 语言的特定改进,但还是提供了许多与 UI,JavaScript 和 SQL 支持相关的错误修复和改进. 可通过 ...
- C语言下double转char*或者std::string,可以精确转换不含多余的0
char* GetDoubleStr(double value) { char buf[32]={0};//长度可以自定义 sprintf(buf,"%.8f",value);// ...