链接:

https://vjudge.net/problem/POJ-1904

题意:

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.

思路:

对王子喜欢的连一条边,再通过最后给的结婚关系,从女孩到王子连一条边。

再求强连通,再一个强连通内的王子和女孩肯定可以结婚。

代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
const int MAXN = 4e3+10; vector<int> G[MAXN];
int Dfn[MAXN], Low[MAXN];
int Vis[MAXN], Dis[MAXN];
int Fa[MAXN];
stack<int> St;
int n, cnt;
int times; void Tarjan(int x)
{
Dfn[x] = Low[x] = ++times;
Vis[x] = 1;
St.push(x);
for (int i = 0;i < G[x].size();i++)
{
int nextnode = G[x][i];
if (Dfn[nextnode] == 0)
{
Tarjan(nextnode);
Low[x] = min(Low[x], Low[nextnode]);
}
else if (Vis[nextnode])
Low[x] = min(Low[x], Dfn[nextnode]);
}
if (Low[x] == Dfn[x])
{
cnt++;
while (St.top() != x)
{
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
} int main()
{
scanf("%d", &n);
for (int i = 1;i <= n;i++)
{
int num, g;
scanf("%d", &num);
while (num--)
{
scanf("%d", &g);
G[i].push_back(n+g);
}
}
int g;
for (int i = 1;i <= n;i++)
{
scanf("%d", &g);
G[n+g].push_back(i);
}
for (int i = 1;i <= 2*n;i++)
{
if (Dfn[i] == 0)
Tarjan(i);
}
for (int i = 1;i <= n;i++)
{
set<int> st;
for (int j = 0;j < G[i].size();j++)
{
int node = G[i][j];
if (Fa[i] == Fa[node])
st.insert(node-n);
}
printf("%d", st.size());
set<int>::iterator it = st.begin();
while (it != st.end())
{
printf(" %d", *it);
it++;
}
printf("\n");
} return 0;
}

POJ-1904-King‘s Quest的更多相关文章

  1. 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 ...

  2. poj 1904 King's Quest

    King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每 ...

  3. POJ 1904 King's Quest(SCC的巧妙应用,思维题!!!,经典题)

    King's Quest Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 10305   Accepted: 3798 Ca ...

  4. Poj 1904 King's Quest 强连通分量

    题目链接: http://poj.org/problem?id=1904 题意: 有n个王子和n个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可 ...

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

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

  6. POJ 1904 King's Quest 强连通分量+二分图增广判定

    http://www.cnblogs.com/zxndgv/archive/2011/08/06/2129333.html 这位神说的很好 #include <iostream> #inc ...

  7. poj 1904 King's Quest tarjan求二分图的所有可选最大匹配边

    因为是完美匹配,所以每个点都已经匹配了,那么如果要选择一条别的边,增光路的最后必定找到原来所匹配的点,加上匹配的边,那么就是一个环.所以可选边在一个强连通分量里. #include <iostr ...

  8. POJ 1904 King's Quest 强联通分量+输入输出外挂

    题意:国王有n个儿子,现在这n个儿子要在n个女孩里选择自己喜欢的,有的儿子可能喜欢多个,最后国王的向导给出他一个匹配.匹配有n个数,代表某个儿子和哪个女孩可以结婚.已知这些条件,要你找出每个儿子可以和 ...

  9. [poj 1904]King's Quest[Tarjan强连通分量]

    题意:(当时没看懂...) N个王子和N个女孩, 每个王子喜欢若干女孩. 给出每个王子喜欢的女孩编号, 再给出一种王子和女孩的完美匹配. 求每个王子分别可以和那些女孩结婚可以满足最终每个王子都能找到一 ...

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

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

随机推荐

  1. sql server 字符串拆分

    最近项目调取存储的时候直接传入string 作为in的查询范围,结果报错了,深思之后才发现,数据库对于传进来的String,并不是我们想的直接可以作为参数,而是作为一个整体,而in是需要一个类似arr ...

  2. PC端网页嵌入百度地图

    1 打开百度地图生成器: http://api.map.baidu.com/lbsapi/creatmap/ 2 设置好了之后,点击获取代码,将代码粘贴到文件中保存为html文件 参考网址:https ...

  3. JAVA 内存的那些事

    (转载)固然Java屏蔽了一下内存细节,但是有时候,了解一下这些常识还是有好处的,特别是一些口试,总是盯着这些玩意不放手. JVM启动以后,会分配两类内存区域,一类用于开发职员使用,比如保存一些变量, ...

  4. freebsd 隐藏ssh版本号

    方案一: vi /etc/ssh/sshd_config VersionAddendum 为空或者no或者别的信息 /etc/rc.d/sshd restart 方案二: https://kram.n ...

  5. Elastic Stack学习

    原文链接 Elastic Stack简称ELK,在本教程你将学习如何快速搭建并运行Elastic Stack. 首先你要安装核心开源产品: Elasticsearch: Kibana: Beats: ...

  6. 微服务简历V1.0

    张三 电话:xxx-xxxx-xxxx      邮箱: xxxxxxx@qq.com 年龄:x岁        籍贯:江苏 求职意向:java开发工程师   期望薪资:面议 专业技能 熟练使用Ecl ...

  7. lua基础学习(一)

    设计目的: 为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能.   特性: 1.编译之后仅仅100k,可以很方便嵌入别的程序里. 2.可扩张性,Lua提供了非常易于使用的扩展接口和机制:由宿 ...

  8. 第八周课程报告&&实验报告六

    Java实验报告 班级 计科一班 学号 20188390 姓名 宋志豪 实验四 类的继承 实验目的 理解异常的基本概念: 掌握异常处理方法及熟悉常见异常的捕获方法. 实验要求 练习捕获异常.声明异常. ...

  9. java监控

    参考: https://www.cnblogs.com/smail-bao/p/6027756.html

  10. Maven - 配置setting.xml

    1.配置本地库路径 <localRepository>C:/fyliu/mvn/repo</localRepository> 2.配置中央仓库 <mirror> & ...