感性理解:

o(* ̄︶ ̄*)o  ^_^ \(^o^)/~

1.

当根节点有大于两个儿子时,割掉它,剩下的点必然不联通(有两个强连通分量),则他为割点。

那么对于非根节点,在无向图G中,刚且仅当点u存在一个可遍历到的后代v,且点v无法走回点u的前辈时,点u就为割点。

洛谷P3388

 1 #include<iostream>
2 #include<cstdio>
3 using namespace std;
4 const int N=1e5+2;
5 int n,m,idx,cnt,tot,h[N];
6 struct node{
7 int v,ne;
8 }e[N*3];
9 int ans[N];
10 void add(int u,int v)
11 {
12 tot++;e[tot]=(node){v,h[u]};h[u]=tot;
13 }
14 int dfn[N],low[N];
15 bool cut[N];
16 void tarjan(int u,int fa)
17 {
18 dfn[u]=low[u]=++idx;
19 int ch=0;
20 for(int i=h[u],rr;i;i=e[i].ne)
21 {
22 rr=e[i].v;
23 if(!dfn[rr])
24 {
25 tarjan(rr,u);
26 low[u]=min(low[u],low[rr]);
27 if(low[rr]>=dfn[u] && u!=fa) cut[u]=1;
28 ch++;
29 }
30 low[u]=min(low[u],dfn[rr]);
31 }
32 if(ch>=2&&u==fa) cut[u]=1;
33 }
34 int main()
35 {
36 scanf("%d%d",&n,&m);
37 for(int i=1,x,y;i<=m;++i)
38 {
39 scanf("%d%d",&x,&y);
40 add(x,y);add(y,x);
41 }
42 tot=0;
43 for(int i=1;i<=n;++i)
44 if(!dfn[i]) tarjan(i,i);
45 for(int i=1;i<=n;++i)
46 if(cut[i]) ans[++tot]=i;
47 printf("%d\n",tot);
48 for(int i=1;i<=tot;++i) printf("%d ",ans[i]);
49 return 0;
50 }

割点模板

=================================================================================================

ZOJ2588

 1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=1e5+2;
4 int n,m,idx,cnt,tot,h[N];
5 struct node{
6 int v,ne,id,fg; //会有重边,所以标记一下,
7 }e[N*3];//注意,图中可能有重边,只要顶点u和v之间有重边,则这些重边任何一条都不可能是割边。
8 int ans[N],fg;
9 void add(int u,int v,int id)
10 {
11 fg=1;
12 for(int i=h[u];i;i=e[i].ne)
13 if(e[i].v==v) e[i].fg=0,fg=0;
14 tot++;e[tot]=(node){v,h[u],id,fg};h[u]=tot;
15 }
16 int dfn[N],low[N];
17 void tarjan(int u,int fa)
18 {
19 dfn[u]=low[u]=++idx;
20 for(int i=h[u],rr;i;i=e[i].ne)
21 {
22 rr=e[i].v;
23 if(rr==fa) continue;
24 if(!dfn[rr])
25 {
26 tarjan(rr,u);
27 low[u]=min(low[u],low[rr]);
28 if(low[rr]>dfn[u] && e[i].fg) ans[++tot]=e[i].id;
29 }
30 low[u]=min(low[u],dfn[rr]);
31 }
32 }
33 int T;
34 int main()
35 {
36 scanf("%d",&T);
37 while(T--)
38 {
39 tot=0;idx=0;
40 scanf("%d%d",&n,&m);
41 for(int i=1;i<=n;++i) h[i]=0,dfn[i]=0,low[i]=0;
42 for(int i=1,x,y;i<=m;++i)
43 {
44 scanf("%d%d",&x,&y);
45 add(x,y,i);add(y,x,i);
46 }tot=0;
47 for(int i=1;i<=n;++i)
48 if(!dfn[i]) tarjan(i,i);
49 printf("%d\n",tot);
50 sort(ans+1,ans+tot+1);
51 if(tot)
52 {
53 printf("%d",ans[1]);
54 for(int i=2;i<=tot;++i) printf(" %d",ans[i]);
55 printf("\n");
56 }
57 if(T) printf("\n");
58 }
59 return 0;
60 }
61 /*
62 Sample Input
63 2
64
65 6 7
66 1 2
67 2 3
68 2 4
69 5 4
70 1 3
71 4 5
72 3 6
73
74 10 16
75 2 6
76 3 7
77 6 5
78 5 9
79 5 4
80 1 2
81 9 8
82 6 4
83 2 10
84 3 8
85 7 9
86 1 4
87 2 4
88 10 5
89 1 6
90 6 10
91
92 Sample Output
93 2
94 3 7
95
96 1
97 4
98
99 */

割边

==================================================================================================

至于边-双连通分量是指在一个无向图中两点间至少有两条路径,且路径中的边不同。

边-双连通分量中一定没有桥。而桥是指当删去这个边时,连通块的数量会增加。

51nod 1076

 1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<cstring>
