51nod 1076
* 无向图的割边将图分为不连通的两部分
* 对于是否有不想交的两条路径将s -> t 相连
* 只需判断是否处于同一部分
* Tarjan即可
#include <bits/stdc++.h> const int N = ; int Low[N], Dfn[N], Bel[N], Stack[N], topp;
struct Node {int u, v, nxt;} G[(int)1e5 + ];
int now, head[N];
int n, m;
bool vis[N]; #define gc getchar() inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline void Add(int u, int v) {
G[++ now].v = v; G[now].nxt = head[u]; head[u] = now;
} int clo, Bel_; void Tarjan(int u, int fa) {
Low[u] = Dfn[u] = ++ clo;
Stack[++ topp] = u; vis[u] = ;
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(!Dfn[v]) {
Tarjan(v, u);
Low[u] = std:: min(Low[u], Low[v]);
} else
if(vis[v] && v != fa) Low[u] = std:: min(Low[u], Low[v]);
}
/*if(Low[u] == Dfn[u]) {
++ Bel_;
while(Low[Stack[topp]] == Low[u]) {
vis[Stack[topp]] = 0, Bel[Stack[topp]] = Bel_, topp --;
}
}*/
if(Low[u] == Dfn[u]) {
++ Bel_;
vis[u] = , Bel[u] = Bel_;
while(Stack[topp] != u) {
vis[Stack[topp]] = ;
Bel[Stack[topp]] = Bel_;
topp --;
} topp --;
}
} int main() {
n = read(), m = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i <= m; i ++) {
int u = read(), v = read();
Add(u, v), Add(v, u);
}
for(int i = ; i <= n; i ++) {
if(!Dfn[i]) Tarjan(i, );
}
int Q = read();
for(; Q; Q --) {
int s = read(), t = read();
if(Bel[s] == Bel[t]) puts("Yes");
else puts("No");
}
return ;
}
51nod 1076的更多相关文章
- 51nod 1076 2条不相交的路径(边双连通分量)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1076 题意: 思路: 边双连通分量,跑一遍存储一下即可. #includ ...
- AC日记——2条不相交的路径 51nod 1076
1076 2条不相交的路径 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 给出一个无向图G的顶点V和边E.进行Q次查询,查询从G的某个顶点V[s] ...
- 51nod 1076强连通
Tarjan算法来解这题.无向图可以转化为有向图来解决. #include<map> #include<queue> #include<stack> #includ ...
- 51nod 1076 2条不相交的路径
给出一个无向图G的顶点V和边E.进行Q次查询,查询从G的某个顶点V[s]到另一个顶点V[t],是否存在2条不相交的路径.(两条路径不经过相同的边) (注,无向图中不存在重边,也就是说确定起点和终点 ...
- tarjan相关模板
感性理解: o(* ̄︶ ̄*)o ^_^ \(^o^)/~ 1. 当根节点有大于两个儿子时,割掉它,剩下的点必然不联通(有两个强连通分量),则他为割点. 那么对于非根节点,在无向图G中,刚且仅当点u存 ...
- 51nod图论题解(4级,5级算法题)
51nod图论题解(4级,5级算法题) 1805 小树 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 她发现她的树的点上都有一个标号(从1到n),这些树都在空 ...
- 【51Nod 1244】莫比乌斯函数之和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...
- 51Nod 1268 和为K的组合
51Nod 1268 和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...
- 51Nod 1428 活动安排问题
51Nod 1428 活动安排问题 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活 ...
随机推荐
- Redis慢日志取出来
http://blog.chinaunix.net/uid-31396856-id-5758295.htmlhttps://blog.51cto.com/legehappy/2151986?sourc ...
- S03_CH07_AXI_VDMA_OV5640摄像头采集系统
S03_CH07_AXI_VDMA_OV5640摄像头采集系统 7.1概述 本章内容和<S03_CH06_AXI_VDMA_OV7725摄像头采集系统>只是摄像头采用的分辨率不同,其他原理 ...
- php底层变量存储
变量存储 php的变量使用一个结构体 zval来保存的,在Zend/zend.h中我们可以看到zval的定义 struct _zval_struct { /* Variable information ...
- kong 命令(五)plugin
介绍 plugin 插件 是运用在kong网关各模块的功能.在http请求或响应过程中执行的插件: 可以实现认证.负载.加密等功能. kong官网提供了一些插件:https://docs.konghq ...
- 关于element ui滚动条使用
element ui 自带的滚动条使用 在容器的直接外层添加 (需要展现滚动条的那一级) <el-scrollbar style="height:100%"></ ...
- FPGA上外挂DDR2&DDR3&MIG IP的使用记录
前言 当需要大容量数据存储及处理的时候,FPGA内部自带的存储资源是远远不够的,所以问题来了,怎么使用外带的DDR3? 首要问题在于DDR3是什么?有没有协议?当然只是需要用Xilinx MIG IP ...
- CodeForces 822C Hacker, pack your bags!
题意 给出一些闭区间(始末+代价),选取两段不重合区间使长度之和恰为x且代价最低 思路 相同持续时间的放在一个vector中,内部再对起始时间排序,从后向前扫获取对应起始时间的最优代价,存在minn中 ...
- c# 枚举和位标志
- 【异常】java.sql.SQLException: Could not retrieve transaction read-only status from server Query
1 详细异常 java.sql.SQLException: Could not retrieve transaction read-only status , ], [ChargingOrderRea ...
- Linux CentOS7 httpd 配置注释
本文首发:https://www.somata.work/2019/LinuxCentOShttpdConfigComment.html 如果没看懂可以去看看官方发布的文档 apache官方文档 co ...