#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define sz(v) (int)(v).size()
#define pii pair<int, int>
using namespace std;
const int MXn = 500005;
void make_better(int &A, int &B, int x, int y) {
if (A < x) {
A = x;
B = y;
} else if (A == x) {
B += y;
}
}
pii get_better(int A, int B, int x, int y) {
if (A < x) {
A = x;
B = y;
} else if (A == x) {
B += y;
}
return {A, B};
}
int n;
vector<int> adj[MXn];
int mx[MXn], cnt[MXn];
void DFS1(int u, int par) {
mx[u] = 0, cnt[u] = 1;
for (int v: adj[u]) if (v != par) {
DFS1(v, u);
make_better(mx[u], cnt[u], mx[v] + 1, cnt[v]);
}
}
int H = 0, dem = 1;
void DFS2(int u, int par, int Mx, int Cnt) {
vector<tuple<int, int, int>> nah;
vector<pii> paths;
if (Mx) {
paths.emplace_back(Mx, Cnt);
}
for (int v: adj[u]) if (v != par) {
nah.emplace_back(v, mx[v] + 1, cnt[v]);
paths.emplace_back(mx[v] + 1, cnt[v]);
}
sort(begin(paths), end(paths), greater<pii>());
if (sz(paths) >= 3) {
int z = paths[0].fi, y = paths[1].fi, x = paths[2].fi;
int h = z * (x + y);
if (H <= h) {
int Dz = paths[0].se, Dy = paths[1].se, Dx = paths[2].se;
int Dem = 0;
if (x < y && y < z) {
Dem = Dx * Dy;
} else if (x == y && y < z) {
Dem = Dy * Dx;
int tmp = Dx + Dy;
for (int i = 3; i < sz(paths); i++) if (paths[i].fi == x) {
Dem += tmp * paths[i].se;
tmp += paths[i].se;
}
} else if (x < y && y == z) {
Dem = Dx * (Dy + Dz);
} else if (x == y && y == z) {
Dem = Dz * Dy + (Dz + Dy) * Dx;
int tmp = Dx + Dy + Dz;
for (int i = 3; i < sz(paths); i++) if (paths[i].fi == x) {
Dem += tmp * paths[i].se;
tmp += paths[i].se;
}
}
make_better(H, dem, h, Dem);
}
}
vector<pii> s;
if (sz(nah)) {
s.resize(sz(nah));
s.back() = {(get<1>(nah.back())) + 1, get<2>(nah.back())};
for (int i = sz(nah) - 2; i >= 0; i--) {
s[i] = get_better(s[i + 1].fi, s[i + 1].se, (get<1>(nah[i])) + 1, get<2>(nah[i]));
}
}
pii p = {Mx + 1, Cnt};
for (int i = 0; i < sz(nah); i++) {
pii New = p;
if (i != sz(nah) - 1) New = get_better(New.fi, New.se, s[i + 1].fi, s[i + 1].se);
DFS2(get<0>(nah[i]), u, New.fi, New.se);
p = get_better(p.fi, p.se, (get<1>(nah[i])) + 1, get<2>(nah[i]));
}
}
signed main(void) {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
DFS1(1, 0);
DFS2(1, 0, 0, 1);
cout << H << ' ' << dem;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15708 KB |
Output is correct |
2 |
Correct |
2 ms |
15708 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
2 ms |
15708 KB |
Output is correct |
5 |
Correct |
2 ms |
15708 KB |
Output is correct |
6 |
Correct |
2 ms |
15708 KB |
Output is correct |
7 |
Correct |
2 ms |
15548 KB |
Output is correct |
8 |
Correct |
2 ms |
15708 KB |
Output is correct |
9 |
Correct |
2 ms |
15708 KB |
Output is correct |
10 |
Correct |
2 ms |
15708 KB |
Output is correct |
11 |
Correct |
2 ms |
15708 KB |
Output is correct |
12 |
Correct |
2 ms |
15708 KB |
Output is correct |
13 |
Correct |
2 ms |
15704 KB |
Output is correct |
14 |
Correct |
3 ms |
15960 KB |
Output is correct |
15 |
Correct |
2 ms |
15708 KB |
Output is correct |
16 |
Correct |
2 ms |
15708 KB |
Output is correct |
17 |
Correct |
2 ms |
15708 KB |
Output is correct |
18 |
Correct |
2 ms |
15764 KB |
Output is correct |
19 |
Correct |
2 ms |
15708 KB |
Output is correct |
20 |
Correct |
3 ms |
15708 KB |
Output is correct |
21 |
Correct |
2 ms |
15708 KB |
Output is correct |
22 |
Correct |
2 ms |
15708 KB |
Output is correct |
23 |
Correct |
2 ms |
15708 KB |
Output is correct |
24 |
Correct |
2 ms |
15708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15708 KB |
Output is correct |
2 |
Correct |
2 ms |
15708 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
2 ms |
15708 KB |
Output is correct |
5 |
Correct |
2 ms |
15708 KB |
Output is correct |
6 |
Correct |
2 ms |
15708 KB |
Output is correct |
7 |
Correct |
2 ms |
15548 KB |
Output is correct |
8 |
Correct |
2 ms |
15708 KB |
Output is correct |
9 |
Correct |
2 ms |
15708 KB |
Output is correct |
10 |
Correct |
2 ms |
15708 KB |
Output is correct |
11 |
Correct |
2 ms |
15708 KB |
Output is correct |
12 |
Correct |
2 ms |
15708 KB |
Output is correct |
13 |
Correct |
2 ms |
15704 KB |
Output is correct |
14 |
Correct |
3 ms |
15960 KB |
Output is correct |
15 |
Correct |
2 ms |
15708 KB |
Output is correct |
16 |
Correct |
2 ms |
15708 KB |
Output is correct |
17 |
Correct |
2 ms |
15708 KB |
Output is correct |
18 |
Correct |
2 ms |
15764 KB |
Output is correct |
19 |
Correct |
2 ms |
15708 KB |
Output is correct |
20 |
Correct |
3 ms |
15708 KB |
Output is correct |
21 |
Correct |
2 ms |
15708 KB |
Output is correct |
22 |
Correct |
2 ms |
15708 KB |
Output is correct |
23 |
Correct |
2 ms |
15708 KB |
Output is correct |
24 |
Correct |
2 ms |
15708 KB |
Output is correct |
25 |
Correct |
4 ms |
16476 KB |
Output is correct |
26 |
Correct |
4 ms |
16732 KB |
Output is correct |
27 |
Correct |
3 ms |
16788 KB |
Output is correct |
28 |
Correct |
4 ms |
16732 KB |
Output is correct |
29 |
Correct |
4 ms |
16732 KB |
Output is correct |
30 |
Correct |
3 ms |
16928 KB |
Output is correct |
31 |
Correct |
3 ms |
16732 KB |
Output is correct |
32 |
Correct |
4 ms |
16624 KB |
Output is correct |
33 |
Correct |
3 ms |
16476 KB |
Output is correct |
34 |
Correct |
3 ms |
16784 KB |
Output is correct |
35 |
Correct |
4 ms |
16732 KB |
Output is correct |
36 |
Correct |
3 ms |
16732 KB |
Output is correct |
37 |
Correct |
5 ms |
16732 KB |
Output is correct |
38 |
Correct |
4 ms |
17500 KB |
Output is correct |
39 |
Correct |
4 ms |
16476 KB |
Output is correct |
40 |
Correct |
3 ms |
16220 KB |
Output is correct |
41 |
Correct |
3 ms |
16020 KB |
Output is correct |
42 |
Correct |
3 ms |
15964 KB |
Output is correct |
43 |
Correct |
3 ms |
15964 KB |
Output is correct |
44 |
Correct |
3 ms |
15996 KB |
Output is correct |
45 |
Correct |
3 ms |
15964 KB |
Output is correct |
46 |
Correct |
4 ms |
15964 KB |
Output is correct |
47 |
Correct |
4 ms |
16220 KB |
Output is correct |
48 |
Correct |
5 ms |
16476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15708 KB |
Output is correct |
2 |
Correct |
2 ms |
15708 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
2 ms |
15708 KB |
Output is correct |
5 |
Correct |
2 ms |
15708 KB |
Output is correct |
6 |
Correct |
2 ms |
15708 KB |
Output is correct |
7 |
Correct |
2 ms |
15548 KB |
Output is correct |
8 |
Correct |
2 ms |
15708 KB |
Output is correct |
9 |
Correct |
2 ms |
15708 KB |
Output is correct |
10 |
Correct |
2 ms |
15708 KB |
Output is correct |
11 |
Correct |
2 ms |
15708 KB |
Output is correct |
12 |
Correct |
2 ms |
15708 KB |
Output is correct |
13 |
Correct |
2 ms |
15704 KB |
Output is correct |
14 |
Correct |
3 ms |
15960 KB |
Output is correct |
15 |
Correct |
2 ms |
15708 KB |
Output is correct |
16 |
Correct |
2 ms |
15708 KB |
Output is correct |
17 |
Correct |
2 ms |
15708 KB |
Output is correct |
18 |
Correct |
2 ms |
15764 KB |
Output is correct |
19 |
Correct |
2 ms |
15708 KB |
Output is correct |
20 |
Correct |
3 ms |
15708 KB |
Output is correct |
21 |
Correct |
2 ms |
15708 KB |
Output is correct |
22 |
Correct |
2 ms |
15708 KB |
Output is correct |
23 |
Correct |
2 ms |
15708 KB |
Output is correct |
24 |
Correct |
2 ms |
15708 KB |
Output is correct |
25 |
Correct |
4 ms |
16476 KB |
Output is correct |
26 |
Correct |
4 ms |
16732 KB |
Output is correct |
27 |
Correct |
3 ms |
16788 KB |
Output is correct |
28 |
Correct |
4 ms |
16732 KB |
Output is correct |
29 |
Correct |
4 ms |
16732 KB |
Output is correct |
30 |
Correct |
3 ms |
16928 KB |
Output is correct |
31 |
Correct |
3 ms |
16732 KB |
Output is correct |
32 |
Correct |
4 ms |
16624 KB |
Output is correct |
33 |
Correct |
3 ms |
16476 KB |
Output is correct |
34 |
Correct |
3 ms |
16784 KB |
Output is correct |
35 |
Correct |
4 ms |
16732 KB |
Output is correct |
36 |
Correct |
3 ms |
16732 KB |
Output is correct |
37 |
Correct |
5 ms |
16732 KB |
Output is correct |
38 |
Correct |
4 ms |
17500 KB |
Output is correct |
39 |
Correct |
4 ms |
16476 KB |
Output is correct |
40 |
Correct |
3 ms |
16220 KB |
Output is correct |
41 |
Correct |
3 ms |
16020 KB |
Output is correct |
42 |
Correct |
3 ms |
15964 KB |
Output is correct |
43 |
Correct |
3 ms |
15964 KB |
Output is correct |
44 |
Correct |
3 ms |
15996 KB |
Output is correct |
45 |
Correct |
3 ms |
15964 KB |
Output is correct |
46 |
Correct |
4 ms |
15964 KB |
Output is correct |
47 |
Correct |
4 ms |
16220 KB |
Output is correct |
48 |
Correct |
5 ms |
16476 KB |
Output is correct |
49 |
Correct |
305 ms |
105096 KB |
Output is correct |
50 |
Correct |
265 ms |
116564 KB |
Output is correct |
51 |
Correct |
264 ms |
126292 KB |
Output is correct |
52 |
Incorrect |
247 ms |
91548 KB |
Output isn't correct |
53 |
Halted |
0 ms |
0 KB |
- |