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] 稳定婚姻的更多相关文章

  1. luogu P1407 稳定婚姻-tarjan

    题目背景 原<工资>重题请做2397 题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有 ...

  2. 【HDU1914 The Stable Marriage Problem】稳定婚姻问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...

  3. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  4. BZOJ2140: 稳定婚姻

    题解: 题意就是求二分图的必须边. 我们有结论: 在残量网络上跑tarjan,对于一条边(u,v) 如果该边满流||scc[u]==scc[v],那么该边是可行边. 因为如果scc[u]==scc[v ...

  5. 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)

    The Stable Marriage Problem   Description The stable marriage problem consists of matching members o ...

  6. 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)

    Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...

  7. 【稳定婚姻问题】【HDU1435】【Stable Match】

    2015/7/1 19:48 题意:给一个带权二分图  求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...

  8. poj 3487 稳定婚姻

    /** 稳定婚姻:男生不停的求婚,女生不停地拒绝 **/ #include <iostream> #include <queue> #include <cstdio> ...

  9. 稳定婚姻问题和Gale-Shapley算法(转)

    什么是算法?每当有人问作者这样的问题时,他总会引用这个例子:假如你是一个媒人,有若干个单身男子登门求助,还有同样多的单身女子也前来征婚.如果你已经知道这些女孩儿在每个男孩儿心目中的排名,以及男孩儿们在 ...

随机推荐

  1. Apache Tomcat 安装与配置教程

    JDK的安装与配置 1. 从官网下载JDK https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213315 ...

  2. Linux每隔1秒kill掉cpu大于50%的进程

    1.新建/test/killcpu.sh shell脚本 并授予权限0755#!/bin/bashps axf -o "pid %cpu" | awk '{if($2>=50 ...

  3. C#常用数据结构

    常碰到的几种数据结构:Array,ArrayList,List,LinkedList,Queue,Stack,Dictionary<K,T>: 1.数组是最简单的数据结构.其具有如下特点: ...

  4. requests Use body.encode('utf-8') if you want to send it encoded in UTF-8

    基本环境 使用 requests 模块发送 post 请求,请求体包含中文报错 系统环境:centos7.3 python版本:python3.6.8 请求代码: // 得到中文 param_json ...

  5. vue 2.0 + 如何实现加入购物车,小球飞入的动画

    github源码地址:https://github.com/13476075014/node-vue/tree/master/mynodeproject/13.sell/sell 在移动端经常会有加入 ...

  6. Mysql基础学习_Windows版(一)

    1.Mysql简介 Mysql是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性.所谓的关系型数据库,是建立在关系模型基础上的 ...

  7. iOS有哪些数据类型/基本数据类型?

    简述 本文主要探究使用OC作为iOS开发语言时,我们能使用哪些数据类型. 一切类型始于C. C语言的类型 基本数据类型: 基本数据类型(fundamental data types)也叫原始数据类型( ...

  8. Mac上搭建Web服务器--Apache

    局域网搭建 Web 服务器测试环境,因为Mac OS X 自带了 Apache 和 PHP 环境,我们只需要简单的启动它就行了. 1.命令:sudo apachectl start Apache服务器 ...

  9. pytorch之nn.Conv1d详解

    转自:https://blog.csdn.net/sunny_xsc1994/article/details/82969867,感谢分享 pytorch之nn.Conv1d详解

  10. CentOS7安装CDH 第五章:CDH的安装和部署-CDH5.7.0

    相关文章链接 CentOS7安装CDH 第一章:CentOS7系统安装 CentOS7安装CDH 第二章:CentOS7各个软件安装和启动 CentOS7安装CDH 第三章:CDH中的问题和解决方法 ...