当前位置:网站首页>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
![]()
边栏推荐
- 完全自主可控三维云CAD:CrownCAD便捷的命令搜索,快速定位所需命令具体位置。
- Unity skframework framework (XX), VFX lab special effects library
- 科技的成就(二十七)
- Performance optimization of memory function
- 阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)
- 国产免费数据仓库ETL调度自动化运维专家—TASKCTL
- 2022零代码/低代码开发白皮书【伙伴云出品】附下载
- Unity skframework framework (XIII), question module
- 无向图的桥
- Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
猜你喜欢

2022零代码/低代码开发白皮书【伙伴云出品】附下载
![[OpenGL] notes 29. Advanced lighting (specular highlights)](/img/6e/56bc7237f691a4355f0b7627b3003e.png)
[OpenGL] notes 29. Advanced lighting (specular highlights)

【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解

Finally, someone explained the supervised learning clearly

PR usage skills, how to use PR to watermark?

阿里发布的Redis开发文档,涵盖了所有的redis操作
![Jerry's watch ringtone audition [article]](/img/18/905c4b64443f4efca55188e36f4b28.jpg)
Jerry's watch ringtone audition [article]

Chinese name extraction (toy code - accurate head is too small, right to play)

leetcode621. 任务调度器

Fundamentals of face recognition (facenet)
随机推荐
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
Everyone wants to eat a broken buffet. It's almost cold
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
Should I have a separate interface assembly- Should I have a separate assembly for interfaces?
JS generates 4-digit verification code
国产免费数据仓库ETL调度自动化运维专家—TASKCTL
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
SSL证书的分类有哪些?如何选择合适的SSL证书?
Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six
三翼鸟两周年:羽翼渐丰,腾飞指日可待
[opencv learning] [template matching]
net share
嵌入式软件开发
Rust language document Lite (Part 1) - cargo, output, basic syntax, data type, ownership, structure, enumeration and pattern matching
Fundamentals of machine learning (II) -- division of training set and test set
Jerry's watch delete alarm clock [chapter]
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
Research shows that "congenial" is more likely to become friends
Japan bet on national luck: Web3.0, anyway, is not the first time to fail!