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. Skip List(跳跃表)原理详解与实现

    ref : https://dsqiu.iteye.com/blog/1705530   本文内容框架: §1 Skip List 介绍 §2 Skip List 定义以及构造步骤   §3 Skip ...

  2. Tree Generator™ CodeForces - 1149C (线段树,括号序列)

    大意: 给定括号序列, 每次询问交换两个括号, 求括号树的直径. 用[ZJOI2007]捉迷藏的方法维护即可. #include <iostream> #include <algor ...

  3. ant build打包

    使用ant build进行增量打包 <?xml version="1.0" encoding="gb2312"?> <project name ...

  4. C++万能头文件<bits/stdc++.h>的内容与优缺点

    最近发现了一个C++的头文件bits/stdc++.h,听说这是一个几乎包含了所有C++库函数的头文件,就想更深入的了解一下,下面是头文件内容 // C++ includes used for pre ...

  5. 中国5级行政区域MySQL数据库库

    爬取国家统计局官网的行政区域数据,包括省市县镇村5个层级,其中港澳地区的数据只有3级;台湾地区4级;包含大陆地区的邮政编码和经纬度信息. 行政划分的总体结构是: 省->市(州)->县(区) ...

  6. C# explicit interface implementation(显式接口实现)

    C# explicit interface implementation 某个类要实现两个包含相同方法名的接口, 应该如何实现这两个方法? namespace ExplicitInterfaceImp ...

  7. opencv-01--图像的遍历

    遍历图像的4种方式 一.at<typename>(i,j) Mat类提供了一个at的方法用于取得图像上的点,它是一个模板函数,可以取到任何类型的图像上的点.下面我们通过一个图像处理中的实际 ...

  8. kong网关命令(一)

    上次在虚拟机里安装kong网关后,因为版本(1.4)太高,目前Kong Dashboard无法支持, 后续发现Git上有个开源工具Kong admin ui,下载源码并部署到NGINX. 但是发现使用 ...

  9. Pandas进阶之提升运行效率

    前言 如果你现在正在学习数据分析,或者正在从事数据分析行业,肯定会处理一些大数据集.pandas就是这些大数据集的一个很好的处理工具.那么pandas到底是什么呢?官方文档上说: " 快速, ...

  10. 解决IE8中select下拉列表文字上下不居中的问题

    对IE8及以下的浏览器设置padding属性,其他浏览器则设置line-height 属性