当前位置:网站首页>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
![]()
边栏推荐
- Unity skframework framework (XVI), package manager development kit Manager
- Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
- Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
- Sensor adxl335bcpz-rl7 3-axis accelerometer complies with rohs/weee
- [indomitable medal activity] life goes on and writing goes on
- 研究表明“气味相投”更易成为朋友
- Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
- Unity skframework framework (XIX), POI points of interest / information points
- 2022零代码/低代码开发白皮书【伙伴云出品】附下载
- Apply lnk306gn-tl converter, non isolated power supply
猜你喜欢

Answer: can the audio be set to on by default during easydss video on demand?

阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)

Unity skframework framework (XXI), texture filter map resource filtering tool
![[OpenGL] notes 29. Advanced lighting (specular highlights)](/img/6e/56bc7237f691a4355f0b7627b3003e.png)
[OpenGL] notes 29. Advanced lighting (specular highlights)
![[opencv learning] [template matching]](/img/4c/7214329a34974c59b4931c08046ee8.jpg)
[opencv learning] [template matching]

二、帧模式 MPLS 操作
![[opencv learning] [image filtering]](/img/4c/fe22e9cdf531873a04a7c4e266228d.jpg)
[opencv learning] [image filtering]
![[opencv learning] [image histogram and equalization]](/img/e7/b8dc55a9febf2b2949fce3a7ac21f9.jpg)
[opencv learning] [image histogram and equalization]

Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk

Unity skframework framework (XIX), POI points of interest / information points
随机推荐
日本赌国运:Web3.0 ,反正也不是第一次失败了!
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
[opencv learning] [template matching]
net share
Nohup command
【云原生数据库】遇到慢SQL该怎么办(上)?
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
完全自主可控三维云CAD:CrownCAD便捷的命令搜索,快速定位所需命令具体位置。
MySQL: Invalid GIS data provided to function st_ geometryfromtext
mac(macos Monterey12.2 m1) 个人使用php开发
Unity skframework framework (XIX), POI points of interest / information points
无向图的桥
阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)
Unity skframework framework (XX), VFX lab special effects library
阿里发布的Redis开发文档,涵盖了所有的redis操作
Obtain file copyright information
Jerry's weather code table [chapter]
Unity skframework framework (XVI), package manager development kit Manager
De4000h storage installation configuration
自主可控三维云CAD:CrownCAD赋能企业创新设计