#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5007;
int n;
vector <int> g[N];
int dep[N];
int root = -1;
map <int, int> cnt;
int mx = -1, mx_cnt = 0;
void pre_dfs(int v, int pr) {
dep[v] = 1;
for (int u : g[v]) {
if (u != pr) {
pre_dfs(u, v);
dep[v] = max(dep[v], dep[u] + 1);
}
}
}
void dfs(int v, int pr, int cur_mx, int cnt_n) {
if ((int) g[v].size() == 1 && v != root) {
cnt[cur_mx * cnt_n]++;
if (cur_mx * cnt_n > mx) {
mx = cur_mx * cnt_n;
mx_cnt = 1;
} else if (cur_mx * cnt_n == mx) {
mx_cnt++;
}
return;
}
vector <pair <int, int> > f;
for (int u : g[v]) {
if (u != pr) {
f.push_back({dep[u], u});
}
}
sort(f.begin(), f.end());
for (int u : g[v]) {
if (u != pr) {
if ((int) f.size() == 1) {
dfs(u, v, cur_mx, cnt_n + 1);
} else {
if (f.back().second == u) {
int new_mx = cur_mx;
new_mx = max(new_mx, f[(int) f.size() - 2].first);
dfs(u, v, new_mx, cnt_n + 1);
} else {
int new_mx = cur_mx;
new_mx = max(new_mx, f.back().first);
dfs(u, v, new_mx, cnt_n + 1);
}
}
}
}
}
void calc(int v) {
root = v;
memset(dep, sizeof dep, 0);
pre_dfs(v, 0);
dfs(v, 0, 0, 0);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if ((int) g[i].size() == 1) {
calc(i);
}
}
cout << mx << ' ' << mx_cnt / 2 << '\n';
return 0;
}
Compilation message
road.cpp: In function 'void calc(int)':
road.cpp:65:30: warning: 'memset' used with constant zero length parameter; this could be due to transposed parameters [-Wmemset-transposed-args]
memset(dep, sizeof dep, 0);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
508 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
508 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
508 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
508 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
755 ms |
1144 KB |
Output is correct |
26 |
Correct |
694 ms |
1144 KB |
Output is correct |
27 |
Correct |
744 ms |
1016 KB |
Output is correct |
28 |
Correct |
757 ms |
1124 KB |
Output is correct |
29 |
Execution timed out |
2055 ms |
24944 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
508 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
508 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
755 ms |
1144 KB |
Output is correct |
26 |
Correct |
694 ms |
1144 KB |
Output is correct |
27 |
Correct |
744 ms |
1016 KB |
Output is correct |
28 |
Correct |
757 ms |
1124 KB |
Output is correct |
29 |
Execution timed out |
2055 ms |
24944 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |