网络迟延

本文介绍了一个计算网络中信息传递最大延时的算法。通过构建网络模型,包括交换机和终端电脑,利用递归方式更新路由表来计算电脑间、电脑与交换机间以及交换机间的最大信息传递步数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

试题名称:网络延时
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  给定一个公司的网络,由n台交换机和m台终端电脑组成,交换机与交换机、交换机与电脑之间使用网络连接。交换机按层级设置,编号为1的交换机为根交换机,层级为1。其他的交换机都连接到一台比自己上一层的交换机上,其层级为对应交换机的层级加1。所有的终端电脑都直接连接到交换机上。
  当信息在电脑、交换机之间传递时,每一步只能通过自己传递到自己所连接的另一台电脑或交换机。请问,电脑与电脑之间传递消息、或者电脑与交换机之间传递消息、或者交换机与交换机之间传递消息最多需要多少步。
输入格式
  输入的第一行包含两个整数n, m,分别表示交换机的台数和终端电脑的台数。
  第二行包含n - 1个整数,分别表示第2、3、……、n台交换机所连接的比自己上一层的交换机的编号。第i台交换机所连接的上一层的交换机编号一定比自己的编号小。
  第三行包含m个整数,分别表示第1、2、……、m台终端电脑所连接的交换机的编号。
输出格式
  输出一个整数,表示消息传递最多需要的步数。
样例输入
4 2
1 1 3
2 1
样例输出
4
样例说明
  样例的网络连接模式如下,其中圆圈表示交换机,方框表示电脑:

  其中电脑1与交换机4之间的消息传递花费的时间最长,为4个单位时间。
样例输入
4 4
1 2 2
3 4 4 4
样例输出
4
样例说明
  样例的网络连接模式如下:

  其中电脑1与电脑4之间的消息传递花费的时间最长,为4个单位时间。
评测用例规模与约定
  前30%的评测用例满足:n ≤ 5, m ≤ 5。
  前50%的评测用例满足:n ≤ 20, m ≤ 20。
  前70%的评测用例满足:n ≤ 100, m ≤ 100。

  所有评测用例都满足:1 ≤ n ≤ 10000,1 ≤ m ≤ 10000。

#include <iostream>
#include <vector>
#include <math.h>
#include <map>

using namespace std;

int max=0;     //最大延时
class node
{
private:
    int no;     //节点标号
    int type;     //类型,1为路由,2为电脑
    vector<node*> v;     //下级节点表
    map<int,pair<int,node*>> map1;     //key是电脑编号,pair-key是总跳数,pair-value是所属节点地址
public:
    node(int no, int type ) : no(no), type(type){ }


    int getNo() {
        return no;
    }
    void add(node* n)
    {
        v.push_back(n);
    }

    int getType() const {
        return type;
    }

    map<int,pair<int,node*>>* echo()     //发送消息,计算最大延迟,将路由表返回上一级路由
    {
        for(int i=0;i<v.size();i++)     //对所有下级节点获取信息并更新至本路由表
        {
            map<int,pair<int,node*>>* temp = v[i]->echo();
            for(map<int,pair<int,node*>>::iterator it = temp->begin();it!=temp->end();it++)
            {
                    int noo = it->first;
                    pair<int,node*> p2 = pair<int,node*>(it->second.first,it->second.second);
                    map1.insert(pair<int,pair<int,node*>>(noo,p2));
            }
        }
        for(int i=0;i<v.size();i++)     //对本层电脑进行搜索,更新路由表
            if(v[i]->getType()==2)
            {
                int noo = v[i]->getNo();
                pair<int,node*> p2 = pair<int,node*>(1,v[i]);
                map1.insert(pair<int,pair<int,node*>>(noo,p2));
            }
        int max1(0),max2(0);     //设置最值进行计算
        node* no1 = NULL;     //纪录第一个最大值
        for(map<int,pair<int,node*>>::iterator it = map1.begin();it!=map1.end();it++)
        {
            if(it->second.first>max1)     //搜索一个最大值,并纪录值和属于哪个路由
            {
                max1 = it->second.first;
                no1 = it->second.second;
            }
        }
        for(map<int,pair<int,node*>>::iterator it = map1.begin();it!=map1.end();it++)
        {
            //cout<<"node "<<no<<" com "<<it->first<<" count "<<it->second.first<<" from "<<it->second.second->getNo()<<endl;
            if(it->second.first<=max1 && it->second.first>max2 && it->second.second != no1)     //纪录第二个最大值,必须和第一个来自不同路由避免重复计算
            {
                max2 = it->second.first;
            }
            it->second.first++;     //更新跳数以及来自哪个节点
            it->second.second=this;
        }
        if(max1+max2>::max){     //更新最大延时
            ::max=max1+max2;//cout<<::max<<endl;
        }
        return &map1;     //返回路由表
    };
};
int main() {
    map<int,node*> j;
    map<int,node*> computer;
    int n,m;
    cin>>n>>m;
    node* root = new node(1,1);
    j[1] = root;
    for(int i=2;i<=n;i++)     //读取节点
    {
        int a;
        cin>>a;
        node* pp = new node(i,1);
        j[a]->add(pp);
        j[i] = pp;
    }
    for(int i=1;i<=m;i++)     //读取电脑
    {
        int a;
        cin>>a;
        node* pp = new node(i,2);
        j[a]->add(pp);
        computer[i]=pp;
    }
    root->echo();     //根节点发送消息
    cout<<::max;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值