#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pi;
int N;
vector<vector<int>> adj_list;
vector<ll> d;
vector<ll> c;
pi ans;
void calc_height(int node, int par)
{
d[node] = 0, c[node] = 1;
for (auto neigh : adj_list[node]) if (neigh != par){
calc_height(neigh, node);
if (d[neigh] + 1 > d[node]) {
d[node] = d[neigh] + 1;
c[node] = c[neigh];
}
else if (d[neigh] + 1 == d[node]) {
c[node] += c[neigh];
}
}
}
void calculate_hardness(int node, int par, int outside_distance, int outside_count)
{
vector<pi> paths;
if (par != -1 || adj_list[node].size() == 1)
paths.pb(mp(outside_distance, outside_count));
for (auto neigh : adj_list[node]) if (neigh != par)
paths.pb(mp(d[neigh] + 1, c[neigh]));
sort(paths.begin(), paths.end(), [&] (pi&a, pi& b) {return a > b;});
if (paths.size() >= 3 && paths[0].f * (paths[1].f + paths[2].f) >= ans.f) {
// Calculate maximum distance
// Paths that are available are : c[1] * c[2] (if all dist are distinct)
ll curdist = paths[0].f * (paths[1].f + paths[2].f);
ll third_chances = 0;
ll total_chances = 0;
for (auto p : paths)
if (p.f == paths[2].f)
third_chances += p.s;
if (paths[1].f == paths[2].f) {
// Many pairing possibilities
total_chances = third_chances * third_chances;
for (auto p : paths)
if (p.f == paths[2].f) {
// Remove the chances going into the same path twice
total_chances -= (p.s) * (p.s);
}
// We have doublecounted each path(front to back and back to front)
// It is the same path, therefore half the result
total_chances /= 2;
}
else if (paths[0].f == paths[1].f) {
// We can use both first and second paths in our chance calculation
total_chances = third_chances * (paths[0].s + paths[1].s);
}
else {
// All distinct
total_chances = third_chances * paths[1].s;
}
if (ans.f < curdist) {
ans = mp(curdist, total_chances);
}
else {
ans.s += total_chances;
}
}
// Calculate the best path to give to children
ll d1 = paths[0].f + 1;
ll d2 = 0;
ll c2 = 1;
ll c1 = 0;
if (paths.size() >= 2) {
d2 = paths[1].f + 1;
c2 = 0;
for (auto p : paths) {
if (p.f + 1 == d1)
c1 += p.s;
if (p.f + 1 == d2)
c2 += p.s;
}
}
paths.clear();
for (auto neigh : adj_list[node]) {
if (neigh != par) {
if (d[neigh] + 2 == d1 && c[neigh] == c1)
calculate_hardness(neigh, node, d2, c2);
else if (d[neigh] + 2 == d1)
calculate_hardness(neigh, node, d1, c1 - c[neigh]);
else
calculate_hardness(neigh, node, d1, c1);
}
}
}
int main(void)
{
scanf("%d", &N);
adj_list.resize(N);
d.resize(N);
c.resize(N);
ans = mp(0, 1);
for (int i = 0; i < N - 1; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--; b--;
adj_list[a].pb(b);
adj_list[b].pb(a);
}
calc_height(0, -1);
calculate_hardness(0, -1, 0, 1);
printf("%lld %lld\n", ans.f, ans.s);
return 0;
}
Compilation message
road.cpp: In function 'int main()':
road.cpp:119:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
road.cpp:128:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
276 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
276 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
952 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1076 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
980 KB |
Output is correct |
33 |
Correct |
2 ms |
980 KB |
Output is correct |
34 |
Correct |
2 ms |
1108 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
3 ms |
1108 KB |
Output is correct |
37 |
Correct |
2 ms |
1244 KB |
Output is correct |
38 |
Correct |
3 ms |
1508 KB |
Output is correct |
39 |
Correct |
2 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
828 KB |
Output is correct |
41 |
Correct |
3 ms |
692 KB |
Output is correct |
42 |
Correct |
2 ms |
696 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
2 ms |
596 KB |
Output is correct |
46 |
Correct |
3 ms |
568 KB |
Output is correct |
47 |
Correct |
2 ms |
724 KB |
Output is correct |
48 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
276 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
952 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1076 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
3 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
980 KB |
Output is correct |
33 |
Correct |
2 ms |
980 KB |
Output is correct |
34 |
Correct |
2 ms |
1108 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
3 ms |
1108 KB |
Output is correct |
37 |
Correct |
2 ms |
1244 KB |
Output is correct |
38 |
Correct |
3 ms |
1508 KB |
Output is correct |
39 |
Correct |
2 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
828 KB |
Output is correct |
41 |
Correct |
3 ms |
692 KB |
Output is correct |
42 |
Correct |
2 ms |
696 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
2 ms |
596 KB |
Output is correct |
46 |
Correct |
3 ms |
568 KB |
Output is correct |
47 |
Correct |
2 ms |
724 KB |
Output is correct |
48 |
Correct |
2 ms |
852 KB |
Output is correct |
49 |
Correct |
443 ms |
77688 KB |
Output is correct |
50 |
Correct |
458 ms |
84264 KB |
Output is correct |
51 |
Correct |
459 ms |
89772 KB |
Output is correct |
52 |
Correct |
440 ms |
69904 KB |
Output is correct |
53 |
Correct |
360 ms |
87060 KB |
Output is correct |
54 |
Correct |
381 ms |
94248 KB |
Output is correct |
55 |
Correct |
357 ms |
76620 KB |
Output is correct |
56 |
Correct |
390 ms |
84328 KB |
Output is correct |
57 |
Correct |
433 ms |
93924 KB |
Output is correct |
58 |
Correct |
398 ms |
86476 KB |
Output is correct |
59 |
Correct |
391 ms |
86348 KB |
Output is correct |
60 |
Correct |
399 ms |
82828 KB |
Output is correct |
61 |
Correct |
665 ms |
136072 KB |
Output is correct |
62 |
Correct |
663 ms |
119004 KB |
Output is correct |
63 |
Correct |
603 ms |
69496 KB |
Output is correct |
64 |
Correct |
617 ms |
57680 KB |
Output is correct |
65 |
Correct |
613 ms |
50280 KB |
Output is correct |
66 |
Correct |
543 ms |
46156 KB |
Output is correct |
67 |
Correct |
554 ms |
44088 KB |
Output is correct |
68 |
Correct |
549 ms |
43212 KB |
Output is correct |
69 |
Correct |
549 ms |
42844 KB |
Output is correct |
70 |
Correct |
618 ms |
42584 KB |
Output is correct |
71 |
Correct |
577 ms |
42484 KB |
Output is correct |
72 |
Correct |
547 ms |
42796 KB |
Output is correct |
73 |
Correct |
580 ms |
43052 KB |
Output is correct |
74 |
Correct |
553 ms |
43368 KB |
Output is correct |
75 |
Correct |
521 ms |
43892 KB |
Output is correct |
76 |
Correct |
493 ms |
45136 KB |
Output is correct |
77 |
Correct |
436 ms |
47636 KB |
Output is correct |
78 |
Correct |
297 ms |
52976 KB |
Output is correct |