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,要求他们的一个儿子从一个房间走到 ...
随机推荐
- 3G开发遇到的问题
1.使用线程时,编译时要加上gcc xxx.c -o xxx -lpthread 2.分离字符串"abc,de,fgh" printf("%s",strtok ...
- 2018面向对象程序设计(Java)第15周学习指导及要求
2018面向对象程序设计(Java)第15周学习指导及要求 (2018.12.6-2018.12.9) 学习目标 (1) 掌握Java应用程序打包操作: (2) 了解应用程序存储配置信息的两种方法 ...
- sql-mybatis-多表查询不查的字段一定不要查
在多表联查的时候,这时用的是左外联(即如果右边的没有就查出左边的表) 如果右边的没有,那么在Navicat中查询出如下图 而在mybatis中运用同样的查询语句时,查询出来字段的也是这样 这时如果左表 ...
- numpy中的数学
1.dot,exp v = np.dot(arg1,arg2) #矩阵乘法 v2 = np.exp() # e的-x 次方
- hive 的理解
什么是Hive 转自: https://blog.csdn.net/qingqing7/article/details/79102691 1.Hive简介 Hive 是建立在 Hadoop 上的数据仓 ...
- Java计算计算活了多少天
Java计算计算活了多少天 思路: 1.输入你的出现日期: 2.利用日期转换,将字符串转换成date类型 3.然后将date时间换成毫秒时间 4.然后获取当前毫秒时间: 5.最后计算出来到这个时间多少 ...
- 创建java项目思路
一.搭建 1.创建搭建项目 2.创建分层 二.理解项目(理清总体思路) 1.是否有共同部分(过滤或者拦截) 常用量 (static) 2.搭建单表基本增(是否需要返回值) 删(条件) 查(条 ...
- python 取当前日期
import time time.strftime('%Y-%m-%d',time.localtime(time.time()))
- cetnos 7 增加新的硬盘
fdisk -l 查看新的硬盘是否挂载 如没有挂载 ls /sys/class/scsi_host/ 查看设备列表 echo "- - - " > /sys/class/sc ...
- df.dropna()函数和df.ix(),df.at(),df.loc()