当前位置:网站首页>无向图的割点
无向图的割点
2022-07-03 00:35:00 【chengqiuming】
一 割点判定法则
如果x不是根节点,则 x 是割点,当且仅当在搜索树上存在 x 的一个子节点 y,满足 low[y]>=dfn[x],若 x 是割点,当且仅当在搜索树上至少存在两个子节点,满足 low[y]>=dfn[x]。
也就是说,如果不是根,且孩子的 low 值大于或等于自己的 dfn 值,则该节点就是割点;如果是根,则至少需要两个孩子满足条件。在下图中,5的子节点是7,满足 low[7] > low[5],因此 5 是割点。
二 割点判定情况
1 不是割点
1不是割点:1是根,只有一个孩子满足 low{2}>dfn[1]
2 割点是根的情况
1是割点:1是根,有两个孩子满足 low{2}>dfn[1],low[3]>dfn[1]
3 割点不是根的情况
2和3是割点:low[3]>dfn[2],low[4]=dfn[3]
三 代码
package graph.targancut;
import java.util.Scanner;
public class TarjanCut {
static final int maxn = 1000 + 5;
static int n;
static int m;
static int head[];
static int cnt;
static int root;
static int low[];
static int dfn[];
static int num;
static Edge e[] = new Edge[maxn << 1];
static {
for (int i = 0; i < e.length; i++) {
e[i] = new Edge();
}
}
static void add(int u, int v) { // 添加一条边u--v
e[++cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
}
static void tarjan(int u, int fa) { //求割点
dfn[u] = low[u] = ++num;
int count = 0;
for (int i = head[u]; i != 0; i = e[i].next) {
int v = e[i].to;
if (v == fa)
continue;
if (dfn[v] == 0) {
tarjan(v, u);
low[u] = Math.min(low[u], low[v]);
if (low[v] >= dfn[u]) {
count++;
if (u != root || count > 1) {
System.out.println(u + "是割点");
}
}
} else
low[u] = Math.min(low[u], dfn[v]);
}
}
static void init() {
head = new int[maxn];
low = new int[maxn];
dfn = new int[maxn];
cnt = num = 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
m = scanner.nextInt();
init();
int u, v;
while (m-- > 0) {
u = scanner.nextInt();
v = scanner.nextInt();
add(u, v);
add(v, u);
}
for (int i = 1; i <= n; i++)
if (dfn[i] == 0) {
root = i;
tarjan(1, 0);
}
}
}
class Edge {
int to;
int next;
}
四 测试
绿色为输入,白色为输出。
边栏推荐
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- File operation io-part2
- leetcode-871:最低加油次数
- Leetcode-1964: find the longest effective obstacle race route to each position
- 【C语言】分支和循环语句(上)
- FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
- Leetcode-2115: find all the dishes that can be made from the given raw materials
- Overlay of shutter (Pop-Up)
- 数学建模之线性规划(含MATLAB代码)
- 【AutoSAR 十三 NVM】
猜你喜欢
The difference between tail -f, tail -f and tail
[AUTOSAR twelve mode management]
Explain the basic concepts and five attributes of RDD in detail
1.11 - 总线
深度剖析数据在内存中的存储
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
Illustrated network: what is virtual router redundancy protocol VRRP?
【AutoSAR 九 C/S原理架构】
[AUTOSAR five methodology]
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
随机推荐
Vulkan is not a "panacea"“
[overview of AUTOSAR four BSW]
Set up nacos2 X cluster steps and problems encountered
465. 最优账单平衡 DFS 回溯
MySQL multi table joint deletion
Infrared thermography temperature detection system based on arm rk3568
Vulkan practice first bullet
[daily training] 871 Minimum refueling times
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
Leetcode-934: the shortest Bridge
Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
Win10 can't be installed in many ways Problems with NET3.5
tail -f 、tail -F、tailf的区别
Sentry developer contribution Guide - configure pycharm
[AUTOSAR 11 communication related mechanism]
Leetcode-1964: find the longest effective obstacle race route to each position
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
[AUTOSAR + IO Architecture]
【C语言】分支和循环语句(上)