#include <bits/stdc++.h>
#define I64 long long
using namespace std;
const int N = (int) 5E5;
struct node {
int d;
I64 x;
friend bool operator < (node u, node v) {
return u.d > v.d;
}
};
void merge(node &u, node v) {
if (u.d < v.d) {
u = v;
} else if (u.d == v.d) {
u.x = u.x + v.x;
}
}
int n;
vector<int> e[N];
int p[N];
node f[N], g[N];
I64 best = 0, total = 1;
void DFS1(int u) {
if (p[u] >= 0) {
e[u].erase(find(e[u].begin(), e[u].end(), p[u]));
}
f[u] = {0, 1};
for (auto v : e[u]) {
p[v] = u;
DFS1(v);
merge(f[u], {f[v].d + 1, f[v].x});
}
}
void DFS2(int u) {
int c = (int) e[u].size();
vector<node> a;
if (p[u] >= 0 || c == 1) {
a.push_back(g[u]);
}
for (auto v : e[u]) {
a.push_back({f[v].d + 1, f[v].x});
}
sort(a.begin(), a.end());
if ((int) a.size() >= 3) {
int d[3] = {a[0].d, a[1].d, a[2].d};
I64 x[3] = {a[0].x, a[1].x, a[2].x};
I64 t = 0;
I64 current = (I64) d[0] * (d[1] + d[2]), number = 0;
for (int i = 0; i < (int) a.size(); i++) {
if (a[i].d == d[2]) {
t = t + a[i].x;
}
}
if (d[0] > d[1] && d[1] > d[2]) {
number = x[1] * t;
}
if (d[0] > d[1] && d[1] == d[2]) {
number = t * t;
for (int i = 0; i < (int) a.size(); i++) {
if (a[i].d == d[2]) {
number = number - a[i].x * a[i].x;
}
}
number = number / 2;
}
if (d[0] == d[1] && d[1] > d[2]) {
number = (x[0] + x[1]) * t;
}
if (d[0] == d[1] && d[1] == d[2]) {
number = t * t;
for (int i = 0; i < (int) a.size(); i++) {
if (a[i].d == d[2]) {
number = number - a[i].x * a[i].x;
}
}
number = number / 2;
}
if (current > best) {
best = current;
total = number;
} else if (current == best) {
total = total + number;
}
}
vector<node> prefix(c), suffix(c);
for (int i = 0; i < c; i++) {
int v = e[u][i];
prefix[i] = {f[v].d + 1, f[v].x};
suffix[i] = {f[v].d + 1, f[v].x};
}
for (int i = 1; i < c; i++) {
merge(prefix[i], prefix[i - 1]);
}
for (int i = c - 2; i >= 0; i--) {
merge(suffix[i], suffix[i + 1]);
}
for (int i = 0; i < c; i++) {
int v = e[u][i];
node t = g[u];
if (i > 0) {
merge(t, prefix[i - 1]);
}
if (i < c - 1) {
merge(t, suffix[i + 1]);
}
g[v] = {t.d + 1, t.x};
DFS2(v);
}
}
void solve() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u = u - 1;
v = v - 1;
e[u].push_back(v);
e[v].push_back(u);
}
p[0] = 0 - 1;
DFS1(0);
g[0] = {0, 1};
DFS2(0);
cout << best << " " << total << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
17232 KB |
Output is correct |
2 |
Correct |
4 ms |
17320 KB |
Output is correct |
3 |
Correct |
4 ms |
17400 KB |
Output is correct |
4 |
Correct |
4 ms |
17100 KB |
Output is correct |
5 |
Correct |
4 ms |
17232 KB |
Output is correct |
6 |
Correct |
3 ms |
17232 KB |
Output is correct |
7 |
Correct |
3 ms |
17232 KB |
Output is correct |
8 |
Correct |
3 ms |
17232 KB |
Output is correct |
9 |
Correct |
3 ms |
17320 KB |
Output is correct |
10 |
Correct |
4 ms |
17296 KB |
Output is correct |
11 |
Correct |
5 ms |
17332 KB |
Output is correct |
12 |
Correct |
3 ms |
17232 KB |
Output is correct |
13 |
Correct |
4 ms |
17232 KB |
Output is correct |
14 |
Correct |
3 ms |
17432 KB |
Output is correct |
15 |
Correct |
3 ms |
17232 KB |
Output is correct |
16 |
Correct |
3 ms |
17232 KB |
Output is correct |
17 |
Correct |
3 ms |
17488 KB |
Output is correct |
18 |
Correct |
3 ms |
17232 KB |
Output is correct |
19 |
Correct |
4 ms |
17244 KB |
Output is correct |
20 |
Correct |
3 ms |
17404 KB |
Output is correct |
21 |
Correct |
3 ms |
17232 KB |
Output is correct |
22 |
Correct |
4 ms |
17232 KB |
Output is correct |
23 |
Correct |
3 ms |
17232 KB |
Output is correct |
24 |
Correct |
4 ms |
17232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
17232 KB |
Output is correct |
2 |
Correct |
4 ms |
17320 KB |
Output is correct |
3 |
Correct |
4 ms |
17400 KB |
Output is correct |
4 |
Correct |
4 ms |
17100 KB |
Output is correct |
5 |
Correct |
4 ms |
17232 KB |
Output is correct |
6 |
Correct |
3 ms |
17232 KB |
Output is correct |
7 |
Correct |
3 ms |
17232 KB |
Output is correct |
8 |
Correct |
3 ms |
17232 KB |
Output is correct |
9 |
Correct |
3 ms |
17320 KB |
Output is correct |
10 |
Correct |
4 ms |
17296 KB |
Output is correct |
11 |
Correct |
5 ms |
17332 KB |
Output is correct |
12 |
Correct |
3 ms |
17232 KB |
Output is correct |
13 |
Correct |
4 ms |
17232 KB |
Output is correct |
14 |
Correct |
3 ms |
17432 KB |
Output is correct |
15 |
Correct |
3 ms |
17232 KB |
Output is correct |
16 |
Correct |
3 ms |
17232 KB |
Output is correct |
17 |
Correct |
3 ms |
17488 KB |
Output is correct |
18 |
Correct |
3 ms |
17232 KB |
Output is correct |
19 |
Correct |
4 ms |
17244 KB |
Output is correct |
20 |
Correct |
3 ms |
17404 KB |
Output is correct |
21 |
Correct |
3 ms |
17232 KB |
Output is correct |
22 |
Correct |
4 ms |
17232 KB |
Output is correct |
23 |
Correct |
3 ms |
17232 KB |
Output is correct |
24 |
Correct |
4 ms |
17232 KB |
Output is correct |
25 |
Correct |
6 ms |
18000 KB |
Output is correct |
26 |
Correct |
6 ms |
18000 KB |
Output is correct |
27 |
Correct |
6 ms |
18256 KB |
Output is correct |
28 |
Correct |
6 ms |
18120 KB |
Output is correct |
29 |
Correct |
5 ms |
18256 KB |
Output is correct |
30 |
Correct |
6 ms |
18256 KB |
Output is correct |
31 |
Correct |
5 ms |
18256 KB |
Output is correct |
32 |
Correct |
6 ms |
18000 KB |
Output is correct |
33 |
Correct |
6 ms |
18000 KB |
Output is correct |
34 |
Correct |
6 ms |
18256 KB |
Output is correct |
35 |
Correct |
6 ms |
18256 KB |
Output is correct |
36 |
Correct |
6 ms |
18172 KB |
Output is correct |
37 |
Correct |
7 ms |
18256 KB |
Output is correct |
38 |
Correct |
7 ms |
18932 KB |
Output is correct |
39 |
Correct |
5 ms |
18072 KB |
Output is correct |
40 |
Correct |
5 ms |
17744 KB |
Output is correct |
41 |
Correct |
5 ms |
17488 KB |
Output is correct |
42 |
Correct |
6 ms |
17488 KB |
Output is correct |
43 |
Correct |
5 ms |
17488 KB |
Output is correct |
44 |
Correct |
6 ms |
17488 KB |
Output is correct |
45 |
Correct |
5 ms |
17488 KB |
Output is correct |
46 |
Correct |
5 ms |
17488 KB |
Output is correct |
47 |
Correct |
7 ms |
17592 KB |
Output is correct |
48 |
Correct |
7 ms |
17744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
17232 KB |
Output is correct |
2 |
Correct |
4 ms |
17320 KB |
Output is correct |
3 |
Correct |
4 ms |
17400 KB |
Output is correct |
4 |
Correct |
4 ms |
17100 KB |
Output is correct |
5 |
Correct |
4 ms |
17232 KB |
Output is correct |
6 |
Correct |
3 ms |
17232 KB |
Output is correct |
7 |
Correct |
3 ms |
17232 KB |
Output is correct |
8 |
Correct |
3 ms |
17232 KB |
Output is correct |
9 |
Correct |
3 ms |
17320 KB |
Output is correct |
10 |
Correct |
4 ms |
17296 KB |
Output is correct |
11 |
Correct |
5 ms |
17332 KB |
Output is correct |
12 |
Correct |
3 ms |
17232 KB |
Output is correct |
13 |
Correct |
4 ms |
17232 KB |
Output is correct |
14 |
Correct |
3 ms |
17432 KB |
Output is correct |
15 |
Correct |
3 ms |
17232 KB |
Output is correct |
16 |
Correct |
3 ms |
17232 KB |
Output is correct |
17 |
Correct |
3 ms |
17488 KB |
Output is correct |
18 |
Correct |
3 ms |
17232 KB |
Output is correct |
19 |
Correct |
4 ms |
17244 KB |
Output is correct |
20 |
Correct |
3 ms |
17404 KB |
Output is correct |
21 |
Correct |
3 ms |
17232 KB |
Output is correct |
22 |
Correct |
4 ms |
17232 KB |
Output is correct |
23 |
Correct |
3 ms |
17232 KB |
Output is correct |
24 |
Correct |
4 ms |
17232 KB |
Output is correct |
25 |
Correct |
6 ms |
18000 KB |
Output is correct |
26 |
Correct |
6 ms |
18000 KB |
Output is correct |
27 |
Correct |
6 ms |
18256 KB |
Output is correct |
28 |
Correct |
6 ms |
18120 KB |
Output is correct |
29 |
Correct |
5 ms |
18256 KB |
Output is correct |
30 |
Correct |
6 ms |
18256 KB |
Output is correct |
31 |
Correct |
5 ms |
18256 KB |
Output is correct |
32 |
Correct |
6 ms |
18000 KB |
Output is correct |
33 |
Correct |
6 ms |
18000 KB |
Output is correct |
34 |
Correct |
6 ms |
18256 KB |
Output is correct |
35 |
Correct |
6 ms |
18256 KB |
Output is correct |
36 |
Correct |
6 ms |
18172 KB |
Output is correct |
37 |
Correct |
7 ms |
18256 KB |
Output is correct |
38 |
Correct |
7 ms |
18932 KB |
Output is correct |
39 |
Correct |
5 ms |
18072 KB |
Output is correct |
40 |
Correct |
5 ms |
17744 KB |
Output is correct |
41 |
Correct |
5 ms |
17488 KB |
Output is correct |
42 |
Correct |
6 ms |
17488 KB |
Output is correct |
43 |
Correct |
5 ms |
17488 KB |
Output is correct |
44 |
Correct |
6 ms |
17488 KB |
Output is correct |
45 |
Correct |
5 ms |
17488 KB |
Output is correct |
46 |
Correct |
5 ms |
17488 KB |
Output is correct |
47 |
Correct |
7 ms |
17592 KB |
Output is correct |
48 |
Correct |
7 ms |
17744 KB |
Output is correct |
49 |
Correct |
403 ms |
105088 KB |
Output is correct |
50 |
Correct |
435 ms |
114760 KB |
Output is correct |
51 |
Correct |
458 ms |
123216 KB |
Output is correct |
52 |
Correct |
422 ms |
93256 KB |
Output is correct |
53 |
Correct |
322 ms |
122440 KB |
Output is correct |
54 |
Correct |
333 ms |
133704 KB |
Output is correct |
55 |
Correct |
296 ms |
106056 KB |
Output is correct |
56 |
Correct |
304 ms |
118100 KB |
Output is correct |
57 |
Correct |
396 ms |
129356 KB |
Output is correct |
58 |
Correct |
447 ms |
118456 KB |
Output is correct |
59 |
Correct |
407 ms |
118064 KB |
Output is correct |
60 |
Correct |
411 ms |
112968 KB |
Output is correct |
61 |
Correct |
667 ms |
192656 KB |
Output is correct |
62 |
Correct |
602 ms |
167240 KB |
Output is correct |
63 |
Correct |
653 ms |
93000 KB |
Output is correct |
64 |
Correct |
578 ms |
75256 KB |
Output is correct |
65 |
Correct |
548 ms |
63860 KB |
Output is correct |
66 |
Correct |
559 ms |
58008 KB |
Output is correct |
67 |
Correct |
566 ms |
54600 KB |
Output is correct |
68 |
Correct |
553 ms |
53320 KB |
Output is correct |
69 |
Correct |
521 ms |
53028 KB |
Output is correct |
70 |
Correct |
564 ms |
52552 KB |
Output is correct |
71 |
Correct |
553 ms |
52672 KB |
Output is correct |
72 |
Correct |
570 ms |
52552 KB |
Output is correct |
73 |
Correct |
604 ms |
53196 KB |
Output is correct |
74 |
Correct |
507 ms |
53632 KB |
Output is correct |
75 |
Correct |
479 ms |
55372 KB |
Output is correct |
76 |
Correct |
508 ms |
58072 KB |
Output is correct |
77 |
Correct |
393 ms |
65336 KB |
Output is correct |
78 |
Correct |
261 ms |
78640 KB |
Output is correct |