当前位置:网站首页>电话网络问题
电话网络问题
2022-07-03 00:35:00 【chengqiuming】
一 原问题链接
Network - POJ 1144 - Virtual Judge
https://vjudge.net/problem/POJ-1144
二 输入与输出
1 输入
输入包含多个测试用例。每个测试用例都描述一个网络。每个测试用例的第 1 行都是 N。接下来最多 N 行中每一行都包含一个地点的编号,后面是该地方可以直达的地点编号。每个测试用例都以一条仅包含0的行结束。N=0时输入结束,不处理。
2 输出
对每个测试用例,都单行输出关键位置的数量。
三 输入样例
5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0
四 输出样例
1
2
五 问题分析
1 输入样例1,构建的图如下。在该图中有1个关键点5。

2 输入样例2,构建的图如下。在该图中有两个关键点,分别是2和5。

本问题就是求割点数,利用 Tarjan 算法求解即可。
六 代码
package graph.poj1144;
import java.util.Scanner;
public class POJ1144 {
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 cut[];
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) { //求割点
dfn[u] = low[u] = ++num;
int flag = 0;
for (int i = head[u]; i != 0; i = e[i].next) {
int v = e[i].to;
if (dfn[v] == 0) {
tarjan(v);
low[u] = Math.min(low[u], low[v]);
if (low[v] >= dfn[u]) {
flag++;
if (u != root || flag > 1) {
cut[u] = 1;
}
}
} else
low[u] = Math.min(low[u], dfn[v]);
}
}
static void init() {
head = new int[maxn];
low = new int[maxn];
dfn = new int[maxn];
cut = new int[maxn];
cnt = num = 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
n = scanner.nextInt();
if (n == 0)
break;
init();
int u, v;
while (true) {
Scanner scanner1 = new Scanner(System.in);
String nums = scanner1.nextLine();
String[] s = nums.split(" ");
u = Integer.parseInt(s[0]);
if (u == 0) {
break;
}
for (int i = 1; i < s.length; i++) {
v = Integer.parseInt(s[i]);
add(u, v);
add(v, u);
}
}
for (int i = 1; i <= n; i++) {
if (dfn[i] == 0) {
root = i;
tarjan(i);
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
if (cut[i] == 1)
ans++;
System.out.println(ans);
}
}
}
class Edge {
int to;
int next;
}七 测试结果
绿色为输入,白色为输出

边栏推荐
- 1.11 - bus
- [shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
- Vulkan practice first bullet
- Rust ownership (very important)
- First hand evaluation of Reza electronics rz/g2l development board
- Key detection and sinusoidal signal output developed by Arduino
- [overview of AUTOSAR four BSW]
- Deep analysis of data storage in memory
- Array and collection performance comparison
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
猜你喜欢

2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)

Rk3568 development board evaluation (II): development environment construction

RK3568开发板评测篇(二):开发环境搭建

Test shift right: Elk practice of online quality monitoring

(C语言)数据的存储

指针进阶(一)

飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
![[AUTOSAR II appl overview]](/img/da/76ccc05e2199705b20d8304bfb86b2.png)
[AUTOSAR II appl overview]

利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度

深度剖析数据在内存中的存储
随机推荐
【AutoSAR 三 RTE概述】
文件操作IO-Part2
线程的启动与优先级
Rust ownership (very important)
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
百度智能云牵头打造智能云综合标准化平台
[love crash] neglected details of gibaro
数学建模之线性规划(含MATLAB代码)
leetcode-2115:从给定原材料中找到所有可以做出的菜
[daily training] 871 Minimum refueling times
Nacos+openfeign error reporting solution
Leetcode-871: minimum refueling times
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
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
Problèmes de configuration lex & yacc & Bison & Flex
cordova-plugin-device获取设备信息插件导致华为审核不通过
【AutoSAR 十二 模式管理】
测试右移:线上质量监控 ELK 实战
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit