POJ-1904-King‘s Quest
链接:
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的更多相关文章
- 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 King's Quest
King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每 ...
- POJ 1904 King's Quest(SCC的巧妙应用,思维题!!!,经典题)
King's Quest Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 10305 Accepted: 3798 Ca ...
- Poj 1904 King's Quest 强连通分量
题目链接: http://poj.org/problem?id=1904 题意: 有n个王子和n个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可 ...
- POJ 1904 King's Quest ★(强连通分量:可行完美匹配边)
题意 有n个女生和n个男生,给定一些关系表示男生喜欢女生(即两个人可以结婚),再给定一个初始匹配,表示这个男生和哪个女生结婚,初始匹配必定是合法的.求每个男生可以和哪几个女生可以结婚且能与所有人不发生 ...
- 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 强联通分量+输入输出外挂
题意:国王有n个儿子,现在这n个儿子要在n个女孩里选择自己喜欢的,有的儿子可能喜欢多个,最后国王的向导给出他一个匹配.匹配有n个数,代表某个儿子和哪个女孩可以结婚.已知这些条件,要你找出每个儿子可以和 ...
- [poj 1904]King's Quest[Tarjan强连通分量]
题意:(当时没看懂...) N个王子和N个女孩, 每个王子喜欢若干女孩. 给出每个王子喜欢的女孩编号, 再给出一种王子和女孩的完美匹配. 求每个王子分别可以和那些女孩结婚可以满足最终每个王子都能找到一 ...
- POJ 1904 King's Quest (强连通分量+完美匹配)
<题目链接> 题目大意: 有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国王不满意,他要求大臣给他另一个表,每个王 ...
随机推荐
- Delphi DBGridEh导出Excel
unit Unit_DBGridEhToExcel; interface uses SysUtils, Variants, Classes, Graphics, Controls, Forms, Ex ...
- SVN的使用和问题解决方法总结
添加仓库之类的很简单,这里就不说了哈...不会的可以问问我,当然百度再快了..嘿嘿 1.从服务器Check Out代码: 2.提交代码: 3.你是不是今天和我一样纠结如何删除已经上传SVN的内容,其实 ...
- cocos2dx基础篇(24) 场景切换效果CCTransitionScene
[3.x] (1)去掉 "CC" (2)卡牌翻转 TransitionFlip 中的样式 tOrientation // //1: kCCTransitionOri ...
- 关于多线程efcore dbcontext 的解决方案。
首先我们大部分的efcore框架用的DbContext(或者封装的repo)都是底层注入的上下文容器实体. 然后Dbcontext不是线程安全的,也就是说,你在当前线程中,只能创建一个 DbConte ...
- 简述前后端项目RSA+AES加解密
一.登录机制 在项目中,我们可以大致得出一个登录的过程,主要分为 登录验证.登录保持.退出三个部分.登录验证是指客户端提供用户名和密码,向服务器提出登录请求,服务器判断客户端是否可以登录并向客户端确 ...
- [转帖]查看ubuntu 各系统的内核版本
查看ubuntu 各系统的内核版本 https://www.cnblogs.com/ranxf/p/6923311.html /etc/issue /proc/version 1.查看ubuntu版本 ...
- 【转帖】UDIMM、RDIMM、SODIMM以及LRDIMM的区别
转载自http://www.sohu.com/a/165343889_781333. DIMM简介 DIMM(Dual Inline Memory Module,双列直插内存模块)与SIMM(sing ...
- 【6.28校内test】T2 【音乐会】二重变革
[音乐会]二重变革[题目链接] T2其实是一道数学题,因为你看: 2MB??一共就可以存下个int,然鹅再看数据范围: 那么大是稳稳的不是TLE就是MLE了,所以肯定是数学题,而且是只需要存很少数据的 ...
- 初步学习jquery学习笔记(四)
Jquery HTML Jquery 捕获内容 什么是dom? DOM = Document Object Model(文档对象模型) 获取内容 text()获取所选元素的文本内容 html()获取所 ...
- 怎么编写properties文件
1. 注释 在properties中注释是采用#号开头的方式来进行注释的 2. 编写properties文件 在properties中,一行就是一个键值对,简单的理解就是一行可以保存一个变量,键和值之 ...