-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree Distances I.cpp
More file actions
49 lines (42 loc) · 1.19 KB
/
Tree Distances I.cpp
File metadata and controls
49 lines (42 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
const ll N=2e5+5 ;
vector<ll> v[N] ;
ll tmp=1,ans=0,d[N]={},dis[N] ;
void DFS(ll x,ll p,ll cnt){
d[x]=cnt ; // dis[x]=max(dis[x],cnt) ;
for(auto i : v[x]){
if (i!=p){
if (cnt+1>ans){
ans=cnt+1 ;
tmp=i ;
}
DFS(i,x,cnt+1) ;
}
}
}
int main() {
IOS ;
ll n,x,y ;
cin>>n ;
for(ll i=1 ; i<n ; i++){
cin>>x>>y ;
v[y].push_back(x) ;
v[x].push_back(y) ;
}
DFS(1,-1,0) ;
ans=0 ;
DFS(tmp,-1,0) ; // tmp is First Node of Maximum Diameter Path
for(ll i=1 ; i<=n ; i++)
dis[i]=max(dis[i],d[i]) ; // Distance From First node of Maximum Diameter path.
memset(d,0,sizeof(d)) ;
ans=0 ;
DFS(tmp,-1,0) ; // tmp was Last Node of Maximum Diameter Path
for(ll i=1 ; i<=n ; i++)
dis[i]=max(dis[i],d[i]) ; // Distance From Last node of Maximum Diameter path.
for(ll i=1 ; i<=n ; i++) cout<<dis[i]<<" " ;
return 0;
}