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个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国王不满意,他要求大臣给他另一个表,每个王 ...
随机推荐
- Service Mesh体验
前言# 计算机软件技术发展到现在,软件架构的演进无不朝着让开发者能够更加轻松快捷地构建大型复杂应用的方向发展.容器技术最初是为了解决运行环境的不一致问题而产生的,随着不断地发展,围绕容器技术衍生出来越 ...
- OpenCV 中获取图像或矩阵最大、最小值的简便方法
C++: void minMaxLoc(InputArray src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc ...
- 【推荐算法工程师技术栈系列】程序语言--Java
目录 JDK 初步 ArrayList LinkedList Vector Stack HashMap Hashtable LinkedHashMap TreeMap HashSet LinkedHa ...
- 问题记录 | 配置ubuntu18.04+cuda9.0+cudnn服务器tensorflow-gpu深度学习环境
因为实验室服务器资源有限,我被分配的服务器经常变化,但是常常就分到连显卡驱动以及cuda都没有装的服务器,真的很头疼,我已经配了四五台了,特此记录一下,以便以后直接照版本安装. Install nvi ...
- adobe Keychain mac
Keychain password access This question has been Answered. janec2070563 May 8, 2018 11:07 AM I consta ...
- Akka系列(三):监管与容错
前言...... Akka作为一种成熟的生产环境并发解决方案,必须拥有一套完善的错误异常处理机制,本文主要讲讲Akka中的监管和容错. 监管 看过我上篇文章的同学应该对Actor系统的工作流程有了一定 ...
- python 并发编程 阻塞IO模型
阻塞IO(blocking IO) 在linux中,默认情况下所有的socket都是blocking,一个典型的读操作流程大概是这样: 当用户进程调用了recvfrom这个系统调用,kernel内核就 ...
- vs资源视图加载失败
原因:引用了未知的资源,通过打开时报的错可以定位然后修改
- 超详细的CentOS8Linux新功能介绍 镜像iso下载安装
在这文章中,我们会使用图解的方式演示 CentOS 8 的安装方法. CentOS8中软件和系统管理请参照https://www.cnblogs.com/fusheng11711/p/11809963 ...
- 小记---------linux远程连接集群内其他机器mysql库
mysql -h -u maxwell -p#10.0.15.145 远程机器ip#-P 注意是大写P 端口#-u 用户#-p 密码