POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)
【题意】:
有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立。注意是或者,不是且。
【思路】:
先考虑,x->y或者y->x是什么意思,如果是且的话就简单了,就直接判断整个图是不是强联通图即可,但是这道题是或,那么可以随手画出一个DAG
比如1->3, 2->3 这样很明显是不行的,1,2没有联通,那么如果是这样1->2->3 就可以了,或者是1->2->3->1,这样也是可以的。
很显然,整个图中某一时刻入度同时为0的点的数量num≤1即可以找出合理方案,反之当某一时刻num>1时则不能。
考虑到图不可能是3个点这么简单,可以先求出强联通分量,因为分量中的每个点都可以相互到达,然后将每个联通分量缩点,这样就不用分别考虑。然后
对于每个缩点的入度判断可以使用topo排序判断。到此完毕。
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std; const int maxn = + ;
const int maxm = + ;
int n, m, t;
struct edge{
int to, next;
} ed[maxm<<];
int head[maxn<<], tot, cnt;
int dfn[maxn], low[maxn], num, stak[maxn], c[maxn];
int indu[maxn<<];
bool instack[maxn], vis[maxn];
inline void init(){
memset( head ,-, sizeof(head) );
memset( dfn, , sizeof(dfn) );
memset( low, , sizeof(low) );
memset( indu, , sizeof(indu) );
memset( vis, , sizeof(vis) );
tot = ;
stak[] = cnt = num = ;
} inline void add( int u, int v ){
ed[++tot].to = v;
ed[tot].next = head[u];
head[u] = tot;
} inline int min( int a, int b ){
return a<b ? a:b;
} inline void tarjan( int x ){ //求强联通
dfn[x] = low[x] = ++num;
instack[x] = ;
stak[++stak[]] = x;
for( int i=head[x]; i!=-; i=ed[i].next ){
int y = ed[i].to;
if( !dfn[y] ){
tarjan(y);
low[x] = min(low[x], low[y]);
}else if(instack[y]) low[x] = min(low[x], dfn[y]);
}
if( low[x]==dfn[x] ){
++cnt;
do{
int p = stak[stak[]];
c[p] = cnt+n;
instack[p] = ;
} while(stak[stak[]--]!=x );
}
} inline void scc( int x ){ //缩点
if( vis[x] ) return;
vis[x] = ;
for( int i=head[x]; i!=-; i=ed[i].next ){
int y = ed[i].to;
if( c[x]!=c[y] ){
add( c[x], c[y] );
indu[c[y]] ++;
}
scc(y);
}
} inline bool topo(){ //topo判断某一时刻有无多个点的入度同时为0
queue<int> q;
for( int i=; i<=cnt; i++ )
if( !indu[i+n] ) q.push(i+n);
if( q.size()> ) return ;
while( !q.empty() ){
int x = q.front(); q.pop();
for( int i=head[x]; i!=-; i=ed[i].next ){
int y =ed[i].to;
indu[y]--;
if(!indu[y]) q.push(y);
if( q.size()> ) return ;
}
}
return ;
} int main(){
// freopen("in.txt", "r", stdin);
scanf("%d", &t);
while( t-- ){
init();
scanf("%d%d", &n, &m);
for( int i=; i<m; i++ ){
int u, v;
scanf("%d%d", &u, &v);
add( u, v );
}
for( int i=; i<=n; i++ )
if( !dfn[i] ) tarjan(i);
for( int i=; i<=n; i++ ) scc(i);
if( topo() ) puts("Yes");
else puts("No");
} return ;
}
POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)的更多相关文章
- Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Description I ...
- 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?【强连通分量缩点+拓扑排序】
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15812 ...
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- poj 2762 强连通缩点+拓扑排序
这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...
- POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序
题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...
- POJ 2186 Popular Cows(强联通+缩点)
Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...
- Java实现判断单联通(强连通缩点+拓扑排序)Going from u to v or from v to u
Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
随机推荐
- 图解 Java 垃圾回收机制,写得非常好!
阅读本文大概需要 3.7 分钟. 翻译:Rhys_Lee, AzureSora, 溪边九节, 小小菜鸟鸡 blog.csdn.net/zl1zl2zl3/article/details/9090408 ...
- VS2019 NetCore3.0找寻grpc模板
今天研究Google的grpc框架的时候看到了https://www.cnblogs.com/yilezhu/p/10631420.html这哥们儿的博客 按照博客的内容找寻grpc模板,始终找不到A ...
- .NET技术webconfig加密操作
webconfig 加密 aspnet_regiis.exe -pef secion physical_directory -prov provider section表示要加密的配置节 phy ...
- 小程序报错 “渲染层错误” Expect END descriptor with depth 0 but get another
项目中有几个页面在控制台出现这个“渲染层错误”,虽然不影响业务操作,怕存在潜在风险,今天抽时间找了下原因,解决这个问题. 控制台报错日志如下: (中国标准时间) 渲染层错误 Error: Expect ...
- CentOS 7.6 安装Python3.7.2 多版本共存
CentOS 7.6 默认安装了 Python 2.7.5 准备环境 yum install git gcc gcc-c++ make automake autoconf libtool pcre p ...
- 腾讯云IPv6技术拿了个一等奖!1.5亿人已经用上
中国通信学会在其官网上公布了2019年中国通信学会科学技术奖的评选结果,腾讯云和中国移动通信集团,中国信息通信研究院.以及华为联合申报的“移动互联网IPv6技术攻关及规模应用”项目荣获今年科学技术一等 ...
- Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组
Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组 import time dt=time.strptime('2019-08-08 11:32:23', ...
- Scala 数组操作之Array、ArrayBuffer以及遍历数组
ArrayBuffer 在Scala中,如果需要类似于Java中的ArrayList这种长度可变的集合类,则可以使用ArrayBuffer. // 如果不想每次都使用全限定名,则可以预先导入Array ...
- asp.net core 和consul
consul集群搭建 Consul是HashiCorp公司推出的使用go语言开发的开源工具,用于实现分布式系统的服务发现与配置,内置了服务注册与发现框架.分布一致性协议实现.健康检查.Key/Valu ...
- 什么是SQL ?
SQL 1.什么是SQL ? Structured Query Languange:结构化查询语言 其实就是定义了操作所有关系型数据库的规则.每一种数据库操作的方式存在不一样的地方,称为“方言”. 2 ...