-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPT07Z.cpp
57 lines (51 loc) · 1.21 KB
/
PT07Z.cpp
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
#include <bits/stdc++.h>
using namespace std;
vector<int> g[10005];
pair<int, int> dfs(int node, int parent)
{
int M1, M2, SUB = 0, depth = 0, sub = 0;
M1 = M2 = -100000000;
pair<int, int> p;
if (g[node].size() == 1 && g[node][0] == parent)
return make_pair(0, 0);
for (int i = 0; i < g[node].size(); i++)
{
if (g[node][i] != parent)
{
p = dfs(g[node][i], node);
depth = p.first;
sub = p.second;
//cout << "dfs " << g[node][i] <<" "<<parent<< " " << depth << " " << sub << endl;
if (depth >= M1)
{
M2 = M1;
M1 = depth;
}
else if (depth > M2)
M2 = depth;
if (sub > SUB)
SUB = sub;
}
}
return make_pair(M1 + 1, max(SUB, M1 + M2 + 2));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, p, q, i;
pair<int, int> res;
cin >> n;
n--;
while (n--)
{
cin >> p >> q;
g[p].push_back(q);
g[q].push_back(p);
}
res = dfs(1, 1);
cout << max(res.first, res.second);
return 0;
}