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的更多相关文章

  1. POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

    职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...

  2. poj 2762(强连通+判断链)

    题目链接:http://poj.org/problem?id=2762 思路:首先当然是要缩点建新图,由于题目要求是从u->v或从v->u连通,显然是要求单连通了,也就是要求一条长链了,最 ...

  3. 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. ...

  4. 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:  ...

  5. poj 2762(强连通分量+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...

  6. POJ 2762 Going from u to v or from v to u? (判断单连通)

    http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...

  7. [ 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 ...

  8. 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 ...

  9. [强连通分量] 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 ...

  10. POJ 2762 tarjan缩点+并查集+度数

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494 ...

随机推荐

  1. 移动构造函数应用最多的地方就是STL中(原文详解移动构造函数)

    移动构造函数应用最多的地方就是STL中 给出一个代码,大家自行验证使用move和不适用move的区别吧 #include <iostream> #include <cstring&g ...

  2. python03-break、continue、for循环、数据bytes类型、字符串与字节的关系、变量指向与深浅拷贝、set集合、文件操作

    目录: 1.break.continue 2.for循环 3.数据bytes类型 4.字符串与字节的关系 5.变量指向与深浅拷贝 6.set集合 7.文件操作 一.break.continue bre ...

  3. Linux自动运维工具Ansible的使用

    Linux自动运维工具Ansible的使用 我们熟悉这个工具后, 可以很轻松的安装k8s. 一.介绍 ansible - run a task on a target host(s) Ansible是 ...

  4. (二十五)JSP九大内置对象(转)

    --转载自孤傲苍狼博客 一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质 ...

  5. Intellij IDEA 快捷键大全【转】

    IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表达式时按 “!”键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文 ...

  6. elementui禁用树形结构全部复选框

    需求:编辑回显数据后,禁用树形结构复选框,不可选中,无复选框也不可选中 <el-tabs v-model="activeName" @tab-click="hand ...

  7. css之弹性盒模型

    弹性盒子(Flexible Box/filebox)是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式.引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子 ...

  8. window.open打开一个新空白页面,不会自动刷新【解决方案】

    调用js方法: function BuildPostForm(fm, url, target) { var e = null, el = []; if (!fm || !url) return e; ...

  9. ubuntu16.04安装zabbix-server3.4

    一.安装前环境准备 部署zabbix需要安装apache,mysql和php sudo apt-get install apache2 sudo apt-get install mysql-serve ...

  10. CentOS7数据库架构之NFS+heartbeat+DRBD(亲测,详解)

    目录 参考文档 理论概述 DRBD 架构 NFS 架构部署 部署DRBD 部署heartbeat 部署NFS及配合heartbeat nfs切记要挂载到别的机器上不要为了省事,省机器 参考文档 htt ...