-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiameterOfTree.cpp
More file actions
62 lines (57 loc) · 1.17 KB
/
diameterOfTree.cpp
File metadata and controls
62 lines (57 loc) · 1.17 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
50
51
52
53
54
55
56
57
58
59
60
61
62
/* To Kaise Hain Aaplog*/
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int> v[100001];
bool vis[100001] = {0};
int maxNode=0,maxD=0;
void dfs(int s,int d)
{
if(d>maxD){
maxD = d;
maxNode = s;
}
vis[s] = true;
for (int u : v[s])
{
if (vis[u] == false)
{
dfs(u,d+1);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
int t;
cin >> t;
while (t--)
{
int n;
cin >> n ;
maxD=0;
maxNode =-1;
for (int i = 0; i < n-1; i++)
{
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(0,0);
maxD=0;
memset(vis,false,sizeof(vis));
dfs(maxNode,0);
cout<<ceil((float)maxD/2)<<"\n";
for(int i=0;i<=n;i++){
v[i].clear();
}
memset(vis,false,sizeof(vis));
}
return 0;
}