March 15 CCF Real problem 4-network delay

Source: Internet
Author: User
Tags switches

Problem description

Given a company's network, it consists of n-switches and M-terminal computers, which use a network connection between the switch and the switch, the switch, and the computer. Switches are set hierarchically and the switch numbered 1 is the root switch with a level of 1. The other switches are connected to a switch that is higher than the previous layer, and the level of the corresponding switch is 1. All terminal computers are connected directly to the switch.
When information is passed between a computer and a switch, each step can only be passed to another computer or switch to which it is connected. Ask, how many steps are required to pass messages between computers and computers, or to pass messages between computers and switches, or to pass messages between switches and switches.

Input format

The first line of the input contains two integers n, m, respectively, indicating the number of switches and the number of computers on the terminal.
The second line contains n-1 integers that represent the number of switches connected to the 2nd, 3 、......、 n switches that are higher than their previous level. The switch number on the previous layer connected to the I switch must be smaller than its own number.
The third line contains m integers representing the number of switches connected to the 1th, 2 、......、 m terminal computers.

Output format

Outputs an integer that represents the maximum number of steps required for message delivery.

Sample input

4 2
1 1 3
2 1

Sample output

3

Sample Description

The network connection mode for the sample is as follows, where the circle represents the switch and the box represents the computer:



The message delivery between PC 1 and switch 4 takes the longest time, which is 4 units of time.

Sample input

4 4
1 2 2
3 4 4 4

Sample output

4

Sample Description

The network connection mode for the sample is as follows:

The message delivery between PC 1 and PC 4 takes the longest time, which is 4 units of time.

Measuring use case size and conventions

The first 30% evaluation cases meet: n≤5, m≤5.
The first 50% evaluation cases meet: N≤20, m≤20.
The first 70% evaluation cases meet: n≤100, m≤100.
All evaluation cases meet: 1≤n≤10000,1≤m≤10000.

1#include <iostream>2#include <vector>3#include <math.h>4#include <map>5 6 using namespacestd;7 8 intmax=0;//Maximum Delay9 classnodeTen { One Private: A     intNo//node Label -     intType//type, 1 for routing, 2 for PC -Vector<node*> v;//Subordinate node Table themap<int,pair<int,node*>> Map1;//key is the computer number, Pair-key is the total hop count, Pair-value is the address of the owning node -  Public: -NodeintNointtype): No (No), type (type) {} -  +  -     intGetno () { +         returnNo; A     } at     voidAdd (node*N) -     { - v.push_back (n); -     } -  -     intGetType ()Const { in         returntype; -     } to  +map<int,pair<int, node*>>* Echo ()//sends a message, calculates the maximum delay, returns the routing table to the previous level of routing -     { the          for(intI=0; I<v.size (); i++)//get information for all subordinate nodes and update to this route table *         { $map<int,pair<int, node*>>* temp = v[i]->Echo ();Panax Notoginseng              for(map<int,pair<int, node*>>::iterator it = Temp->begin (); It!=temp->end (); it++) -             { the                     intNoo = it->First ; +pair<int,node*> P2 = pair<int,node*> (it->second.first,it->second.second); AMap1.insert (pair<int,pair<int,node*>>(NOO,P2)); the             } +         } -          for(intI=0; I<v.size (); i++)//search for this tier of computers, update the routing table $             if(V[i]->gettype () = =2) $             { -                 intNoo = v[i]->Getno (); -pair<int,node*> P2 = pair<int,node*> (1, V[i]); theMap1.insert (pair<int,pair<int,node*>>(NOO,P2)); -             }Wuyi         intMAX1 (0), Max2 (0);//set maximum value for calculation thenode* no1 = NULL;//record the first maximum value -          for(map<int,pair<int, node*>>::iterator it = Map1.begin (); It!=map1.end (); it++) Wu         { -             if(IT-&GT;SECOND.FIRST&GT;MAX1)//search for a maximum value and record the value and which route belongs to About             { $MAX1 = it->Second.first; -No1 = it->Second.second; -             } -         } A          for(map<int,pair<int, node*>>::iterator it = Map1.begin (); It!=map1.end (); it++) +         { the             //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)//record the second maximum value, must and the first one from different routes to avoid repeating the calculation $             { theMAX2 = it->Second.first; the             } theit->second.first++; //Update hop count and from which node  theIt->second.second= This; -         } in         if(Max1+max2>::max) {//Update Maximum delay the:: MAX=MAX1+MAX2;//cout<<::max<<endl; the         } About         return&Map1; //Return to route table  the     }; the }; the intMain () { +map<int,node*>J; -map<int,node*>computer; the     intn,m;BayiCin>>n>>m; thenode* root =NewNode1,1); thej[1] =Root; -      for(intI=2; i<=n;i++)//Read Node -     { the         intA; theCin>>A; thenode* pp =NewNode (i,1); theJ[a]->Add (PP); -J[i] =pp; the     } the      for(intI=1; i<=m;i++)//Reading Computer the     {94         intA; theCin>>A; thenode* pp =NewNode (i,2); theJ[a]->Add (PP);98computer[i]=pp; About     } -Root->echo ();//root node Send message101cout<<:: Max;102 103     return 0;104}

March 15 CCF Real problem 4-network delay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.