#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int inf = 1e9, N = 1e6 + 5;
int root;
ll ans = -1, cnt = 0;
vector<vector<int>> g;
vector<int> longest;
vector<bool> vis;
void dfs1(int u) {
longest[u] = vis[u] = true;
for (int v: g[u]) {
if (!vis[v]) {
dfs1(v);
longest[u] = max(longest[u], longest[v] + 1);
}
}
}
void dfs2(int u, int mx, int dist) {
int mx1 = mx, mx2 = 0;
vis[u] = true;
for (int v: g[u]) {
if (vis[v]) continue;
if (mx1 < longest[v]) {
mx2 = mx1;
mx1 = longest[v];
} else {
mx2 = max(mx2, longest[v]);
}
}
for (int v: g[u]) {
if (!vis[v]) {
if (mx1 == longest[v]) dfs2(v, mx2, dist + 1);
else dfs2(v, mx1, dist + 1);
}
}
if (g[u].size() == 1 and u != root) {
if (ans < 1ll * dist * mx) {
ans = 1ll * dist * mx;
cnt = 1;
} else if (ans == 1ll * dist * mx) {
cnt++;
}
}
}
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("game.in", "r", stdin);
// freopen("game.out", "w", stdout);
#endif
int n;
cin >> n;
g.assign(n + 1, {});
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
for (int i = 1; i <= n; i++) {
if (g[i].size() > 1) continue;
root = i;
longest.assign(n + 1, 0);
vis.assign(n + 1, false);
dfs1(i);
vis.assign(n + 1, false);
dfs2(i, 0, 0);
}
cout << ans << ' ' << cnt / 2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
464 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
456 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
464 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
456 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
165 ms |
860 KB |
Output is correct |
26 |
Correct |
156 ms |
976 KB |
Output is correct |
27 |
Correct |
172 ms |
1016 KB |
Output is correct |
28 |
Correct |
171 ms |
972 KB |
Output is correct |
29 |
Correct |
287 ms |
860 KB |
Output is correct |
30 |
Correct |
250 ms |
1112 KB |
Output is correct |
31 |
Correct |
267 ms |
964 KB |
Output is correct |
32 |
Correct |
262 ms |
968 KB |
Output is correct |
33 |
Correct |
3 ms |
860 KB |
Output is correct |
34 |
Correct |
3 ms |
860 KB |
Output is correct |
35 |
Correct |
3 ms |
860 KB |
Output is correct |
36 |
Correct |
3 ms |
980 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 |
4 ms |
860 KB |
Output is correct |
41 |
Correct |
5 ms |
836 KB |
Output is correct |
42 |
Correct |
11 ms |
728 KB |
Output is correct |
43 |
Correct |
20 ms |
604 KB |
Output is correct |
44 |
Correct |
41 ms |
956 KB |
Output is correct |
45 |
Correct |
80 ms |
748 KB |
Output is correct |
46 |
Correct |
103 ms |
712 KB |
Output is correct |
47 |
Correct |
250 ms |
604 KB |
Output is correct |
48 |
Correct |
358 ms |
804 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
464 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
456 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
165 ms |
860 KB |
Output is correct |
26 |
Correct |
156 ms |
976 KB |
Output is correct |
27 |
Correct |
172 ms |
1016 KB |
Output is correct |
28 |
Correct |
171 ms |
972 KB |
Output is correct |
29 |
Correct |
287 ms |
860 KB |
Output is correct |
30 |
Correct |
250 ms |
1112 KB |
Output is correct |
31 |
Correct |
267 ms |
964 KB |
Output is correct |
32 |
Correct |
262 ms |
968 KB |
Output is correct |
33 |
Correct |
3 ms |
860 KB |
Output is correct |
34 |
Correct |
3 ms |
860 KB |
Output is correct |
35 |
Correct |
3 ms |
860 KB |
Output is correct |
36 |
Correct |
3 ms |
980 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 |
4 ms |
860 KB |
Output is correct |
41 |
Correct |
5 ms |
836 KB |
Output is correct |
42 |
Correct |
11 ms |
728 KB |
Output is correct |
43 |
Correct |
20 ms |
604 KB |
Output is correct |
44 |
Correct |
41 ms |
956 KB |
Output is correct |
45 |
Correct |
80 ms |
748 KB |
Output is correct |
46 |
Correct |
103 ms |
712 KB |
Output is correct |
47 |
Correct |
250 ms |
604 KB |
Output is correct |
48 |
Correct |
358 ms |
804 KB |
Output is correct |
49 |
Execution timed out |
2050 ms |
56900 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |