#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = -1e9;
struct S {
int mx, fq;
S() : mx(inf), fq(0) {}
S(int v, int c) : mx(v), fq(c) {}
};
S f(const S &a, const S &b) {
if (a.mx > b.mx) return a;
if (b.mx > a.mx) return b;
return S(a.mx, a.fq + b.fq);
}
S g(S a) {
a.mx++;
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<vector<int>> adj(N), ch(N);
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<S> dp(N), pd(N);
function<void(int, int)> dfs = [&](int x, int p) {
if (x && adj[x].size() == 1) {
dp[x] = S(0, 1);
return;
}
for (int y : adj[x]){
if (y == p) continue;
dfs(y, x);
dp[x] = f(dp[x], g(dp[y]));
ch[x].push_back(y);
}
};
dfs(0, -1);
ll ans = 0, cnt = 1;
function<void(int)> rer = [&](int x) {
if (adj[x].size() > 2) {
vector<S> a{g(pd[x])};
for (int y : ch[x]) a.push_back(g(dp[y]));
sort(a.begin(), a.end(), [&](auto &s, auto &t) {
return s.mx > t.mx;
});
ll v = 1LL * a[0].mx * (a[1].mx + a[2].mx), c = 0;
if (a[1].mx == a[2].mx) {
for (auto &s : a) if (s.mx == a[1].mx) c += s.fq;
c *= c;
for (auto &s : a) if (s.mx == a[1].mx) c -= 1LL * s.fq * s.fq;
c /= 2;
} else {
int c1 = 0, c2 = 0;
for (auto &s : a) {
if (s.mx == a[1].mx) c1 += s.fq;
if (s.mx == a[2].mx) c2 += s.fq;
}
c = 1LL * c1 * c2;
}
if (v > ans) ans = v, cnt = c;
else if (v == ans) cnt += c;
}
int K = ch[x].size(); if (K == 0) return;
vector<S> pre(K + 1), suf(K + 1);
for (int i = 0; i < K; i++) pre[i + 1] = f(pre[i], dp[ch[x][i]]);
for (int i = K - 1; i; i--) suf[i - 1] = f(suf[i], dp[ch[x][i]]);
for (int i = 0; i < K; i++) {
int y = ch[x][i];
if (x == 0 && K == 1) pd[y] = S(0, 1);
else pd[y] = g(f(f(pre[i], suf[i]), pd[x]));
rer(y);
}
};
rer(0);
cout << ans << " " << cnt;
return 6/22;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
2 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 |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
2 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 |
2 ms |
1180 KB |
Output is correct |
26 |
Correct |
2 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
4 ms |
1244 KB |
Output is correct |
32 |
Correct |
4 ms |
1116 KB |
Output is correct |
33 |
Correct |
2 ms |
1372 KB |
Output is correct |
34 |
Correct |
3 ms |
1628 KB |
Output is correct |
35 |
Correct |
2 ms |
1628 KB |
Output is correct |
36 |
Correct |
3 ms |
1320 KB |
Output is correct |
37 |
Correct |
3 ms |
1624 KB |
Output is correct |
38 |
Correct |
3 ms |
2140 KB |
Output is correct |
39 |
Correct |
2 ms |
1372 KB |
Output is correct |
40 |
Correct |
2 ms |
1096 KB |
Output is correct |
41 |
Correct |
2 ms |
1116 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
2 ms |
856 KB |
Output is correct |
46 |
Correct |
2 ms |
860 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
2 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 |
2 ms |
1180 KB |
Output is correct |
26 |
Correct |
2 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
4 ms |
1244 KB |
Output is correct |
32 |
Correct |
4 ms |
1116 KB |
Output is correct |
33 |
Correct |
2 ms |
1372 KB |
Output is correct |
34 |
Correct |
3 ms |
1628 KB |
Output is correct |
35 |
Correct |
2 ms |
1628 KB |
Output is correct |
36 |
Correct |
3 ms |
1320 KB |
Output is correct |
37 |
Correct |
3 ms |
1624 KB |
Output is correct |
38 |
Correct |
3 ms |
2140 KB |
Output is correct |
39 |
Correct |
2 ms |
1372 KB |
Output is correct |
40 |
Correct |
2 ms |
1096 KB |
Output is correct |
41 |
Correct |
2 ms |
1116 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
2 ms |
860 KB |
Output is correct |
45 |
Correct |
2 ms |
856 KB |
Output is correct |
46 |
Correct |
2 ms |
860 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
49 |
Correct |
459 ms |
100188 KB |
Output is correct |
50 |
Correct |
470 ms |
115024 KB |
Output is correct |
51 |
Correct |
442 ms |
121376 KB |
Output is correct |
52 |
Correct |
433 ms |
98108 KB |
Output is correct |
53 |
Correct |
444 ms |
106828 KB |
Output is correct |
54 |
Correct |
467 ms |
113824 KB |
Output is correct |
55 |
Correct |
434 ms |
96340 KB |
Output is correct |
56 |
Correct |
431 ms |
103864 KB |
Output is correct |
57 |
Correct |
454 ms |
129828 KB |
Output is correct |
58 |
Correct |
442 ms |
121288 KB |
Output is correct |
59 |
Correct |
458 ms |
121168 KB |
Output is correct |
60 |
Correct |
475 ms |
117212 KB |
Output is correct |
61 |
Correct |
628 ms |
179280 KB |
Output is correct |
62 |
Correct |
628 ms |
159252 KB |
Output is correct |
63 |
Correct |
612 ms |
101432 KB |
Output is correct |
64 |
Correct |
594 ms |
87820 KB |
Output is correct |
65 |
Correct |
578 ms |
78928 KB |
Output is correct |
66 |
Correct |
572 ms |
74368 KB |
Output is correct |
67 |
Correct |
581 ms |
71876 KB |
Output is correct |
68 |
Correct |
541 ms |
70852 KB |
Output is correct |
69 |
Correct |
571 ms |
70280 KB |
Output is correct |
70 |
Correct |
526 ms |
70160 KB |
Output is correct |
71 |
Correct |
585 ms |
70100 KB |
Output is correct |
72 |
Correct |
520 ms |
70228 KB |
Output is correct |
73 |
Correct |
562 ms |
70136 KB |
Output is correct |
74 |
Correct |
545 ms |
69940 KB |
Output is correct |
75 |
Correct |
540 ms |
69748 KB |
Output is correct |
76 |
Correct |
464 ms |
69132 KB |
Output is correct |
77 |
Correct |
411 ms |
68060 KB |
Output is correct |
78 |
Correct |
242 ms |
66180 KB |
Output is correct |