当前位置:网站首页>电话网络问题
电话网络问题
2022-07-03 00:35:00 【chengqiuming】
一 原问题链接
Network - POJ 1144 - Virtual Judgehttps://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;
}
七 测试结果
绿色为输入,白色为输出
边栏推荐
- 正确甄别API、REST API、RESTful API和Web Service之间的异同
- [daily training] 871 Minimum refueling times
- [AUTOSAR nine c/s principle Architecture]
- leetcode-871:最低加油次数
- 【AutoSAR 十二 模式管理】
- 1.12 - 指令
- 瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
- 【爱死机】《吉巴罗》被忽略的细节
- [C language] branch and loop statements (Part 1)
- matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
猜你喜欢
Vulkan-性能及精细化
【AutoSAR 五 方法论】
Arduino开发之按键检测与正弦信号输出
[introduction to AUTOSAR seven tool chain]
指针进阶(一)
Vulkan performance and refinement
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
Hdu3507 (slope DP entry)
指针初阶(基础)
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
随机推荐
指针进阶(一)
mysql 多表联合删除
(C语言)数据的存储
leetcode-241:为运算表达式设计优先级
解决ReactNative使用webView存在缓存问题
机器学习:numpy版本线性回归预测波士顿房价
Leetcode 294. Flip game II (game theory)
[overview of AUTOSAR three RTE]
RK3568开发板评测篇(二):开发环境搭建
Illustrated network: what is virtual router redundancy protocol VRRP?
Vulkan-实践第一弹
【AutoSAR 十一 通信相关机制】
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
Array and collection performance comparison
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
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
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
Foundations of data science is free to download
正确甄别API、REST API、RESTful API和Web Service之间的异同