5 #include<string>
6 #include<cmath>
7 using namespace std;
8 const int N=3e5+10;
9 int n,m,tot,h[N];
10 struct node{
11 int v,ne;
12 }e[N*4];
13 int dfn[N],low[N],idx;
14 void add(int u,int v)
15 {
16 tot++;e[tot]=(node){v,h[u]};h[u]=tot;
17 }
18 bool in[N];
19 int Q,x,y,bg[N],s[N],top;
20 void dfs(int u,int fa)
21 {
22 dfn[u]=low[u]=++idx;
23 s[++top]=u;in[u]=1;
24 for(int i=h[u],rr;i;i=e[i].ne)
25 {
26 rr=e[i].v;
27 if(rr==fa) continue;
28 if(!dfn[rr])
29 {
30 dfs(rr,u);
31 low[u]=min(low[u],low[rr]);
32 }else if(in[rr]) low[u]=min(low[u],low[rr]);
33 }
34 if(low[u]==dfn[u])
35 {
36 do{
37 in[s[top]]=0;
38 bg[s[top]]=u;
39 }while(s[top--]!=u);
40 }
41 }
42 int main()
43 {
44 scanf("%d%d",&n,&m);
45 for(int i=1;i<=m;++i)
46 {
47 scanf("%d%d",&x,&y);
48 add(x,y);add(y,x);
49 }
50 for(int i=1;i<=n;++i)
51 if(!dfn[i]) dfs(i,i);
52 scanf("%d",&Q);
53 while(Q--)
54 {
55 scanf("%d%d",&x,&y);
56 if(bg[x]==bg[y]) puts("Yes");
57 else puts("No");
58 }
59 return 0;
60 }

代码

参考:百度百科

tarjan相关模板的更多相关文章

  1. Tarjan总结(缩点+割点(边)+双联通+LCA+相关模板)

    Tarjan求强连通分量 先来一波定义 强连通:有向图中A点可以到达B点,B点可以到达A点,则称为强连通 强连通分量:有向图的一个子图中,任意两个点可以相互到达,则称当前子图为图的强连通分量 强连通图 ...

  2. 多项式FFT相关模板

    自己码了一个模板...有点辛苦...常数十分大,小心使用 #include <iostream> #include <stdio.h> #include <math.h& ...

  3. Tarjan 算法&模板

    Tarjan 算法 一.算法简介 Tarjan 算法一种由Robert Tarjan提出的求解有向图强连通分量的算法,它能做到线性时间的复杂度. 我们定义: 如果两个顶点可以相互通达,则称两个顶点强连 ...

  4. HDU 2586 ( LCA/tarjan算法模板)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:n个村庄构成一棵无根树,q次询问,求任意两个村庄之间的最短距离 思路:求出两个村庄的LCA,d ...

  5. tarjan算法模板

    终于能自己完整的打下来 #include<cstdio> #include<cstring> #include<iostream> #include<vect ...

  6. hdu1269 有向图强连通 【Tarjan】(模板)

    <题目链接> 题目大意: 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称 ...

  7. tarjan【模板】缩点

    传送门:https://www.luogu.org/problemnew/show/P3387 首先呢,tarjan找一个图的强连通分量是基于对图的dfs的.这中间开了一个dfn[]代表dfs序,还有 ...

  8. tarjan强连通模板

    #include<stdio.h>//用于求一个图存在多少个强连通分量 #include<string.h> #include<vector> using name ...

  9. LCA离线算法Tarjan的模板

    hdu 2586:题意:输入n个点的n-1条边的树,m组询问任意点 a b之间的最短距离 思路:LCA中的Tarjan算法,RMQ还不会.. #include <stdio.h> #inc ...

随机推荐

  1. 【Linux开发】【Qt开发】tslibs的配置(触摸屏没有,HDMI屏幕):Qt界面响应USB鼠标

    s3c2416   linux qt4.x 由于触摸屏坏了,板子只能用鼠标了,结果以前可以用的现在鼠标突然不能用了 为此交叉编译了qt的多个版本,也换过根文件系统,以为是tslib版本的问题,却发现q ...

  2. 【Qt开发】解决Qt5.7.0中文显示乱码的问题

    [Qt开发]解决Qt5.7.0中文显示乱码的问题 亲测可用: 乱码主要是编码格式的问题,这里可以通过Edit菜单中选择当前文档的编码方式,选择按照UTF-8格式保存,然后输入对应的中文,保存,然后运行 ...

  3. echars 柱状图正常状态 --》二次封装

    <template> <!-- 柱状图 正常 1. 调用页面引入 import EcharsColumnNormal from '@/components/echarsColumnN ...

  4. Cassandra视图

    一.简介 Cassandra作为一个P2P结构的NOSQL数据库,使用与HBase不同的去中心化架构,在国外使用非常广泛,受欢迎程度甚至在Hbase之上.今天这篇文章介绍Cassandra在视图方面设 ...

  5. JavaDoc注释

    标签 说明 JDK 1.1 doclet 标准doclet 标签类型 @author 作者 作者标识 √ √ 包. 类.接口 @version 版本号 版本号 √ √ 包. 类.接口 @param 参 ...

  6. linux中忘记mysql用户root密码解决方案

    1.vim /etc/my.cnf[mysqld]skip-grant-tables ##追加此行,跳过权限表, 2.重启mysqlsystemctl restart mysqld 3.mysql 登 ...

  7. linux下安装msgpack,yar,phalcon

    安装msgpack扩展 下载:http://pecl.php.net/package/msgpack cd /usr/local tar zxvf msgpack-0.5.5.tgz cd msgpa ...

  8. 【学习总结】快速上手Linux玩转典型应用-第3章-CentOS的安装

    课程目录链接 快速上手Linux玩转典型应用-目录 目录 1. 虚拟机是什么 2. 在虚拟机中安装CentOS 3. 云服务器介绍 ================================== ...

  9. linux后台运行springboot项目

    首先需要进到自己springboot项目的根目录,然后执行如下linux命令 nohup java -jar 自己的springboot项目.jar >日志文件名.log 2>&1 ...

  10. Nginx服务项的基本配置

    由于Nginx配置项较多,把他们按照用户使用时的预期功能分为以下4类: 1.调试,定位问题的配置项 2.正常运行必备配置项 3.优化性能配置项 4.事件类配置项 1. 用于调试进程,定位问题的配置项 ...