poj 2762
Tarjan + Topsort
Tarjan 缩点
Topsort 判断
Topsort 判断:
在DAG中
若初始状态下存在多于1个入度为0的点
则说明这些 入度为0的点之间不会有路径可达
若不存在入度为0的点,则状态为Yes
若只存在1个入度为0的点,将该点指出的边删除
继续上述判断
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> const int N = , M = N * ; #define gc getchar() inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} int head[N], head_2[N], cnt;
struct Node {int u, v, nxt;};
Node G[M], E[M];
int In[N], n, m;
int Low[N], Dfn[N], Stack[N], Belong[N], Scc, Tim, topp;
bool vis[N]; inline void Add_1(int u, int v) {G[++ cnt].v = v; G[cnt].nxt = head[u]; head[u] = cnt;}
inline void Add_2(int u, int v) {E[++ cnt].v = v; E[cnt].nxt = head_2[u]; head_2[u] = cnt; In[v] ++;} inline void Clear() {
memset(head, -, sizeof head);
memset(head_2, -, sizeof head_2);
memset(In, , sizeof In);
memset(Low, , sizeof Low);
memset(Dfn, , sizeof Dfn);
memset(vis, , sizeof vis);
topp = cnt = Scc = Tim = ;
} inline void Init() {
n = read(), m = read();
for(int i = ; i <= m; i ++) Add_1(read(), read());
} void Tarjan(int x) {
Low[x] = Dfn[x] = ++ Tim;
Stack[++ topp] = x; vis[x] = ;
for(int i = head[x]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(!Dfn[v]) {
Tarjan(v);
Low[x] = std:: min(Low[x], Low[v]);
} else if(vis[v]) Low[x] = std:: min(Low[x], Low[v]);
}
if(Dfn[x] == Low[x]) {
vis[x] = , Belong[x] = ++ Scc;
while(Stack[topp] != x) {
vis[Stack[topp]] = , Belong[Stack[topp]] = Scc;
topp --;
} topp --;
}
} inline void Rebuild() {
cnt = ;
for(int u = ; u <= n; u ++)
for(int i = head[u]; ~ i; i = G[i].nxt)
if(Belong[u] != Belong[G[i].v]) Add_2(Belong[u], Belong[G[i].v]);
} void Topsort() {
if(Scc == ) {puts("Yes"); return ;}
int Ans(), flag;
for(int i = ; i <= Scc; i ++) if(!In[i]) Ans ++, flag = i;
if(Ans > ) {puts("No"); return ;}
int temp = Scc;
for(; temp; temp --) {
Ans = ;
for(int i = head_2[flag]; ~ i; i = E[i].nxt) {
int v = E[i].v;
In[v] --;
if(!In[v]) Ans ++, flag = v;
}
if(Ans > ) {puts("No"); return ;}
if(!Ans) {puts("Yes"); return ;}
}
puts("Yes"); return ;
} void Work() {
Clear();
Init();
for(int i = ; i <= n; i ++) if(!Dfn[i]) Tarjan(i);
Rebuild();
Topsort();
} int main() {
int t = read();
for(; t; t --, Work());
return ;
}
poj 2762的更多相关文章
- POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)
职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...
- poj 2762(强连通+判断链)
题目链接:http://poj.org/problem?id=2762 思路:首先当然是要缩点建新图,由于题目要求是从u->v或从v->u连通,显然是要求单连通了,也就是要求一条长链了,最 ...
- POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...
- poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
- poj 2762(强连通分量+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...
- POJ 2762 Going from u to v or from v to u? (判断单连通)
http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...
- [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?
题目链接: http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory L ...
- POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)
id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...
- [强连通分量] POJ 2762 Going from u to v or from v to u?
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17089 ...
- POJ 2762 tarjan缩点+并查集+度数
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15494 ...
随机推荐
- spring cloud微服务实践四
spring cloud的hystrix还有一个配搭的库hystrix-dashboard,它是hystrix的一款监控工具,能直观的显示hystrix响应信息,请求成功率等.但是hystrix-da ...
- 记录我第一篇用Markdown写的Blog
Markdown的介绍 喝水不忘挖井人-Markdown的创造者 Markdown 最初是由 John Gruber 和 Aaron Swartz 于 2004 年共同设计的(在这里插一句,Aaron ...
- ASP.NET 使用 SyndicationFeed 输出 Rss
以前生成 RSS 都是使用拼接 Xml 的方式生成的,不仅麻烦而且还不规范. #region 输出指定分类编号的消息源内容... /// <summary> /// 输出指定分类编号的消息 ...
- 3、java基础:抽象类与接口的区别
抽象类 我们都知道在面向对象的领域一切都是对象,同时所有的对象都是通过类来描述的,但是并不是所有的类都是来描述对象的.如果一个类没有足够的信息来描述一个具体的对象,而需要其他具体的类来支撑它,那么这样 ...
- Servlet实现图片读取显示
1.导入jar包:commons-io-1.4.jar 2.index.jsp: <%@ page language="java" import="java.uti ...
- phpstorm+xdebug+mvc
前一段时间自己琢磨出来,今天又给忘了,还去t00ls发帖.... 写到这里备忘 拿这个yxcms举例子 版本: yxcms1.2.1 源码:http://pan.baidu.com/s/1pJM1CP ...
- 如何搭建一个基于nuxt.js的项目
介绍 nuxt.js(中文官方文档)是vue.js的一个通用型应用框架,有了之前搭建vue项目的过程之后,搭建一个nuxt项目就会十分简单. 搭建步骤 1.打开命令提示符,进入到相关文件夹下: 2.使 ...
- C++ 项目和资源导引
值得学习的C语言开源项目 注意:本文转载自:https://blog.csdn.net/a110658684/article/details/78862348 - 1. Webbench Webben ...
- docker 部署 elasticsearch + elasticsearch-head + elasticsearch-head跨域问题 + IK分词器
0. docker pull 拉取elasticsearch + elasticsearch-head 镜像 1. 启动elasticsearch Docker镜像 docker run -di ...
- Reverse数组以及大O表达式
这篇主要是对数组实现一个倒排序(比如数组1.2.3,最后输出3.2.1),当然实现这个功能是非常easy的事,但是这里需要引入另外一个很重要的概念-----如何计算一个算法的时间复杂度并学会用大O表达 ...