#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
const int N = 5e3 + 5;
const int INF = 1e9;
int n;
vector <int> adj[N];
ll ans, cnt;
using T = pair <ll, ll>;
T dp[N];
// <maximum distance from current node, number of ways>
void dfs(int u, int p, int dep) {
dp[u] = make_pair(dep, 1);
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u, dep + 1);
if (dp[u].first < dp[v].first)
dp[u] = make_pair(dp[v].first, dp[v].second);
else if (dp[u].first == dp[v].first)
dp[u].second += dp[v].second;
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(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);
}
//sub2: O(n^2)
ans = 0, cnt = 1;
for (int u = 1; u <= n; ++u) {
dfs(u, u, 0);
vector <T> cand;
for (int v : adj[u]) cand.push_back(dp[v]);
sort(cand.rbegin(), cand.rend());
debug(cand);
if ((int) cand.size() <= 2) continue; // at least three candidates;
ll hardness = cand[0].first * (cand[1].first + cand[2].first);
ll cur_cnt = 0;
ll d = 0;
for (int i = 0; i < (int) (cand.size()); ++i) {
if (cand[i].first == cand[2].first) cur_cnt += d * cand[2].second;
if (cand[i].first == cand[1].first) d += cand[i].second;
}
if (ans < hardness) {
ans = hardness;
cnt = cur_cnt;
}
else if (ans == hardness)
cnt += cur_cnt;
}
cout << ans << " " << cnt << "\n";
return 0;
}
/*
leaf nodes always product maximum distance?
*/
Compilation message
road.cpp: In function 'int main()':
road.cpp:9:20: warning: statement has no effect [-Wunused-value]
9 | #define debug(...) 166
| ^~~
road.cpp:51:9: note: in expansion of macro 'debug'
51 | debug(cand);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
584 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 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 |
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 |
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 |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
584 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 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 |
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 |
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 |
278 ms |
1104 KB |
Output is correct |
26 |
Correct |
288 ms |
1044 KB |
Output is correct |
27 |
Correct |
287 ms |
860 KB |
Output is correct |
28 |
Correct |
274 ms |
1112 KB |
Output is correct |
29 |
Correct |
205 ms |
856 KB |
Output is correct |
30 |
Correct |
206 ms |
856 KB |
Output is correct |
31 |
Correct |
230 ms |
1036 KB |
Output is correct |
32 |
Correct |
203 ms |
1028 KB |
Output is correct |
33 |
Correct |
306 ms |
1116 KB |
Output is correct |
34 |
Correct |
304 ms |
1364 KB |
Output is correct |
35 |
Correct |
316 ms |
1116 KB |
Output is correct |
36 |
Correct |
324 ms |
1128 KB |
Output is correct |
37 |
Correct |
565 ms |
1224 KB |
Output is correct |
38 |
Correct |
538 ms |
1224 KB |
Output is correct |
39 |
Correct |
451 ms |
1004 KB |
Output is correct |
40 |
Correct |
420 ms |
912 KB |
Output is correct |
41 |
Correct |
463 ms |
872 KB |
Output is correct |
42 |
Correct |
404 ms |
860 KB |
Output is correct |
43 |
Correct |
351 ms |
856 KB |
Output is correct |
44 |
Correct |
351 ms |
860 KB |
Output is correct |
45 |
Correct |
288 ms |
816 KB |
Output is correct |
46 |
Correct |
181 ms |
604 KB |
Output is correct |
47 |
Correct |
202 ms |
856 KB |
Output is correct |
48 |
Correct |
126 ms |
864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
584 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 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 |
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 |
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 |
278 ms |
1104 KB |
Output is correct |
26 |
Correct |
288 ms |
1044 KB |
Output is correct |
27 |
Correct |
287 ms |
860 KB |
Output is correct |
28 |
Correct |
274 ms |
1112 KB |
Output is correct |
29 |
Correct |
205 ms |
856 KB |
Output is correct |
30 |
Correct |
206 ms |
856 KB |
Output is correct |
31 |
Correct |
230 ms |
1036 KB |
Output is correct |
32 |
Correct |
203 ms |
1028 KB |
Output is correct |
33 |
Correct |
306 ms |
1116 KB |
Output is correct |
34 |
Correct |
304 ms |
1364 KB |
Output is correct |
35 |
Correct |
316 ms |
1116 KB |
Output is correct |
36 |
Correct |
324 ms |
1128 KB |
Output is correct |
37 |
Correct |
565 ms |
1224 KB |
Output is correct |
38 |
Correct |
538 ms |
1224 KB |
Output is correct |
39 |
Correct |
451 ms |
1004 KB |
Output is correct |
40 |
Correct |
420 ms |
912 KB |
Output is correct |
41 |
Correct |
463 ms |
872 KB |
Output is correct |
42 |
Correct |
404 ms |
860 KB |
Output is correct |
43 |
Correct |
351 ms |
856 KB |
Output is correct |
44 |
Correct |
351 ms |
860 KB |
Output is correct |
45 |
Correct |
288 ms |
816 KB |
Output is correct |
46 |
Correct |
181 ms |
604 KB |
Output is correct |
47 |
Correct |
202 ms |
856 KB |
Output is correct |
48 |
Correct |
126 ms |
864 KB |
Output is correct |
49 |
Runtime error |
1 ms |
856 KB |
Execution killed with signal 11 |
50 |
Halted |
0 ms |
0 KB |
- |