POJ - 2762 Going from u to v or from v to u? (强连通缩点+判断单向连通)
题意:判断一个有向图中的任意两点u、v,是否可以由其中一个点到达另一个点。
分析:这个问题转化以后就是:将该图强连通缩点后再判断其是否是单向连通的。缩点用Tarjan处理强连通分量。
有一个定理是这样的:一个有向图是单项连通的当且仅当其拓扑排序唯一。那么将这个子问题再转化为其缩点之后的图拓扑排序是否唯一。
如果一个有向图拓扑排序唯一,那么在根据入度求拓扑排序的过程中,不会有超过一个点在同一时刻同时为0。
#include<stack>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
typedef long long LL;
const int MAXN = 1e3+;
struct Edge{
int to,next;
}edges[MAXN<<],E[MAXN<<];
int ID,H[MAXN];
int dfn[MAXN],low[MAXN],sccno[MAXN],head[MAXN],tot,dfs_clock,scc_cnt,in[MAXN],sccnum[MAXN];
stack<int> S; void init()
{
ID=dfs_clock=tot=scc_cnt=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(in,,sizeof(in));
memset(sccno,,sizeof(sccno));
memset(head,-,sizeof(head));
memset(H,-,sizeof(H));
} void Tarjan(int u)
{
int v;
dfn[u]=low[u]=++dfs_clock;
S.push(u);
for(int i=head[u];~i;i=edges[i].next){
v = edges[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!sccno[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
++scc_cnt;
while(true){
int x = S.top();S.pop();
sccno[x] = scc_cnt;
if(x==u) break;
}
}
} void AddEdge(int u,int v)
{
edges[tot]=(Edge){v,head[u]};
head[u]=tot++;
}
void new_AddEdge(int u,int v){
E[ID]= (Edge){v,H[u]};
H[u]=ID++;
} bool Topo()
{
queue<int> Q;
for(int u=;u<=scc_cnt;++u){
if(!in[u]) Q.push(u);
}
if(Q.size()>) return false;
while(!Q.empty()){
int x = Q.front();Q.pop();
for(int i=H[x];~i;i=E[i].next){
int v =E[i].to;
in[v]--;
if(!in[v]) Q.push(v);
}
if(Q.size()>) return false;
}
return true;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,u,v;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
init();
for(int i=;i<=M;++i){
scanf("%d%d",&u,&v);
AddEdge(u,v);
}
for(int i=;i<=N;++i){
if(!dfn[i])Tarjan(i);
}
for(int u=;u<=N;++u){
for(int i=head[u];~i;i=edges[i].next){
int v=edges[i].to;
if(sccno[u]!=sccno[v]){
new_AddEdge(sccno[u],sccno[v]);
in[sccno[v]]++;
}
}
}
if(Topo()||scc_cnt==) printf("Yes\n");
else printf("No\n");
}
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的话,那么从此之后这两个岔路的点就不可 ...
- 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 ...
随机推荐
- [Tips] bzr Import error
# bazzar error on Mac Marvericks: 192:~ piaoger$ bzrbzr: ERROR: Couldn't import bzrlib and dependenc ...
- Jmeter接口测试系列之参数化方法
至于参数化的用途,我这里就不多说了,本文主要介绍最全.最强大的参数化方法,对参数化有一个彻底的认识,这里提供了多种参数化方法 1.jmeter参数化之用户变量 在测试计划里面添加一个用户自定义的变 ...
- android 性能測试CTS篇
CTS介绍 一.CTS简单介绍 CTS 全称Compatibility Test Suite兼容性測试工具. 当电子产品开发出来.并定制了自己的Android系统后,必需要通过最新的CTS检測.以保证 ...
- Castle 整合.NET Remoting
今天研究了一下Castle的Remoting Facility.记录如下: 微软以前使用COM/DCOM的技术来处理分布式系统架构,通过Client端的Proxy代理程序来呼叫远程Server机器上的 ...
- c++ new(不断跟新)
1.基础知识 /* 可以定义大小是0的数组,但不能引用,因为没有指向任何对象 new string[10]调用类的默认构造函数 new int[10]没有初始化,但new int[10]()会将数组初 ...
- 深入java虚拟机(一) 虚拟机内存结构
java虚拟机所管理的内存区域(运行时数据区)主要分为如下几个部分:堆(heap).方法区(method area).虚拟机栈(VM stack).本地方法栈(native method stack) ...
- [MongoDB]学习笔记--User管理
1. 创建一个超级用户 use admin db.createUser( { user: "adminUserName", pwd: "userPassword" ...
- 解决scipy安装(pip install scipy)失败,以及其他问题
解决scipy安装(pip install scipy)失败,以及其他问题 解决: 1.在scipy官方库中并没有适合Windows的python3.6相关版本,故需要在网址http://www.lf ...
- 通过手机浏览器打开APP或者跳转到下载页面.md
目录 通过手机浏览器打开APP或者跳转到下载页面 添加 schemes 网页设置 参考链接 通过手机浏览器打开APP或者跳转到下载页面 以下仅展示最简单的例子及关键代码 由于硬件条件有限,仅测试了 A ...
- Linux中搭建HTTP服务器
1.配置IP [root@localhost~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static ...