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 活 ...
随机推荐
- 解决django的后台管理界面添加中文内容乱码问题
在使用django migrate功能时,默认数据库的字符集不是utf8. 是latin 1,然后在后台管理model时,不允许有中文字符插入 解决方案: 在使用migrate建库之前先把数据库建立起 ...
- elasticsearch 集群详解
ES为什么要实现集群 在单台ES服务器节点上,随着业务量的发展索引文件慢慢增多,会影响到效率和内存存储问题等. 如果使用ES集群,会将单台服务器节点的索引文件使用分片技术,分布式的存放在多个不同的物理 ...
- hdu 1002 prime 模板
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- MVC5项目转.Net Core 2.2学习与填坑记录(1)
流程都是自己摸索,错误地方随便指正... 老项目过于臃肿,并且所有请求都是提交到一个api中,这样当api挂掉的时候,基本所有的项目都瘫痪掉了. 在4月底的时候,下决心将项目用微服务进行重写,刚开始的 ...
- js移动端回退监听 popstate
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- iOS有哪些数据类型/基本数据类型?
简述 本文主要探究使用OC作为iOS开发语言时,我们能使用哪些数据类型. 一切类型始于C. C语言的类型 基本数据类型: 基本数据类型(fundamental data types)也叫原始数据类型( ...
- 用python在屏幕上输出一个杨辉三角
在屏幕上输出一个杨辉三角,代码如下 def yanghui(): L = [1] while True: yield L L.append(0) L = [L[i - 1] + L[i] for i ...
- Server SAN
http://blog.sina.com.cn/s/blog_5946bd590102veni.html http://blog.sina.com.cn/s/blog_5946bd590102vemm ...
- IBM XIV
参考:https://www.doit.com.cn/p/author/xigua 参考:http://www.doit.com.cn/p/196056.html 图片说明: IBM XIV存储系统采 ...
- Nginx系列1.1:ubuntu16.04编译nginx-rtmp流媒体服务器
1.下载nginx和nginx-rtmp-module nginx官网:nginx.org tar.gz文件 解压缩命令: wget https://nginx.org/download/nginx- ...