How to find the diameter of the tree

Learning blog: https://www.cnblogs.com/ywjblog/p/9254997.html

The diameter of the tree
given distance defined between a tree, the tree has a weight of each edge, two to the right of the tree path connecting the two points and edges. The diameter of the tree is called the distance between the farthest two nodes in the tree, the path connecting the two points is called the longest chain of the tree. The latter may also be referred to generally in diameter, i.e. the diameter is a 
numerical concept, also on behalf of that path
tree diameter Seeking usually two, are time complexity O (n). We assume the form of a directed graph tree given by N N-1 point edge strips and stored in the neighbor table.

The first is a method for finding dp, where I only introduce bfs Seeking:

Two BFS (DFS) diameter tree required
by two or BFS DFS may be twice the diameter of the tree requirements, and more particularly easy to calculate the diameter of the nodes on the
detail, this procedure involves two steps:
1. from any node starting, by a p-BFS and DFS traversal of a tree, and obtains the starting point node farthest referred
2. from node p, then one pass through the through BFS or DFS, p-determined distance from the farthest node, denoted q.
Path from p to q is a diameter of the tree. Since p must be the end of the diameter, otherwise can always find a longer chain, contradicts the definition of the diameter. Apparently brain-dong click. p is the diameter of the end, so natural, q and p is the farthest end of the other diameter.
In the second step of traversal may be recorded for each predecessor node of the first point to be accessed. From the last recursion q p, to obtain specific programs diameter

Let's look at an example:

Topic links: https://ac.nowcoder.com/acm/contest/884/A

Links: https://ac.nowcoder.com/acm/contest/884/A
Source: Cattle-off network

meeting
Time limit: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 524288K, other languages 1048576K
64bit the IO the Format: LLD%

Title Description

A new city has just been built. There're  nnn interesting places numbered by positive numbers from 111 to nnn.
In order to save resources, only exactly  n−1n-1n1 roads are built to connect these nnn interesting places. Each road connects two places and it takes 1 second to travel between the endpoints of any road.
There is one person in each of the places numbered x1,x2…xkx_1,x_2 \ldots x_kx1,x2xk and they've decided to meet at one place to have a meal. They wonder what's the minimal time needed for them to meet in such a place. (The time required is the maximum time for each person to get to that place.)

Enter a description:

First line two positive integers,  n,kn,kn,k - the number of places and persons.
For each the following  n−1n-1n1 lines, there're two integers a,ba,ba,b that stand for a road connecting place aaa and bbb. It's guaranteed that these roads connected all nnn places.
On the following line there're  kkk different positive integers x1,x2…xkx_1,x_2 \ldots x_kx1,x2xk separated by spaces. These are the numbers of places the persons are at.

Output Description:

A non-negative integer - the minimal time for persons to meet together.
Example 1

Entry

copy
4 2
1 2
3 1
3 4
2 4

Export

copy
2

Explanation

They can meet at place 1 or 3.

Remarks:

1≤n≤1051 \ the n-Leq \ Leq 10 ^ 5 1 the n- 1 0 5
subject to the effect: give you a tree, the tree nodes on certain someone, you ask these people in all of the minimum point in the meeting is how much time
thinking: obviously the distance between the two key points furthest seeking a
look at the code:
#include<iostream>
#include<stack>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=1e5+5;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long LL;
const LL INF=1e18;
int cnt=0;
int head[maxn],dis[maxn];
int a[maxn];
bool vis[maxn],key[maxn];
queue<int>q;
struct E
{
    int to,next;
}edge[maxn<<1];
void add(int u,int v)
{
//    cout<<"u:"<<u<<" v:"<<v<<endl;
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void bfs(int x)
{
//    cout<<"2:"<<vis[2]<<endl;
    vis[x]=true;
    dis[x]=0;
    q.push(x);
    while(!q.empty())
    {
       int u=q.front();q.pop();
       for(int i=head[u];i!=-1;i=edge[i].next)
       {
            int v=edge[i].to;
            if(!vis[v])
            {

                dis[v]=dis[u]+1;
//                cout<<"v:"<<v<<" dis[v]:"<<dis[v]<<endl;
                vis[v]=true;q.push(v);
            }
       }
    }
}
int main()
{
    Memset (head, - . 1 , the sizeof (head));
     int N, K; 
    CIN >> N >> K;
     for ( int I = . 1 ; I <N; I ++ ) 
    { 
        int U, V; CIN >> U> > V; 
        the Add (U, V); the Add (V, U); 
    } 
    for ( int I = . 1 ; I <= K; I ++ ) 
    { 
        CIN >> A [I]; Key [A [I]] = to true ; // tag is a critical point 
    } 
    BFS (a [ . 1 ]); // either take a key
 //    for(int i=1;i<=N;i++) cout<<dis[i]<<" ";cout<<endl;
    int ma=dis[a[1]],id=a[1];
    for(int i=1;i<=N;i++)//找最远的关键点
    {
        if(key[i]&&dis[i]>ma)
        {
            ma=dis[i];id=i;
        }
    }
//    cout<<"id:"<<id<<endl;
    memset(dis,0,sizeof(dis));
    memset(vis,false,sizeof(vis));
    bfs(id);
    for(int i=1;i<=N;i++) cout<<dis[i]<<" ";cout<<endl;;
    1
    -= ma//for(int i=1;i<=N;i++)
    {
        if(key[i]&&dis[i]>ma)
        {
            ma=dis[i];
        }
    }
    cout<<(ma+1)/2<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/caijiaming/p/11257804.html