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,要求他们的一个儿子从一个房间走到 ...
随机推荐
- yii NAV x下拉
$menuItems[] = [ 'label' => "<img src='/images/small.jpg'>", 'url' => ['/site/ ...
- input type='number'时,maxlength属性无效
<input type="number" oninput="if(value.length>11)value=value.slice(0,11)" ...
- poj1850(组合数)
题目链接:http://poj.org/problem;jsessionid=B0D9A01EC0F1043088A37454B6CED469?id=1850 题意:给字符串编号,该字符串必须满足由小 ...
- suse glibcxx版本过高问题
实际开发中发现,suse11虽然glibc版本很低,只有2.11.3,但是glibcxx版本很高,达到了3.4.19.这里我需要降低glibcxx版本.所谓glibcxx版本,即libstdc++.s ...
- hdu 2089 数位dp
链接:https://vjudge.net/problem/23625/origin 中文,题目不用说了. 其实这题的数据很小,所以直接暴力也可以过,但是还是要学会数位dp,因为并不是每一题的数据都会 ...
- 安装scrapy时遇到的问题
会报错,安装这个试试: pip install cryptography --force-reinstall
- The Attention Merchants
Title: The Attention Merchants (书评类文章) <注意力商人> attention 注意力 merchant 商人(零售商,强调通过贩卖物品获取利益) bu ...
- CentOS查看进程、杀死进程、启动进程等常用命令
关键字: linux 查进程.杀进程.起进程 1.查进程 ps命令查找与进程相关的PID号: ps a 显示现行终端机下的所有程序,包括其他用户的程序. ps -A 显示所有程 ...
- vue params和query传参区别
参考地址:https://blog.csdn.net/bluefish_flying/article/details/81011230 router.js中 路由设置这里, 当你使用params方法传 ...
- Python: PySide(PyQt)QMessageBox按钮显示中文
习惯了Delphi.c#调用系统MessageBox本地化显示,待用PySide调用时,Qt原生提示对话框,默认以英文显示. 如何本地化呢? 参考些资料,加以摸索,实现所需效果.并可根据此思路,设计自 ...