POJ-2762-Going from u to v or from v to u(强连通, 拓扑排序)
链接:
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(强连通, 拓扑排序)的更多相关文章
- poj 2762 Going from u to v or from v to u? 【 强连通 拓扑排序】
给出n个点,m条边,问是否任意两点u,v,是否满足u能够到达v,或者v能够到达u 自己写的时候以为缩一下点,然后再判断一下能不能拓扑排序就可以了 但是--wa--- 后来看了这篇题解 http://e ...
- POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)
[题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...
- 拓扑排序 POJ 1094 Sorting It All Out
题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...
- 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 ...
随机推荐
- 嵌套的JsonObject与JSONArray的取值---JSON中嵌套JSONArray
在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的.就跟if else语句一样,如果if中套if,if中再套if,写的 ...
- lnmp 环境下 部署 laravel 项目
出现错误 Warning: require(): open_basedir restriction in effect. File(/xxxx/vendor/autoload.php) is not ...
- python学习之面向对象(二)
6.2 类的空间角度研究类 6.2.1 添加对象属性 [总结]对象的属性不仅可以在__init__里面添加,还可以在类的其他方法或者类的外面添加. class A: address = '召唤师峡谷' ...
- LeetCode.997-找到镇法官(Find the Town Judge)
这是悦乐书的第373次更新,第400篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第234题(顺位题号是997).在一个城镇,有N个人从1到N标记.有传言说其中一个人是秘 ...
- MySQL:添加用户、删除用户、授权、远程访问、修改密码
1.创建用户 #test表示你要建立的用户名,后面的123456表示密码, #localhost限制在固定地址localhost登陆 CREATE USER test@localhost IDENTI ...
- Mac_Navicat Premium连接MySQL错误2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(../Frameworks/caching_sha2_password.so, 2): image not found
mac下MySql启动连接报错:Authentication plugin ‘caching_sha2_password’ cannot be loaded: dlopen(/usr/local/my ...
- HCL 试验1
PC端配置:配置ip地址 交换机配置:①创建VLAN system-view vlan 10 vlan 20 ②配置PC端接口 interface gi 1/0/1 port link-type ac ...
- Logistic回归基础篇之梯度上升算法
代码示例: import numpy as np import matplotlib.pyplot as plt def loadDataSet(): dataMat = [];labelMat = ...
- js 数组去重方法总结
var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, ...
- empty() 为true
//empty() 为truevar_dump(empty(0));var_dump(empty('0'));var_dump(empty(array()));var_dump(empty(null) ...