11521: 【原1521】网络延时
题目
题目描述
author: 金耀楠 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1521
Description
给定一个公司的网络,由n台交换机和m台终端电脑组成,交换机与交换机、交换机与电脑之间使用网络连接。交换机按层级设置,编号为1的交换机为根交换机,层级为1。其他的交换机都连接到一台比自己上一层的交换机上,其层级为对应交换机的层级加1。所有的终端电脑都直接连接到交换机上。 当信息在电脑、交换机之间传递时,每一步只能通过自己传递到自己所连接的另一台电脑或交换机。请问,电脑与电脑之间传递消息、或者电脑与交换机之间传递消息、或者交换机与交换机之间传递消息最多需要多少步。
Input Format
输入的第一行包含两个整数n, m,分别表示交换机的台数和终端电脑的台数。 第二行包含n - 1个整数,分别表示第2、3、……、n台交换机所连接的比自己上一层的交换机的编号。第i台交换机所连接的上一层的交换机编号一定比自己的编号小。 第三行包含m个整数,分别表示第1、2、……、m台终端电脑所连接的交换机的编号。
Output Format
输出一个整数,表示消息传递最多需要的步数。
Sample Input
4 2
1 1 3
2 1
Sample Output
4
Limits
- 前20%的评测用例满足:n ≤ 100, m ≤ 100。
- 前50%的评测用例满足:n ≤ 1000, m ≤ 1000。
- 所有评测用例都满足:1 ≤ n ≤ 10000,1 ≤ m ≤ 10000
ligongzzz's solution
#include "iostream"
#include "cstdio"
#include "cstring"
using namespace std;
class node {
public:
int first_max = 0, second_max = 0;
int depth = 0;
int parent = 0;
};
node tree_data[20009]{};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
tree_data[1].depth = 1;
for (int i = 2; i <= n + m; ++i) {
cin >> tree_data[i].parent;
tree_data[i].depth = tree_data[tree_data[i].parent].depth + 1;
}
for (int i = n + m; i > 0; --i) {
if (tree_data[i].depth > tree_data[i].first_max) {
tree_data[i].second_max = tree_data[i].first_max;
tree_data[i].first_max = tree_data[i].depth;
}
if (tree_data[i].depth > tree_data[i].second_max) {
tree_data[i].second_max = tree_data[i].depth;
}
if (tree_data[i].first_max > tree_data[tree_data[i].parent].first_max) {
tree_data[tree_data[i].parent].second_max = tree_data[tree_data[i].parent].first_max;
tree_data[tree_data[i].parent].first_max = tree_data[i].first_max;
}
else if (tree_data[i].first_max > tree_data[tree_data[i].parent].second_max) {
tree_data[tree_data[i].parent].second_max = tree_data[i].first_max;
}
}
int ans = 0;
for (int i = 1; i <= n + m; ++i) {
int cur_ans = tree_data[i].first_max + tree_data[i].second_max - 2 * tree_data[i].depth;
ans = cur_ans > ans ? cur_ans : ans;
}
cout << ans;
return 0;
}