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 ...
随机推荐
- Vue解决项目白屏
第一步: vue-cli项目根目录下面新建Vue.config.js文件 proxy反向代理 module.exports = { devServer: { proxy: { ...
- asp.net core-2.在vs2017中创建asp.net core应用程序
今天我们用vs2017创建一个asp.net core 的应用程序,打开vs2017 点击:文件—>项目,选择asp.net core web 应用程序 点击确定 红框内就昨天用控制台去创建的应 ...
- java代码检出打包
这里先提下前提,就是有个维护的(可能有二期的一个项目),后端是Java,由于很久都不做Java,剩下的只是不多了.之前做的Java容器要么是tomcat,要么接触过新的spring cloud.从来没 ...
- (二)SpringBoot之springboot开发工具的使用以及springboot插件的功能
一.springboot开发工具的使用 1.1 在项目中添加springoot开发工具 1.2 功能 修改代码后点击保存自动重启 二.springboot插件的功能 2.1 maven配置 <p ...
- Sublime Text 开发神器相关 插件安装 功能介绍
无法安装更多见http://blog.csdn.net/freshlover/article/details/44261229/ Sublime Text 3 安装插件管理 Package Contr ...
- Docker启动Elasticsearch报错java.nio.file.AccessDeniedException
报错信息 Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes 问题分析 表面上是说容 ...
- 关于vue.js的部分总结
1.MVVM和MVC的区别: MVVM:是Model-View-ViewModel的简写,即模型-视图-视图模型 模型:后端传递的数据 试图:所看到的页面 视图模型:mvvm模式的核心,它是连接vie ...
- rabbit MQ 的环境及命令使用(一)
RabbitMQ依赖erlang,所以先安装erlang,然后再安装RabbitMQ; 先安装erlang,双击erlang的安装文件即可,然后配置环境变量: ERLANG_HOME=D:\Progr ...
- 【leetcode】566. Reshape the Matrix
原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...
- git日常命令
克隆项目:git clone addr dir 切换分支,分支并没有 git checkout -b production origin/production 分支已存在,单纯切换分支 git che ...