当前位置:网站首页>Bridge of undirected graph
Bridge of undirected graph
2022-07-02 13:18:00 【chengqiuming】
One The finishing touch
Time stamp :dfn[u] Representation node u Sequence number of depth first traversal .
Traceability point :low[u] Representation node u or u Our descendants can be traced back to dfn Sequence number of the smallest node , That is to return to the earliest past .
Two give an example
At the beginning ,dfn[u]=low[u], If the adjacent node of this node is not accessed , Depth first traversal is always performed ,1-2-3-5-6-4, here 4 The adjacency point 1 Has been interviewed , And 1 No 4 Parent node ,4 The parent of is 6( Depth first search the parent node on the tree ).
node 4 The earliest node that can be returned is node 1(dfn=1), therefore low[4]=min(low[4],dfn[1])=1, return , to update low[6]=min(low[6],low[4])=1. Update all ancestor nodes on the path low value , Because descendants can go back to the trace point , Their ancestors can also return .
3、 ... and The bridge of an undirected graph
Bridge decision rule : No to the edge x-y It's a bridge , If and only if there is a node in the search tree y, Satisfy low[y] > dfn[x].
in other words , If the child's low Worth more than yourself , Then the bridge is from the node to the child's side . In the diagram above , edge 5-7 in ,5 The child node of is 7, Satisfy low[7]>dfn[5], therefore 5-7 For the bridge .
Four Code
package graph.tarjanbridge;
import java.util.Arrays;
import java.util.Scanner;
public class TarjanBridge {
static int SIZE = 100010;
static int head[] = new int[SIZE], to[] = new int[SIZE << 1], next[] = new int[SIZE << 1];
static int cnt = 0;
static void addEdge(int x, int y) {
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
to[cnt] = x;
next[cnt] = head[y];
head[y] = cnt++;
}
static int dfn[] = new int[SIZE], low[] = new int[SIZE];
static int index;
static boolean bridge[] = new boolean[SIZE << 1];
static void tarjan(int x, int edge) {
dfn[x] = low[x] = ++index;
for (int i = head[x]; i >= 0; i = next[i]) {
int y = to[i];
if (dfn[y] == 0) {
tarjan(y, i);
low[x] = Math.min(low[x], low[y]);
if (dfn[x] < low[y])
bridge[i] = bridge[i ^ 1] = true;
} else if (i != (edge ^ 1))
low[x] = Math.min(low[x], dfn[y]);
}
}
static void init() {
Arrays.fill(head, -1);
cnt = 0;
}
static int n, m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
init();
for (int i = 0; i < m; i++)
addEdge(sc.nextInt(), sc.nextInt());
for (int i = 1; i <= n; i++)
if (dfn[i] == 0)
tarjan(i, -1);
for (int i = 1; i < cnt; i += 2)
if (bridge[i])
System.out.println(to[i ^ 1] + " " + to[i]);
}
}
5、 ... and test
边栏推荐
- JS逆向之行行查data解密
- 解答:EasyDSS视频点播时音频是否可以设置为默认开启?
- Numpy array calculation
- Apply lnk306gn-tl converter, non isolated power supply
- Explanation of 34 common terms on the Internet
- moon
- Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
- Hundreds of web page special effects can be used. Don't you come and have a look?
- 互联网常见34个术语解释
- 日本赌国运:Web3.0 ,反正也不是第一次失败了!
猜你喜欢
Ntmfs4c05nt1g N-ch 30V 11.9a MOS tube, pdf
三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
Unity skframework framework (XIX), POI points of interest / information points
Essential for operation and maintenance - Elk log analysis system
EasyDSS点播服务分享时间出错如何修改?
国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
Redis database persistence
3 a VTT terminal regulator ncp51200mntxg data
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
【云原生数据库】遇到慢SQL该怎么办(上)?
随机推荐
Unity SKFramework框架(十四)、Extension 扩展函数
Unity SKFramework框架(十五)、Singleton 单例
Unity SKFramework框架(十三)、Question 问题模块
Unity SKFramework框架(十二)、Score 计分模块
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
[opencv learning] [image filtering]
[opencv] [image gradient]
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
完全自主可控三维云CAD:CrownCAD便捷的命令搜索,快速定位所需命令具体位置。
Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
能自动更新的万能周报模板,有手就会用!
MySQL: Invalid GIS data provided to function st_ geometryfromtext
Unity skframework Framework (XVI), package manager Development Kit Manager
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
leetcode621. 任务调度器
Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
嵌入式软件开发
[opencv learning] [moving object detection]