Description

判断一个有向图是否对于任意两点 $x$,  $y$ 都有一条路径使$x - >y$或 $y - >x$

Solution

对于一个强联通分量内的点 都是可以互相到达的。

接下来我们考虑缩点后的DAG是否任意两点都有路径能使一点到达另一点。

然后我就不会了~~

我们进行一遍拓扑排序, 如果过程中有超过一个点的入度为 $0$ ,那么就不符合条件(仔细想想好像还是对的

Code

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define rd read()
#define R register
using namespace std; const int N = 1e5; int head[N], tot;
int Head[N], Tot;
int dfn[N], low[N], st[N] ,tp, vis[N], cnt;
int col_num, col[N], ru[N];
int n, m, T; queue<int> q; struct edge {
int nxt, to, fr;
}e[N << ], E[N << ]; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} void add(int u, int v) {
e[++tot].to = v;
e[tot].nxt = head[u];
e[tot].fr = u;
head[u] = tot;
} void Add(int u, int v) {
E[++Tot].to = v;
E[Tot].nxt = Head[u];
E[Tot].fr = u;
Head[u] = Tot;
} bool topsort() {
int num = ;
for(int i = ; i <= tot; ++i) {
int u = col[e[i].fr], v = col[e[i].to];
if(u == v) continue;
ru[v]++;
Add(u, v);
}
for(int i = ; i <= col_num; ++i)
if(ru[i] == ) num++, q.push(i);
if(num > )
return ;
for(int u; !q.empty();) {
if(num > ) return ;
u = q.front(); q.pop();
num--;
for(int i = Head[u]; i; i = E[i].nxt) {
int nt = E[i].to;
ru[nt]--;
if(!ru[nt])
num++, q.push(nt);
}
}
return ;
} void tarjan(int u) {
dfn[u] = low[u] = ++cnt;
st[++tp] = u;
vis[u] = ;
for(R int i = head[u]; i; i = e[i].nxt) {
int nt = e[i].to;
if(!dfn[nt]) {
tarjan(nt);
low[u] = min(low[u], low[nt]);
}
else if(vis[nt]) low[u] = min(low[u], dfn[nt]);
}
if(low[u] == dfn[u]) {
++col_num;
for(; tp; ) {
int z = st[tp--];
vis[z] = ;
col[z] = col_num;
if(z == u) break;
}
}
} void init() {
Tot = tp = tot = cnt = col_num = ;
memset(vis, ,sizeof(vis));
memset(dfn, , sizeof(dfn));
memset(col, , sizeof(col));
memset(head, , sizeof(head));
memset(low, , sizeof(low));
memset(st, , sizeof(tp));
memset(Head, , sizeof(Head));
memset(ru, , sizeof(ru));
while(!q.empty()) q.pop();
} int main()
{
T = rd;
for(; T; T--) {
init();
n = rd; m = rd;
for(int i = ; i <= m; ++i) {
int u = rd, v = rd;
add(u, v);
}
for(int i = ; i <= n; ++i)
if(!dfn[i]) tarjan(i);
if(topsort()) puts("Yes");
else puts("No");
}
}

POJ 2762 Going from u to v or from v to u?- Tarjan的更多相关文章

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

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

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

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

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

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

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

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

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

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

  9. POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17104   Accepted: 4594 Description In o ...

  10. poj 2762 Going from u to v or from v to u?

    题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...

随机推荐

  1. openlayers/// Puppeteer.js

    1.定位;https://viglino.github.io/ol3-ext/examples/map.interaction.geolocationdraw.html 2 .添加Overlay  d ...

  2. Shell 处理文件名中包含空格的文件

    最近在学Gradle, 使用git clone 命令下载了一些资料,但是文件名含有空格,看上去不是很舒服,因此想到用shell脚本对其进行批处理,去掉文件名中的空格,注意这里是把所有的空格全去掉 gi ...

  3. Retrofit 打印log时,中文会显示类似%E8%BE%BD字符

    https://blog.csdn.net/honghailiang888/article/details/54289632?utm_source=blogxgwz6 参照Android Retrof ...

  4. Trustin Lee

    Trustin Lee,MINA.Netty2通讯框架的作者韩国人,80年出生,8岁起在MSX迷你计算机上编写BASIC程序,爱好游戏编程以及使用汇编.C和C++解决编程问题,1998年获得韩国信息奥 ...

  5. Using Service Workers

    [Using Service Workers] 1.This is an experimental technology Because this technology's specification ...

  6. 转化为分组背包 zoj 3769

    题目链接:https://vjudge.net/problem/ZOJ-3769 题意:现在你要去打怪,你有13种装备,每件装备会有伤害和防御两种属性,一般来说,每种装备只可以装备一件,但是特别的,戒 ...

  7. OpenGL ES 2 for Android - A Quick Start Guide

    英文原版,语法很简单,带书签,带实例,很好的书,从头到尾讲述OpenGL强烈推荐!~~~ 扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com ...

  8. 食物链(带权&种类并查集)

    食物链 http://poj.org/problem?id=1182 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9326 ...

  9. 批处理(.bat脚本)基本命令语法

    .bat脚本基本命令语法   目录批处理的常见命令(未列举的命令还比较多,请查阅帮助信息)     1.REM 和 ::     2.ECHO 和 @     3.PAUSE     4.ERRORL ...

  10. 【C】C语言中的_exit()与exit()

    _exit()和exit()主要区别是一个退出进程会清理I/O缓冲区,一个直接结束进程进入到内核中. 举例说明: #include <stdio.h> /*demo01 程序只输出 hel ...