当前位置:网站首页>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
![]()
边栏推荐
- Clean up system cache and free memory under Linux
- 【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
- Fundamentals of face recognition (facenet)
- [200 opencv routines] 100 Adaptive local noise reduction filter
- [opencv learning] [image pyramid]
- 国产免费数据仓库ETL调度自动化运维专家—TASKCTL
- 8A Synchronous Step-Down regulator tps568230rjer_ Specification information
- JS逆向之行行查data解密
- [opencv learning] [moving object detection]
- 国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
猜你喜欢

West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials

Unity skframework Framework (XVI), package manager Development Kit Manager

Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本

Ali was killed by two programming problems at the beginning, pushed inward again, and finally landed (he has taken an electronic offer)

TVOC, VOC, VOCs gas detection + Solution

Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
![[opencv learning] [Canny edge detection]](/img/8b/37694ae2f0f13f829f3c033da0605e.jpg)
[opencv learning] [Canny edge detection]
![[error record] cannot open](/img/d3/0435ae698ad635be71729c7c047a22.jpg)
[error record] cannot open "XXX" because Apple cannot check whether it contains malware

Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?

Ali on three sides, it's really difficult to successfully get the offer rated P7
随机推荐
Traverse entrylist method correctly
国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
Oracle from entry to mastery (4th Edition)
国产免费数据仓库ETL调度自动化运维专家—TASKCTL
Principle analysis of security rememberme
Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
Three methods of finding LCA of the nearest common ancestor
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
Download files and preview pictures
伙伴云表格强势升级!Pro版,更非凡!
Fundamentals of face recognition (facenet)
最近公共祖先LCA的三种求法
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
Unity skframework framework (XX), VFX lab special effects library