poj1985&& the fourth time CCF software certification 4th question to find the diameter of the tree

Source: Internet
Author: User

Cow Marathon
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 4216 Accepted: 2137
Case Time Limit: 1000MS

Description

After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he had Committ Ed to create a bovine marathon for the cows to run. The marathon route would include a pair of farms and a path comprised of a sequence of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the both farms on his map that is the farthes T apart from all other (distance being measured in terms of total length of road on the path between the same farms). Help him determine the distances between this farthest pair of farms.

Input

* Lines 1 ...: same input format as "Navigation nightmare".

Output

* Line 1:an integer giving the distance between the farthest pair of farms.

Sample Input

7 6 E6 3 9 E3 5 7 S4 1 3 N2 4 W4 7 2 S

Sample Output

52

Hint

The longest marathon runs from farm 2 via Roads 4, 1, 6 and 3 to farm 5 and is of length 20+3+13+9+7=52.

Source

Usaco 2004 February the approximate meaning of the body is to find the maximum distance between any two points in a tree, this tree is a non-oriented graph tree, the method is not unique, can be resolved with DP, you can also use DFS to solve, storage edge must remember should be no map, both sides should be stored in Utilizes a property of the diameter of a tree: the leaf node farthest from a point must be the end of a certain diameter of the tree. So that you can take a point first, bfs out of its farthest point, in the same way to find out the node farthest from the leaf point, the distance between two points is the diameter of the tree.
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<queue>using namespacestd;Const intMAXN =10005; Vector<int>SON[MAXN], W[MAXN];BOOLVIS[MAXN], VISS[MAXN];intF[MAXN];intBFsintroot) {    intI, j, K; intAns = root, Maxx =0; Queue<int>Q; memset (Vis,0,sizeof(VIS)); Memset (F,0,sizeof(f));    Q.push (root); Vis[root]=1; F[root] =0; Viss[root] =1;  while(!Q.empty ()) {Root=Q.front ();        Q.pop ();  for(i=0; I<son[root].size (); i++){            if(vis[son[root][i]]==0) {Q.push (son[root][i]); Vis[son[root][i]]=1; Viss[son[root][i]] =1; F[son[root][i]]= f[root]+W[root][i]; if(maxx<F[son[root][i]]) {Maxx=F[son[root][i]]; Ans=Son[root][i]; }            }        }    }    returnans;}intSolveintroot) {    intu, v; U=BFS (root); V=BFS (U); returnf[v];}intMain () {intI, J, K, N, M; intx1, x2, L, U; intRes;  while(~SCANF ("%d%d",&n,&m)) {         for(i=0; i<=n;i++) {son[i].clear ();        W[i].clear (); }         for(i=0; i<m;i++) {scanf ("%d%d%d",&x1,&x2,&l); scanf ("%c",&opt);            Son[x1].push_back (x2); W[x1].push_back (L);        Son[x2].push_back (x1); W[x2].push_back (L); } Res=0; memset (Viss,0,sizeof(VIS));  for(i=1; i<=n;i++){            if(viss[i]==0) {res=Max (Res,solve (i)); }} printf ("%d\n", RES); }    return 0;}
The following is the CCF certification of the fourth question, the problem is still determined to be any two points between the maximum distance, but the question does not give the length of the edge, so in the template when you can save the side as 1 in addition to the topic has n+m, Edge, in the loop when must pay attention to the boundary of the problem is m+n name:????? Return questions List Question description
Question number: 201503-4
Question Name: Network delay
Time limit: 1.0s
Memory Limit: 256.0MB
Problem Description: The problem describes the network of a given company, consisting 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 input contains two integers n, m, respectively, indicating the number of stations of the switch and the number of terminal computers.
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. The 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 4 example shows the network connection mode of the sample, 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 example shows the network connection mode of the sample as follows:

The message delivery between PC 1 and PC 4 takes the longest time, which is 4 units of time. The evaluation use case scale and the 30% evaluation use case satisfies: 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.
Answer Bar
Question number: 201503-4
Question Name: Network delay

poj1985&& the fourth time CCF software certification 4th question to find the diameter of the tree

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.