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
space restrictions: C / C ++ 524288K, other languages 1048576K
64bit the IO the Format: LLD%
Title Description
Enter a description:
Output Description:
A non-negative integer - the minimal time for persons to meet together.
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; }