#include <bits/stdc++.h>
using namespace std;
const int NM = 5000;
int N, deg[NM+5], f[NM+5], g[NM+5], dep[NM+5];
vector <int> adj[NM+5];
int ans = -1, cnt = -1;
void dfs(int u, int p){
dep[u] = (p == -1 ? 0 : dep[p]+1);
f[u] = g[u] = 0;
for (int v : adj[u]){
if (v == p) continue;
dfs(v, u);
if (f[v]+1 > f[u]){
g[u] = f[u];
f[u] = f[v]+1;
}
else if (f[v]+1 > g[u]){
g[u] = f[v]+1;
}
}
}
void calc(int u, int p, int cur){
bool has_child = 0;
for (int v : adj[u]){
if (v == p) continue;
has_child = 1;
if (f[v]+1 == f[u]) calc(v, u, max(cur, g[u]));
else calc(v, u, max(cur, f[u]));
}
if (!has_child){
if (dep[u]*cur > ans){
ans = dep[u]*cur;
cnt = 1;
}
else if (dep[u]*cur == ans) cnt++;
}
}
void solve(int r){
dfs(r, -1);
calc(r, -1, 0);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i < N; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
deg[u]++; deg[v]++;
}
for (int i = 1; i <= N; i++)
if (deg[i] == 1) solve(i);
cout << ans << ' ' << cnt/2;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
580 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
580 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
144 ms |
860 KB |
Output is correct |
26 |
Correct |
146 ms |
1044 KB |
Output is correct |
27 |
Correct |
160 ms |
1112 KB |
Output is correct |
28 |
Correct |
172 ms |
860 KB |
Output is correct |
29 |
Correct |
208 ms |
860 KB |
Output is correct |
30 |
Correct |
220 ms |
1108 KB |
Output is correct |
31 |
Correct |
196 ms |
860 KB |
Output is correct |
32 |
Correct |
226 ms |
860 KB |
Output is correct |
33 |
Correct |
3 ms |
1116 KB |
Output is correct |
34 |
Correct |
3 ms |
1116 KB |
Output is correct |
35 |
Correct |
3 ms |
1116 KB |
Output is correct |
36 |
Correct |
3 ms |
952 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 |
940 KB |
Output is correct |
41 |
Correct |
5 ms |
860 KB |
Output is correct |
42 |
Correct |
11 ms |
1112 KB |
Output is correct |
43 |
Correct |
19 ms |
860 KB |
Output is correct |
44 |
Correct |
43 ms |
860 KB |
Output is correct |
45 |
Correct |
77 ms |
604 KB |
Output is correct |
46 |
Correct |
88 ms |
604 KB |
Output is correct |
47 |
Correct |
218 ms |
860 KB |
Output is correct |
48 |
Correct |
278 ms |
868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
580 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
144 ms |
860 KB |
Output is correct |
26 |
Correct |
146 ms |
1044 KB |
Output is correct |
27 |
Correct |
160 ms |
1112 KB |
Output is correct |
28 |
Correct |
172 ms |
860 KB |
Output is correct |
29 |
Correct |
208 ms |
860 KB |
Output is correct |
30 |
Correct |
220 ms |
1108 KB |
Output is correct |
31 |
Correct |
196 ms |
860 KB |
Output is correct |
32 |
Correct |
226 ms |
860 KB |
Output is correct |
33 |
Correct |
3 ms |
1116 KB |
Output is correct |
34 |
Correct |
3 ms |
1116 KB |
Output is correct |
35 |
Correct |
3 ms |
1116 KB |
Output is correct |
36 |
Correct |
3 ms |
952 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 |
940 KB |
Output is correct |
41 |
Correct |
5 ms |
860 KB |
Output is correct |
42 |
Correct |
11 ms |
1112 KB |
Output is correct |
43 |
Correct |
19 ms |
860 KB |
Output is correct |
44 |
Correct |
43 ms |
860 KB |
Output is correct |
45 |
Correct |
77 ms |
604 KB |
Output is correct |
46 |
Correct |
88 ms |
604 KB |
Output is correct |
47 |
Correct |
218 ms |
860 KB |
Output is correct |
48 |
Correct |
278 ms |
868 KB |
Output is correct |
49 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
50 |
Halted |
0 ms |
0 KB |
- |