链接:

https://vjudge.net/problem/POJ-2762

题意:

In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn't know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?

给一个有向图,求判断能否选任意的两个点,有一条从u到v或v到u的路径。

思路:

先缩点形成一个DAG。只有当这个DAG是一条链的时候,才有u到v的路径。

代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
const int MAXN = 1e3+10; stack<int> St;
vector<int> G_new[MAXN], G[MAXN];
int Vis[MAXN];
int Dfn[MAXN], Low[MAXN];
int Fa[MAXN];
int Dis[MAXN];
int n, m;
int times, cnt; void Tarjan(int x)
{
Dfn[x] = Low[x] = ++times;
Vis[x] = 1;
St.push(x);
for (int i = 0;i < G[x].size();i++)
{
int node = G[x][i];
if (Dfn[node] == 0)
{
Tarjan(node);
Low[x] = min(Low[x], Low[node]);
}
else if (Vis[node])
Low[x] = min(Low[x], Dfn[node]);
}
if (Dfn[x] == Low[x])
{
cnt++;
while (St.top() != x)
{
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
Fa[St.top()] = cnt;
Vis[St.top()] = 0;
St.pop();
}
} void Init()
{
for (int i = 1;i <= n;i++)
G[i].clear(), G_new[i].clear();
memset(Vis, 0, sizeof(Vis));
memset(Dis, 0, sizeof(Dis));
memset(Dfn, 0, sizeof(Dfn));
times = cnt = 0;
while (St.size())
St.pop();
} bool Tupo()
{
int sum = 0;
stack<int> tu;
for (int i = 1;i <= cnt;i++)
if (Dis[i] == 0)
tu.push(i);
while (!tu.empty())
{
if (tu.size() > 1)
return false;
int x = tu.top();
sum++;
tu.pop();
for (int i = 0;i < G_new[x].size();i++)
{
int node = G_new[x][i];
if (--Dis[node] == 0)
tu.push(node);
}
}
return sum == cnt;
} int main()
{
int t;
int l, r;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
Init();
for (int i = 1;i <= m;i++)
{
scanf("%d%d", &l, &r);
G[l].push_back(r);
}
for (int i = 1;i <= n;i++)
if (Dfn[i] == 0)
Tarjan(i);
for (int i = 1;i <= n;i++)
{
for (int j = 0;j < G[i].size();j++)
{
int node = G[i][j];
if (Fa[i] != Fa[node])
{
Dis[Fa[node]]++;
G_new[Fa[i]].push_back(Fa[node]);
}
}
}
if (Tupo())
printf("Yes\n");
else
printf("No\n");
} return 0;
}

POJ-2762-Going from u to v or from v to u(强连通, 拓扑排序)的更多相关文章

  1. poj 2762 Going from u to v or from v to u? 【 强连通 拓扑排序】

    给出n个点,m条边,问是否任意两点u,v,是否满足u能够到达v,或者v能够到达u 自己写的时候以为缩一下点,然后再判断一下能不能拓扑排序就可以了 但是--wa--- 后来看了这篇题解 http://e ...

  2. POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

  3. 拓扑排序 POJ 1094 Sorting It All Out

    题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...

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

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

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

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

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

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

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

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

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

随机推荐

  1. 2 Configuring SAP ERP Sales and Distribution -introduction to SAP

    First Steps in SAPWe’ll now discuss some of the basic menus, screens, and transactions that you need ...

  2. 如何实现在Eclipse导入c3p0

    1 右键项目->Properties->Java Build Path->Libraries->Add External JARs...-> c3p0-0.9.5.2.j ...

  3. css换行用省略号代替

    css换行用省略号代替,也可以说是长标题的文章可以使用简单的CSS样式实现省略号控制显示. 一般的文字截断(适用于内联与块): .text-overflow{ display:block;/*内联对象 ...

  4. Mysql事务特性

    事务概念 事务可由一条sql或者一组sql组成.事务是访问并更新数据库中各种数据项的一个程序执行单元. 事务会把数据库从一种一致状态转换为另一种一致状态.在数据提交工作时,可以确保要么所有修改都已经保 ...

  5. [Vuejs] 给ref赋值需要注意的问题

    1.简单赋值 <div ref="refCon"></div> 访问方式: this.$refs.refCon 2.循环赋值,相同名称 <div v- ...

  6. python 并发编程 协程池

    协程池 from gevent.pool import Pool from gevent import monkey;monkey.patch_all() import gevent from gev ...

  7. excel常用公式--关联匹配类

    index: 根据位置返回单元格的值. match: 根据单元格的值返回位置. index+match替代vlookup rank:返回一列数字的数字排位.数字的排位是其相对于列表中其他值的大小.

  8. 直线的Bresenham算法

    在实验课上用自己的算法画直线被diss效率低 花了半天时间看了下Bresenham算法真

  9. css精灵图使用

    1. 精灵技术的使用 CSS 精灵其实是将网页中的一些背景图像整合到一张大图中(精灵图),然而,各个网页元素通常只需要精灵图中不同位置的某个小图,要想精确定位到精灵图中的某个小图,就需要使用CSS的b ...

  10. windows环境下搭建mysql主从

    参考 windows环境下mysql主从配置 1. 环境 参数 说明 主库所在的操作系统 win7 主库的版本 mysql-5.6.46-winx64 主库的ip地址 127.0.0.1 主库的端口 ...