#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = -1e9;
const int MAXN = 5e3 + 25;
vector <int> adj[MAXN];
int n;
ll mx = 0, cnt = 1;
pair <ll, ll> dfs (int pos, int par, int dep) {
if (par != -1 && adj[pos].size() == 1) {
return {0, 1};
}
pair <ll, ll> d1 = {inf, 0}, d2 = {inf, 0};
pair <ll, ll> ret = {inf, 0};
bool flag = 0;
for (auto j : adj[pos]) {
if (j == par) continue;
auto h = dfs(j, pos, dep + 1);
h.first += 1;
if (h.first > d1.first) {
d2 = d1; d1 = h;
} else if (h.first == d1.first) {
d1.second += h.second;
} else if (h.first > d2.first) {
d2 = h;
} else if (h.first == d2.first) {
d2.second += h.second;
}
if (h.first > ret.first) {
ret = h; flag = 0;
} else if (h.first == ret.first) {
ret.second += h.second;
flag = 1;
}
}
if (flag) {
ll t = (dep + d1.first) * d1.first, s = ret.second;
if (t > mx) {
mx = t; cnt = s;
} else if (t == mx) {
cnt += s;
}
return ret;
}
ll t = (dep + d2.first) * d1.first, s = d2.second;
if (t > mx) {
mx = t; cnt = s;
} else if (t == mx) {
cnt += s;
}
return ret;
}
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if ((int)adj[i].size() == 1) {
dfs(i, -1, 0);
}
}
if (mx == 0) {
cout << "0 1\n";
return 0;
}
cout << mx << " " << cnt / 2 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
64 ms |
988 KB |
Output is correct |
26 |
Correct |
63 ms |
856 KB |
Output is correct |
27 |
Correct |
67 ms |
860 KB |
Output is correct |
28 |
Correct |
66 ms |
984 KB |
Output is correct |
29 |
Correct |
92 ms |
996 KB |
Output is correct |
30 |
Correct |
92 ms |
860 KB |
Output is correct |
31 |
Correct |
100 ms |
980 KB |
Output is correct |
32 |
Correct |
97 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
1136 KB |
Output is correct |
34 |
Correct |
2 ms |
1116 KB |
Output is correct |
35 |
Correct |
2 ms |
1116 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
2 ms |
1116 KB |
Output is correct |
38 |
Correct |
2 ms |
1116 KB |
Output is correct |
39 |
Correct |
3 ms |
860 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
3 ms |
604 KB |
Output is correct |
42 |
Correct |
6 ms |
752 KB |
Output is correct |
43 |
Correct |
9 ms |
600 KB |
Output is correct |
44 |
Correct |
16 ms |
604 KB |
Output is correct |
45 |
Correct |
40 ms |
604 KB |
Output is correct |
46 |
Correct |
40 ms |
604 KB |
Output is correct |
47 |
Correct |
89 ms |
600 KB |
Output is correct |
48 |
Correct |
96 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
64 ms |
988 KB |
Output is correct |
26 |
Correct |
63 ms |
856 KB |
Output is correct |
27 |
Correct |
67 ms |
860 KB |
Output is correct |
28 |
Correct |
66 ms |
984 KB |
Output is correct |
29 |
Correct |
92 ms |
996 KB |
Output is correct |
30 |
Correct |
92 ms |
860 KB |
Output is correct |
31 |
Correct |
100 ms |
980 KB |
Output is correct |
32 |
Correct |
97 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
1136 KB |
Output is correct |
34 |
Correct |
2 ms |
1116 KB |
Output is correct |
35 |
Correct |
2 ms |
1116 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
2 ms |
1116 KB |
Output is correct |
38 |
Correct |
2 ms |
1116 KB |
Output is correct |
39 |
Correct |
3 ms |
860 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
3 ms |
604 KB |
Output is correct |
42 |
Correct |
6 ms |
752 KB |
Output is correct |
43 |
Correct |
9 ms |
600 KB |
Output is correct |
44 |
Correct |
16 ms |
604 KB |
Output is correct |
45 |
Correct |
40 ms |
604 KB |
Output is correct |
46 |
Correct |
40 ms |
604 KB |
Output is correct |
47 |
Correct |
89 ms |
600 KB |
Output is correct |
48 |
Correct |
96 ms |
600 KB |
Output is correct |
49 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
50 |
Halted |
0 ms |
0 KB |
- |