POJ 2762 Going from u to v or from v to u?- Tarjan
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的更多相关文章
- 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 Going from u to v or from v to u?(强连通分量+拓扑排序)
职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...
- 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 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 ...
- 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 ...
- poj 2762 Going from u to v or from v to u?
题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...
随机推荐
- vue 组件中this指向
今天开始学习慕课网的“去哪网”app开发,之前用学了一段时间对vue还是没有深刻理解透,先在开始要从新开始学习vue,今天学的第一堂课是vue 中v-model.v-for的简单例子,以前改变dom中 ...
- TensorFlow常用函数
[1]卷积层(Convolutional Layer),构建一个2维卷积层,常用的参数有 conv = tf.layers.conv2d( inputs=pool, filters=64, kerne ...
- NCBI之gene系列
1.基因系列中的data索引 2.基因ID之间的转换 对于生信,依托于别人的工具不如自己动手,由于研究发表的滞后性,往往很多工具提供的转换并不是最新的,况且开发者水平也参差不齐,理解原理才能让你来去自 ...
- msf客户端渗透(五):注册表
先获取到一个session 上传nc到被攻击主机上 建立一个键值 创建一个策略 kali上查看是否成功创建键值 后台开启cmd 查看防火墙的策略 打开防火墙的端口 添加一条防火墙策略 在win7上查看 ...
- 使用Fiddler查看APP的请求接口、接口参数和返回值的方法
1.下载Fiddler,然后安装成功后. 2.开启代理的设置 3.查看电脑的ip, 4.建立一个wifi局域网,什么360wifi,猎豹wifi,腾讯wifi都可以,用安装手机接入到这个局域网的wif ...
- SpringBoot配置logback
1.在SpringBoot中已经集成了logback.在pom.xml中加入以下spring-boot-starter依赖,使用默认版本即可: <dependency> <group ...
- centos 6 和centos 7 系统下vnc配置
一. VNC 服务的大概介绍: VNC (Virtual Network Console)是虚拟网络控制台的缩写.它 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的. ...
- RedHat 更新CentOS Yum源(转)
经测试,可用.转自:https://www.cnblogs.com/tangsen/p/5151994.html 一.随笔引言 1.1随笔内容: 1.RedHat 配置Centos yum源 2.yu ...
- Codeforces Beta Round #42 (Div. 2)
Codeforces Beta Round #42 (Div. 2) http://codeforces.com/contest/43 A #include<bits/stdc++.h> ...
- f5会话保持
B/S架构的建议选择 inset cookie :c/s架构的 建议选择 sorce ip 1. Introduction to session persistence profiles Using ...