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. mysql查看表结构命令,如下:

    desc 表名; show columns from 表名; describe 表名; show create table 表名;

  2. Java并发(思维导图)

    1,线程状态转换 无限期等待: 限期等待: 线程生命流程: 2,实现方式 代码实现样例[三种方式]: package com.cnblogs.mufasa.demo2; import java.uti ...

  3. dotnetcore下解压zip文件,解决中文文件名乱码问题

    (迄今为止网上那些说的用Encoding.Default解决中文文件名乱码的都不能真正解决问题!) 1.在程序开始处 Encoding.RegisterProvider(CodePagesEncodi ...

  4. Vue字符串padStart和padEnd方法

    padStart()用于头部补全,padEnd()用于尾部补全. 'a'.padStart(3, '0') // '00a' 'x'.padEnd(5, 'ab') // 'xabab' 'x'.pa ...

  5. 如何解决js地址栏中传递中文乱码的问题

    目标要求: 实现从A页面跳转至B页面,B页面接收A页面通过地址栏传递过来的中文参数,中文不能出现乱码. A页面部分代码(传递参数): var title = "这是中文"; var ...

  6. Java Socket编程----网络基础

    详见:https://www.cnblogs.com/rocomp/p/4790340.html Java最初是作为网络编程语言出现的,其对网络提供了高度的支持,使得客户端和服务器的沟通变成了现实,而 ...

  7. SmartBinding实现DataSet与ListView的绑定及同步显示

    kbmMW 5.10.10发布了,这个版本解决了我提出的问题,当对DataSet增删记录时,ListView能够同步显示.下面看看具体的实现代码. 为了解决上面的问题,作者为IkbmMWBinding ...

  8. CSS 样式表{二}

    1 选择器的优先级 选择器的优先主要考虑选择器的权重 可以将各种选择器的权重以数值来表示,数值越大,优先级越高 选择器 权重值 标签selector 1 类选择器 10 ID选择器 100 行内样式 ...

  9. Flutter——AspectRatio组件

    AspectRatio 的作用是根据设置调整子元素 child 的宽高比. AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,widget 的高度是由宽度和比率决定的,类似于 Bo ...

  10. 2.数码相框-编码(ASCII/GB2312/Unicode)介绍

    转载:https://www.cnblogs.com/lifexy/p/8485634.html 在上章-学习了数码相框的框架分析(1)了 本章主要内容如下: 1)熟悉ASCII/GB2312/Uni ...