试题编号: | 201503-4 |
试题名称: | 网络延时 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 给定一个公司的网络,由n台交换机和m台终端电脑组成,交换机与交换机、交换机与电脑之间使用网络连接。交换机按层级设置,编号为1的交换机为根交换机,层级为1。其他的交换机都连接到一台比自己上一层的交换机上,其层级为对应交换机的层级加1。所有的终端电脑都直接连接到交换机上。 输入格式 输入的第一行包含两个整数n, m,分别表示交换机的台数和终端电脑的台数。 输出格式 输出一个整数,表示消息传递最多需要的步数。 样例输入 4 2 样例输出 4 样例说明 样例的网络连接模式如下,其中圆圈表示交换机,方框表示电脑: |
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <time.h>
#include <vector>
#include <list>
using namespace std;
//201503-4 网络延时
//定义一个向量数组
vector<int>Data[20001];
//DFS 搜索标志 最大深度 当前深度 最深点 当前点
//数据不能构成环路
void DFS(bool Flag[], int &max, int lenth, int &to, int now)
{
Flag[now] = 1;
if (max < lenth)
{
max = lenth;
to = now;
}
for (int i = 0; i < Data[now].size(); i++)
{
if (!Flag[Data[now][i]])
{
DFS(Flag, max, lenth + 1, to, Data[now][i]);
}
}
}
int main()
{
int i, j, N, M;
int mum;
cin >> N >> M;
for (i = 2; i <= N + M; i++)
{
cin >> mum;
Data[i].push_back(mum);
Data[mum].push_back(i);
}
bool Flag_1[20001] = { 0 }, Flag_2[20001] = { 0 };
int max=0, to=0;
DFS(Flag_1, max, 0, to, 1);
DFS(Flag_2, max, 0, to, to);
cout << max;
cin >> N;
return 0;
}