A1258. 稳定婚姻(魏铭) tarjan
2016-04-18 11:03
357 查看
如果把婚♂外♂情看做有向边,可以发现Safe就是婚姻关系出现了环,那么tarjan一下看看所属强连通分量大小是否为1就好了。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=32005; int head ,next ,list ,dfn ,low ,q ,num ,ret ,belong ; bool inset ; int n,m,cnt,top,tot,scc,yzy; int ch [52]; char s[10]; inline void insert(int x,int y) { next[++cnt]=head[x]; head[x]=cnt; list[cnt]=y; } inline int get(char c) { if (c>='a'&&c<='z') return c-'a'; return c-'A'+26; } inline int add() { int len=strlen(s),c,now=0; for (int i=0;i<len;i++) { c=get(s[i]); if (ch[now][c]) now=ch[now][c]; else now=ch[now][c]=++yzy; } return now; } void dfs(int x) { dfn[x]=low[x]=++tot; inset[x]=true; q[++top]=x; for (int i=head[x];i;i=next[i]) if (!dfn[list[i]]) { dfs(list[i]); low[x]=min(low[x],low[list[i]]); } else if (inset[list[i]]) low[x]=min(low[x],dfn[list[i]]); if (dfn[x]==low[x]) { int i=-1; scc++; while (i!=x) { i=q[top--]; inset[i]=false; num[scc]++; ret[i]=scc; } } } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%s",s); belong[add()]=i; scanf("%s",s); belong[add()]=i; } scanf("%d",&m); for (int i=1;i<=m;i++) { scanf("%s",s); int u=belong[add()]; scanf("%s",s); int v=belong[add()]; insert(u,v); } for (int i=1;i<=n;i++) if (!dfn[i]) dfs(i); for (int i=1;i<=n;i++) if (num[ret[i]]>1) puts("Unsafe"); else puts("Safe"); return 0; }
相关文章推荐
- jboss中控制台jmx-console 登录的用户名和密码设置
- Android 开发笔记1 (MTK)
- bzoj 2219: 数论之神 数论
- Linux学习-杂
- 解决办法:Access restriction: The type JPEGImageEncoder is not accessible due to restriction
- Mysql错误:Ignoring query to other database解决方法
- 如何配置远程mysql服务器
- map/reduce类简单介绍
- Httplistener Access Denied
- 【python学习】python连接数据库实例
- Eclipse+Maven搭建Wbe项目(1)
- windows+virtualbox+xshell通信
- Laravel5 集合Collection
- 数组和指针的区别(a和&a以及&a[0]的区别)
- 堆排序
- JBOSS运程访问端口配置
- libevent-之安装
- 重读“javascript高级程序设计(第三版)”--原型
- Eclipse中jsp、js文件编辑时,卡死现象解决汇总
- 光栅化坐标映射公式