POJ 2762 Going from u to v or from v to u? (判断单连通)
http://poj.org/problem?id=2762
题意:
给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u。
思路:
判断图是否是单连通的。
首先来一遍强连通缩点,重新建立新图,接下来我们在新图中找入度为0的点,入度为0的点只能有1个,如果有多个那么这些个点肯定是不能相互到达的。
如果只有一个入度为0的点,走一遍dfs判断新图是否是单链,如果有分支,那么分支上的点肯定是不能相互到达的。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL; const int maxn=+; int n,m; int x[maxn],y[maxn];
int in[maxn];
int flag; vector<int> G[maxn];
vector<int> new_G[maxn]; stack<int> S;
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt; void dfs(int u)
{
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if (!pre[v])
{
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}
else if(!sccno[v])
lowlink[u] = min(lowlink[u], pre[v]);
}
if (pre[u] == lowlink[u])
{
scc_cnt++;
for(;;)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if (x == u) break;
}
}
} void find_scc(int n)
{
dfs_clock = scc_cnt = ;
memset(pre, , sizeof(pre));
memset(sccno, , sizeof(sccno));
for (int i = ; i <= n; i++)
if (!pre[i]) dfs(i);
} void dfs2(int u)
{
if(flag==) return;
if(new_G[u].size()>) {flag=;return;}
if(new_G[u].size()!=) dfs2(new_G[u][]);
} void rebulid()
{
for(int i=;i<=scc_cnt;i++) {new_G[i].clear();in[i]=;}
for(int i=;i<m;i++)
{
if(sccno[x[i]]!=sccno[y[i]])
{
new_G[sccno[x[i]]].push_back(sccno[y[i]]);
in[sccno[y[i]]]=;
}
}
int num=;
int pos;
for(int i=;i<=scc_cnt;i++)
{
if(in[i]==) {num++;pos=i;}
if(num>=) {flag=;return;}
}
if(flag) dfs2(pos);
} int main()
{
//freopen("D:\\input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
flag=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) G[i].clear();
for(int i=;i<m;i++)
{
scanf("%d%d",&x[i],&y[i]);
G[x[i]].push_back(y[i]);
}
find_scc(n);
rebulid();
if(flag) puts("Yes");
else puts("No");
}
return ;
}
POJ 2762 Going from u to v or from v to u? (判断单连通)的更多相关文章
- 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的话,那么从此之后这两个岔路的点就不可 ...
- [ 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,要求他们的一个儿子从一个房间走到 ...
随机推荐
- CHMOD命令怎么用?
文件/目录权限设置命令:chmod 这是Linux系统管理员最常用到的命令之一,它用于改变文件或目录的访问权限.该命令有两种用法: 用包含字母和操作符表达式的文字设定法 其语法格式为:chmod [w ...
- Unity3D笔记三 物理引擎
一.物理引擎 1.物理引擎就是模拟真实世界中物体碰撞.跌落等反应,通过Ballance.愤怒的小鸟来理解什么是物理引擎.Unity的物理引擎使用的是NviDIA的PhysX.2.选中一个游戏对象,主菜 ...
- Malformed \uxxxx encoding
今天碰到个问题. FATAL [btir.server.ServerStartup:54] - <java.lang.IllegalArgumentException: Malformed \u ...
- 模拟退火算法A Star not a Tree?(poj2420)
http://write.blog.csdn.net/postedit A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Tot ...
- 170530、java 迭代hashmap常用的三种方法
@SuppressWarnings("rawtypes") public class HashMapDemo { //hashMap遍历 public static void ma ...
- #pragma once含义及用法
#pragma once是一个比较常用的C/C++杂注,只要在头文件的最开始加入这条杂注,就能够保证头文件只被编译一次. #pragma once是编译器相关的,有的编译器支持,有的编译器不支持,具体 ...
- Oracle Schema Objects——伪列ROWID Pseudocolumn(ROWNUM、ROWID)
Oracle Schema Objects Oracle Schema Objects——Tables——Oracle Data Types Oracle伪列 在Oracle数据库之中为了实现完整的关 ...
- has to be escaped using backslash to be included in string value\n
[root@d myssh]# cat ESdel_bulk_file1544528090.log{"error":{"root_cause":[{" ...
- Drawing Graphs using Dot and Graphviz
Drawing Graphs using Dot and Graphviz Table of Contents 1. License 2. Introduction 2.1. What is DOT? ...
- 邮件服务器Postfix的管理 重启php-fpm
Postfix邮件系统安装与配置:Postfix,Cyrus-IMAP,Cyrus-sasl,Dovecot和SPFhttp://www.freehao123.com/postfix-cyrus/Ce ...