[Luogu] 稳定婚姻
https://www.luogu.org/problemnew/show/1407
tarjan求一下强连通分量,然后判断一下两个人是否在同一强连通分量中
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map> using namespace std; inline int read() {
int sum();
char ch(getchar());
for(; ch<''||ch>''; ch=getchar());
for(; ch>=''&&ch<=''; sum=sum*+(ch^),ch=getchar());
return sum;
} struct edge {
int e;
edge *n;
edge():e(),n(NULL) {}
} a[],*pre[]; int tot; inline void insert(int s,int e) {
a[++tot].e=e;
a[tot].n=pre[s];
pre[s]=&a[tot];
} int n,m;
char s1[],s2[];
map<string,int>ma;
int cnt,top,qlt;
int dfn[],low[],sta[],bl[];
bool vis[]; inline void tarjan(int u) {
dfn[u]=low[u]=++cnt;
sta[++top]=u;
vis[u]=;
for(edge *i=pre[u]; i; i=i->n) {
int e(i->e);
if(!dfn[e]) {
tarjan(e);
low[u]=min(low[u],low[e]);
} else if(vis[e])
low[u]=min(low[u],dfn[e]);
}
if(low[u]==dfn[u]) {
int tmp;
++qlt;
while() {
tmp=sta[top--];
vis[tmp]=;
bl[tmp]=qlt;
if(tmp==u)
break;
}
}
} int main()
{
memset(pre,NULL,sizeof(pre));
n=read();
for(int i=; i<=n; ++i) {
scanf("%s%s",s1,s2);
insert((i<<)-,i<<);
ma[s1]=(i<<)-,ma[s2]=i<<;
}
m=read();
for(int i=; i<=m; ++i) {
scanf("%s%s",s1,s2);
insert(ma[s2],ma[s1]);
}
for(int i=; i<=(n<<); ++i) if(!dfn[i]) tarjan(i);
for(int i=; i<=n; ++i)
if(bl[(i<<)-]==bl[i<<]) puts("Unsafe");
else puts("Safe");
return ;
}
[Luogu] 稳定婚姻的更多相关文章
- luogu P1407 稳定婚姻-tarjan
题目背景 原<工资>重题请做2397 题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有 ...
- 【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- BZOJ2140: 稳定婚姻
题解: 题意就是求二分图的必须边. 我们有结论: 在残量网络上跑tarjan,对于一条边(u,v) 如果该边满流||scc[u]==scc[v],那么该边是可行边. 因为如果scc[u]==scc[v ...
- 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
The Stable Marriage Problem Description The stable marriage problem consists of matching members o ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
- 【稳定婚姻问题】【HDU1435】【Stable Match】
2015/7/1 19:48 题意:给一个带权二分图 求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...
- poj 3487 稳定婚姻
/** 稳定婚姻:男生不停的求婚,女生不停地拒绝 **/ #include <iostream> #include <queue> #include <cstdio> ...
- 稳定婚姻问题和Gale-Shapley算法(转)
什么是算法?每当有人问作者这样的问题时,他总会引用这个例子:假如你是一个媒人,有若干个单身男子登门求助,还有同样多的单身女子也前来征婚.如果你已经知道这些女孩儿在每个男孩儿心目中的排名,以及男孩儿们在 ...
随机推荐
- Spring邮件服务:Maven + Spring SMTP Mail (可发送附件)
1 spring-smtp-mail.xml <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- 面试经典算法:快速排序Golang实现
Golang快速排序 定义 快速排序由C. A. R. Hoare在1962年提出.快速排序是对冒泡排序的一种改进,采用了一种分治的策略. 基本思想 通过一趟排序将要排序的数据分割成独立的两部分,其中 ...
- XML-RPC-3XML-RPC 与 XML-RPC 服务器类
http://codeigniter.org.cn/user_guide/libraries/xmlrpc.html XML-RPC 与 XML-RPC 服务器类 CodeIgniter 的 XML- ...
- [书籍翻译] 《JavaScript并发编程》第三章 使用Promises实现同步
本文是我翻译<JavaScript Concurrency>书籍的第三章 使用Promises实现同步,该书主要以Promises.Generator.Web workers等技术来讲解J ...
- el-table表格错误问题
.el-table--border th.gutter:last-of-type { display: block!important; width: 17px!important; } 如果不行,则 ...
- # URL异常检测
(Isolation Forest无监督)这个算法是随机森林的推广. iTree树构造:随机选一个属性,再随机选该特征的一个值,对样本进行二叉划分,重复以上操作. iTree构建好了后,就可以对数据进 ...
- Mysql基础学习_Windows版(一)
1.Mysql简介 Mysql是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性.所谓的关系型数据库,是建立在关系模型基础上的 ...
- 关于NSOperationQueue,一个容易让初学者误解的问题
凡是学习NSOperationQueue的人,都会遇到setMaxConcurrentOperationCount这个函数.在网上的许多博文中,都将setMaxConcurrentOperationC ...
- 5.Redis的持久化
Redis中数据的持久化有两种方式:RDB(Redis DataBsse) 和 AOF(Append Only File),默认采取的是RDB方式 RDB 1.是什么:在指定的时间间隔内将内存中的数据 ...
- 【大数据】Clickhouse基础知识
第1章 ClickHouse概述 1.1 什么是ClickHouse ClickHouse 是俄罗斯的Yandex于2016年开源的列式存储数据库(DBMS),主要用于在线分析处理查询(OLAP),能 ...