BZOJ2140: 稳定婚姻(tarjan解决稳定婚姻问题)
2140: 稳定婚姻
Time Limit: 2 Sec Memory Limit: 259 MB
Submit: 1321 Solved: 652
[Submit][Status][Discuss]
Description
Input
Output
输出文件共包含n行,第i行为“Safe”(如果婚姻i是安全的)或“Unsafe”(如果婚姻i是不安全的)。
Sample Input
2
Melanie Ashley
Scarlett Charles
1
Scarlett Ashley
【样例输入2】
2
Melanie Ashley
Scarlett Charles
2
Scarlett Ashley
Melanie Charles
Sample Output
Safe
Safe
【样例输出2】
Unsafe
Unsafe
HINT
Source
思路:我们把原配中男到女连边,情侣关系中女到男连边。 然后求tarjan,如果原配在一个连通块里,说明他们的婚姻不稳定。
这其实也是一个定向问题,我们把男到女表示为原配,女到男表示为婚外情,那么如果原配在一个连通块里,他们一定在一个环里,环里有偶数个边。 即这个连通块的最大匹配数=非扩展边。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
map<string,int>mp; int tot;
int Laxt[maxn],Next[maxn],To[maxn],cnt,instk[maxn],q[maxn],top;
int a[maxn],b[maxn],scc[maxn],scc_cnt,low[maxn],dfn[maxn],times;
void add(int u,int v){ Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;}
void tarjan(int u)
{
low[u]=dfn[u]=++times; instk[u]=; q[++top]=u;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instk[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
scc_cnt++;
while(true){
int t=q[top--];
scc[t]=scc_cnt;
instk[t]=;
if(u==t) break;
}
}
}
int main()
{
int N,M; string s,t;
scanf("%d",&N);
rep(i,,N){
cin>>s>>t;
if(mp.find(s)==mp.end()) mp[s]=++tot;
if(mp.find(t)==mp.end()) mp[t]=++tot;
a[i]=mp[s]; b[i]=mp[t];
add(a[i],b[i]);
}
scanf("%d",&M);
rep(i,,M){
cin>>s>>t;
add(mp[t],mp[s]);
}
rep(i,,tot) if(!dfn[i]) tarjan(i);
rep(i,,N) puts(scc[a[i]]==scc[b[i]]?"Unsafe":"Safe");
return ;
}
BZOJ2140: 稳定婚姻(tarjan解决稳定婚姻问题)的更多相关文章
- 【BZOJ2140】稳定婚姻 Tarjan
[BZOJ2140]稳定婚姻 Description 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. ...
- luogu P1407 稳定婚姻-tarjan
题目背景 原<工资>重题请做2397 题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有 ...
- UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
- tarjan解决路径询问问题
好久没更新了,就更一篇普及组内容好了. 首先我们考虑如何用tarjan离线求出lca,伪代码大致如下: def tarjan(x): 将x标记为已访问 for c in x的孩子: tarjan(c) ...
- Python---Tkinter---贪吃蛇(稳定的外部环境,稳定的内心)
# 项目分析: - 构成: - 蛇 Snake - 食物 Food - 世界 World - 蛇和食物属于整个世界 class World: self.snake self.food - 上面代码不太 ...
- hdu1435 稳定婚姻问题
题意: Stable Match Special Judge Problem Description Network 公司的BOSS 说现在他们公司建立的信号发射站和接收站经常出现信号发送接收不稳定的 ...
- hdu1914 稳定婚姻问题
稳定婚姻问题就是给你n个男的,n个女的,然后给你每个男生中女生的排名,和女生心目中男生的排名,然后让你匹配成n对,使婚姻稳定,假如a和b匹配,c和d匹配,如果a认为d比b好,同时 ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
- poj 3478 The Stable Marriage Problem 稳定婚姻问题
题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们全部人的婚姻都是稳定的. 所谓不稳婚姻是说.比方说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的 ...
随机推荐
- Educational Codeforces Round 55 (Rated for Div. 2) Solution
A. Vasya and Book Solved. 三种方式取$Min$ #include <bits/stdc++.h> using namespace std; #define ll ...
- Polya
using namespace std; typedef long long LL; const int MAXN = 1e3 +10; const LL MOD = (LL)1 << 6 ...
- 《C++ Concurrency in Action》
http://wiki.jikexueyuan.com/project/cplusplus-concurrency-action/content/resources/resource.html
- SQL学习笔记四之MySQL数据操作
阅读目录 一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理 一 介绍 MySQL数据操作: DML =========== ...
- JavaScript 预编译(变量提升和函数提升的原理)
本文部分内容转自https://www.cnblogs.com/CBDoctor/p/3745246.html 1.变量提升 console.log(global); // undefined var ...
- ELF文件分析
(1)文件ELF.c (2)编译: gcc -c ELF_1.c -o ELF_1.o (3)显示文件类型: (4)查看大小: (5)转换为16进制 (6)显示各段信息 (7)分析
- pip安装tensorflow-gpu好慢怎么办
答:为pip换源,如换成清华源 cat ~/.pip/pip.conf(没有此文件,自行创建即可,然后加入以下内容) [global]index-url = https://pypi.tuna.tsi ...
- HttpContext.Current and Web Api
Using HttpContext.Current in WebApi is dangerous because of async HttpContext.Current gets the curre ...
- C++网络通信字节序问题
htonl(), ntohl(), htons(), ntohs() 函数 在C/C++写网络程序的时候,往往会遇到字节的网络顺序和主机顺序的问题.这是就可能用到htons(), ntohl(), n ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp
D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...