#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
const int maxN = 1e6 + 10;
pii d[maxN];
long long ans;
long long ways = 1;
int n;
vector<int> v[maxN];
void dfs1(int x, int pred) {
d[x] = {0, 1};
for (int i: v[x]) {
if (i != pred) {
dfs1(i, x);
if (d[i].first + 1 > d[x].first) {
d[x] = {d[i].first + 1, d[i].second};
} else {
if (d[i].first + 1 == d[x].first) {
d[x].second+=d[i].second;
}
}
}
}
}
void dfs2(int x, int pred, pii bestUp) {
vector<pii> choices;
if (x != 1) {
choices.pb(bestUp);
}
for (int i: v[x]) {
if (i != pred) {
choices.pb(d[i]);
}
}
sort(choices.begin(), choices.end());
reverse(choices.begin(), choices.end());
if (choices.size() > 2) {
long long maxProduct = 1ll * (choices[0].first + 1) * (choices[1].first + choices[2].first + 2);
if (maxProduct >= ans) {
long long pathCount = 0;
if (choices[1].first == choices[2].first) {
long long total = 0;
long long removeSum = 0;
for (auto i: choices) {
if (choices[1].first == i.first) {
total+=i.second;
removeSum+= 1ll * i.second * i.second;
}
}
pathCount = total * total - removeSum;
pathCount/=2;
} else {
if (choices[0].first == choices[1].first) {
long long total = 0;
for (auto i: choices) {
if (choices[2].first == i.first) {
total+=i.second;
}
}
pathCount = total * (choices[0].second + choices[1].second);
} else {
long long total = 0;
for (auto i: choices) {
if (choices[2].first == i.first) {
total+=i.second;
}
}
pathCount = total * choices[1].second;
}
}
if (maxProduct == ans) ways+=pathCount; else {
ans = maxProduct;
ways = pathCount;
}
}
}
// leaf
if (v[x].size() == 1 && pred) {
return;
}
int len = choices[0].first;
int nacini = 0;
int drugiNacini = 1;
int drugiDuzina = -1;
if (choices.size() > 1) {
drugiDuzina = choices[1].first;
drugiNacini = 0;
}
for (auto i: choices) {
if (i.first == len) nacini+=i.second;
if (choices.size() > 1 && drugiDuzina == i.first) drugiNacini+=i.second;
}
for (int i: v[x]) {
if (i != pred) {
if (d[i].first == len) {
if (nacini - d[i].second != 0) {
dfs2(i, x, {len + 1, nacini - d[i].second});
} else {
dfs2(i, x, {drugiDuzina + 1, drugiNacini});
}
} else {
dfs2(i, x, {len + 1, nacini});
}
}
}
}
int main() {
cin >> n;
for (int i = 1; i<n; i++) {
int x, y;
scanf("%d%d", &x, &y);
v[x].pb(y);
v[y].pb(x);
}
dfs1(1, 0);
dfs2(1, 0, {0, 0});
cout << ans << " " << ways << endl;
return 0;
}
Compilation message
road.cpp: In function 'int main()':
road.cpp:146:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23736 KB |
Output is correct |
4 |
Correct |
12 ms |
23700 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23696 KB |
Output is correct |
10 |
Correct |
12 ms |
23780 KB |
Output is correct |
11 |
Correct |
12 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
13 ms |
23712 KB |
Output is correct |
14 |
Correct |
14 ms |
23780 KB |
Output is correct |
15 |
Correct |
12 ms |
23752 KB |
Output is correct |
16 |
Correct |
12 ms |
23708 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23796 KB |
Output is correct |
19 |
Correct |
13 ms |
23796 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23792 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23796 KB |
Output is correct |
24 |
Correct |
12 ms |
23792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23736 KB |
Output is correct |
4 |
Correct |
12 ms |
23700 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23696 KB |
Output is correct |
10 |
Correct |
12 ms |
23780 KB |
Output is correct |
11 |
Correct |
12 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
13 ms |
23712 KB |
Output is correct |
14 |
Correct |
14 ms |
23780 KB |
Output is correct |
15 |
Correct |
12 ms |
23752 KB |
Output is correct |
16 |
Correct |
12 ms |
23708 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23796 KB |
Output is correct |
19 |
Correct |
13 ms |
23796 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23792 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23796 KB |
Output is correct |
24 |
Correct |
12 ms |
23792 KB |
Output is correct |
25 |
Correct |
15 ms |
24192 KB |
Output is correct |
26 |
Correct |
14 ms |
24276 KB |
Output is correct |
27 |
Correct |
15 ms |
24392 KB |
Output is correct |
28 |
Correct |
16 ms |
24324 KB |
Output is correct |
29 |
Correct |
15 ms |
24316 KB |
Output is correct |
30 |
Correct |
14 ms |
24316 KB |
Output is correct |
31 |
Correct |
16 ms |
24320 KB |
Output is correct |
32 |
Correct |
14 ms |
24276 KB |
Output is correct |
33 |
Correct |
14 ms |
24316 KB |
Output is correct |
34 |
Correct |
14 ms |
24392 KB |
Output is correct |
35 |
Correct |
14 ms |
24364 KB |
Output is correct |
36 |
Correct |
14 ms |
24320 KB |
Output is correct |
37 |
Correct |
15 ms |
24396 KB |
Output is correct |
38 |
Correct |
14 ms |
24696 KB |
Output is correct |
39 |
Correct |
14 ms |
24276 KB |
Output is correct |
40 |
Correct |
14 ms |
24064 KB |
Output is correct |
41 |
Correct |
14 ms |
24056 KB |
Output is correct |
42 |
Correct |
14 ms |
24020 KB |
Output is correct |
43 |
Correct |
18 ms |
24020 KB |
Output is correct |
44 |
Correct |
14 ms |
24020 KB |
Output is correct |
45 |
Correct |
14 ms |
24020 KB |
Output is correct |
46 |
Correct |
14 ms |
23984 KB |
Output is correct |
47 |
Correct |
14 ms |
24004 KB |
Output is correct |
48 |
Correct |
15 ms |
24144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23736 KB |
Output is correct |
4 |
Correct |
12 ms |
23700 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23696 KB |
Output is correct |
10 |
Correct |
12 ms |
23780 KB |
Output is correct |
11 |
Correct |
12 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23764 KB |
Output is correct |
13 |
Correct |
13 ms |
23712 KB |
Output is correct |
14 |
Correct |
14 ms |
23780 KB |
Output is correct |
15 |
Correct |
12 ms |
23752 KB |
Output is correct |
16 |
Correct |
12 ms |
23708 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23796 KB |
Output is correct |
19 |
Correct |
13 ms |
23796 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23792 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23796 KB |
Output is correct |
24 |
Correct |
12 ms |
23792 KB |
Output is correct |
25 |
Correct |
15 ms |
24192 KB |
Output is correct |
26 |
Correct |
14 ms |
24276 KB |
Output is correct |
27 |
Correct |
15 ms |
24392 KB |
Output is correct |
28 |
Correct |
16 ms |
24324 KB |
Output is correct |
29 |
Correct |
15 ms |
24316 KB |
Output is correct |
30 |
Correct |
14 ms |
24316 KB |
Output is correct |
31 |
Correct |
16 ms |
24320 KB |
Output is correct |
32 |
Correct |
14 ms |
24276 KB |
Output is correct |
33 |
Correct |
14 ms |
24316 KB |
Output is correct |
34 |
Correct |
14 ms |
24392 KB |
Output is correct |
35 |
Correct |
14 ms |
24364 KB |
Output is correct |
36 |
Correct |
14 ms |
24320 KB |
Output is correct |
37 |
Correct |
15 ms |
24396 KB |
Output is correct |
38 |
Correct |
14 ms |
24696 KB |
Output is correct |
39 |
Correct |
14 ms |
24276 KB |
Output is correct |
40 |
Correct |
14 ms |
24064 KB |
Output is correct |
41 |
Correct |
14 ms |
24056 KB |
Output is correct |
42 |
Correct |
14 ms |
24020 KB |
Output is correct |
43 |
Correct |
18 ms |
24020 KB |
Output is correct |
44 |
Correct |
14 ms |
24020 KB |
Output is correct |
45 |
Correct |
14 ms |
24020 KB |
Output is correct |
46 |
Correct |
14 ms |
23984 KB |
Output is correct |
47 |
Correct |
14 ms |
24004 KB |
Output is correct |
48 |
Correct |
15 ms |
24144 KB |
Output is correct |
49 |
Correct |
444 ms |
79704 KB |
Output is correct |
50 |
Correct |
459 ms |
85304 KB |
Output is correct |
51 |
Correct |
467 ms |
89900 KB |
Output is correct |
52 |
Correct |
455 ms |
73388 KB |
Output is correct |
53 |
Correct |
405 ms |
85416 KB |
Output is correct |
54 |
Correct |
402 ms |
91064 KB |
Output is correct |
55 |
Correct |
387 ms |
77204 KB |
Output is correct |
56 |
Correct |
386 ms |
83124 KB |
Output is correct |
57 |
Correct |
422 ms |
93196 KB |
Output is correct |
58 |
Correct |
411 ms |
86988 KB |
Output is correct |
59 |
Correct |
429 ms |
86948 KB |
Output is correct |
60 |
Correct |
423 ms |
84176 KB |
Output is correct |
61 |
Correct |
627 ms |
128324 KB |
Output is correct |
62 |
Correct |
642 ms |
114168 KB |
Output is correct |
63 |
Correct |
621 ms |
72992 KB |
Output is correct |
64 |
Correct |
607 ms |
63080 KB |
Output is correct |
65 |
Correct |
636 ms |
56652 KB |
Output is correct |
66 |
Correct |
579 ms |
53380 KB |
Output is correct |
67 |
Correct |
570 ms |
51664 KB |
Output is correct |
68 |
Correct |
577 ms |
51064 KB |
Output is correct |
69 |
Correct |
559 ms |
50736 KB |
Output is correct |
70 |
Correct |
563 ms |
50612 KB |
Output is correct |
71 |
Correct |
575 ms |
50540 KB |
Output is correct |
72 |
Correct |
558 ms |
50464 KB |
Output is correct |
73 |
Correct |
603 ms |
50780 KB |
Output is correct |
74 |
Correct |
581 ms |
50952 KB |
Output is correct |
75 |
Correct |
552 ms |
51276 KB |
Output is correct |
76 |
Correct |
525 ms |
51944 KB |
Output is correct |
77 |
Correct |
463 ms |
53440 KB |
Output is correct |
78 |
Correct |
301 ms |
56712 KB |
Output is correct |