#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);
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);
function<void(int, int)> dfs = [&](int x, int p) {
if (p != -1 && 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]));
}
};
dfs(0, -1);
ll ans = 0, cnt = 1;
for (int r = 0; r < N; r++) {
if (adj[r].size() <= 2) continue;
dp = vector<S>(N);
dfs(r, -1);
vector<S> a;
for (int y : adj[r]) 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;
}
cout << ans << " " << cnt;
return 6/22;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
456 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
456 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
49 ms |
1116 KB |
Output is correct |
26 |
Correct |
47 ms |
1116 KB |
Output is correct |
27 |
Correct |
48 ms |
1116 KB |
Output is correct |
28 |
Correct |
55 ms |
1116 KB |
Output is correct |
29 |
Correct |
144 ms |
1116 KB |
Output is correct |
30 |
Correct |
140 ms |
1112 KB |
Output is correct |
31 |
Correct |
148 ms |
1112 KB |
Output is correct |
32 |
Correct |
139 ms |
1124 KB |
Output is correct |
33 |
Correct |
2 ms |
1112 KB |
Output is correct |
34 |
Correct |
3 ms |
1112 KB |
Output is correct |
35 |
Correct |
3 ms |
1368 KB |
Output is correct |
36 |
Correct |
2 ms |
1112 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
1116 KB |
Output is correct |
39 |
Correct |
2 ms |
856 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
860 KB |
Output is correct |
42 |
Correct |
2 ms |
708 KB |
Output is correct |
43 |
Correct |
2 ms |
856 KB |
Output is correct |
44 |
Correct |
2 ms |
600 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
676 KB |
Output is correct |
48 |
Correct |
1 ms |
860 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
456 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
49 ms |
1116 KB |
Output is correct |
26 |
Correct |
47 ms |
1116 KB |
Output is correct |
27 |
Correct |
48 ms |
1116 KB |
Output is correct |
28 |
Correct |
55 ms |
1116 KB |
Output is correct |
29 |
Correct |
144 ms |
1116 KB |
Output is correct |
30 |
Correct |
140 ms |
1112 KB |
Output is correct |
31 |
Correct |
148 ms |
1112 KB |
Output is correct |
32 |
Correct |
139 ms |
1124 KB |
Output is correct |
33 |
Correct |
2 ms |
1112 KB |
Output is correct |
34 |
Correct |
3 ms |
1112 KB |
Output is correct |
35 |
Correct |
3 ms |
1368 KB |
Output is correct |
36 |
Correct |
2 ms |
1112 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
2 ms |
1116 KB |
Output is correct |
39 |
Correct |
2 ms |
856 KB |
Output is correct |
40 |
Correct |
2 ms |
860 KB |
Output is correct |
41 |
Correct |
2 ms |
860 KB |
Output is correct |
42 |
Correct |
2 ms |
708 KB |
Output is correct |
43 |
Correct |
2 ms |
856 KB |
Output is correct |
44 |
Correct |
2 ms |
600 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
676 KB |
Output is correct |
48 |
Correct |
1 ms |
860 KB |
Output is correct |
49 |
Execution timed out |
2041 ms |
66504 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |