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. 跨年操作--new Date()

    //时间在2017/12/31 17:00 --- 2018/1/1 06:00区间,提示用户无法操作公告. //time.js (function(){ var date = new Date(); ...

  2. 2018面向对象程序设计(Java)第15周学习指导及要求

    2018面向对象程序设计(Java)第15周学习指导及要求 (2018.12.6-2018.12.9)   学习目标 (1) 掌握Java应用程序打包操作: (2) 了解应用程序存储配置信息的两种方法 ...

  3. JMeter学习(十五)JMeter测试Java(转载)

    转载自 http://www.cnblogs.com/yangxia-test 目的:对Java程序进行测试 目录 一.核心步骤 二.实例 三.JMeter Java Sampler介绍 四.自带Ja ...

  4. Could not load conf for core new_core 解決方法

    new_core: org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Could not load ...

  5. 【pycharm】使用过程的相关问题

    背景:安装scrapy后在cmd里可以正常import scrapy模块,但是在pycharm里不可以(python2.7) 问题:cmd中能正常导入模块,在pycharm报错 原因:pycharm里 ...

  6. BeautifulSoup中查找元素 select() 和find()区别

    从html中查找元素,之前一般都用find(),查找符合条件的第一个,如下 f = open(file, 'r') # 读取文件内容content = f.read()soup= BeautifulS ...

  7. GIRDVIEW 控件绑定数据后 后台c#控制隐藏某列

    gv_EnterpriseInfo.DataSource = pageResult.Data; gv_EnterpriseInfo.DataBind(); 之后加判断条件: if (true) { g ...

  8. leetcode 443. String Compression

    下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...

  9. PTA 7-9 旅游规划(SPFA)

    有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条路径都是最短的,那么需要输出最便 ...

  10. Inno Setup界面拉伸

    1.源起: 源于一个安装包的广告定制.广告客服提供的图片太大,inno setup默认尺寸容不下它,需要扩充,拉宽安装界面尺寸. 以inno setup所附带例子说事,其默认尺寸如下: 2.Scale ...