#include "bits/stdc++.h"
#define int long long
using namespace std;
void file(string name = ""){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(name != ""){
freopen((name + ".inp").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
void _max(pair<int, int> &a, pair<int, int> b){
if(a.first < b.first){
a = b;
} else
if(a.first == b.first){
a.second += b.second;
}
}
const int mxN = 5e5 + 10;
int n;
vector<vector<int>> adj(mxN);
pair<int, int> d[mxN], f[mxN], pref[mxN], suff[mxN];
int max_hardness = 0, count_path = 1;
void dfs(int u, int p){
d[u] = {0, 1};
for(int v : adj[u]){
if(v == p) continue ;
dfs(v, u);
_max(d[u], make_pair(d[v].first + 1, d[v].second));
}
}
void _dfs(int u, int p){
vector<int> b;
vector<pair<int, int>> a;
if(adj[u].size() == 1 || u > 1){
a.push_back({f[u].first + 1, f[u].second});
}
for(int v : adj[u]){
if(v == p) continue ;
a.push_back({d[v].first + 1, d[v].second});
}
sort(a.begin(), a.end(), greater<pair<int, int>> ());
if(a.size() >= 3 && a[0].first * (a[1].first + a[2].first) >= max_hardness){
int curr_hardness = a[0].first * (a[1].first + a[2].first);
int curr_path = 0;
int cnt = 0;
for(auto [x, y] : a){
if(x == a[2].first){
cnt += y;
}
}
if(a[1].first == a[2].first){
curr_path = cnt * cnt;
for(auto [x, y] : a){
if(x == a[2].first){
curr_path -= y * y;
}
}
curr_path /= 2;
} else
if(a[0].first == a[1].first){
curr_path = (a[0].second + a[1].second) * cnt;
} else {
curr_path = a[1].second * cnt;
}
if(curr_hardness > max_hardness){
max_hardness = curr_hardness;
count_path = curr_path;
} else {
count_path += curr_path;
}
}
a.clear();
for(int v : adj[u]){
if(v == p) continue ;
a.push_back({d[v].first + 1, d[v].second});
b.push_back(v);
}
pref[0] = {0, 0};
for(int i = 1; i <= a.size(); i ++){
pref[i] = pref[i - 1];
_max(pref[i], a[i - 1]);
}
suff[a.size() + 1] = {0, 0};
for(int i = a.size(); i >= 1; i --){
suff[i] = suff[i + 1];
_max(suff[i], a[i - 1]);
}
for(int i = 1; i <= a.size(); i ++){
int v = b[i - 1];
_max(f[v], pref[i - 1]);
_max(f[v], suff[i + 1]);
if(adj[u].size() == 1 || u > 1) _max(f[v], make_pair(f[u].first + 1, f[u].second));
}
for(int i = 1; i <= a.size(); i ++){
int v = b[i - 1];
_dfs(v, u);
}
}
signed main(){
file("");
cin >> n;
for(int i = 1; i <= n - 1; i ++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1);
f[1] = {-1, 1};
_dfs(1, 1);
cout << max_hardness << " " << count_path << "\n";
return 0;
}
Compilation message
road.cpp: In function 'void _dfs(long long int, long long int)':
road.cpp:61:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | for(auto [x, y] : a){
| ^
road.cpp:69:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
69 | for(auto [x, y] : a){
| ^
road.cpp:99:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for(int i = 1; i <= a.size(); i ++){
| ~~^~~~~~~~~~~
road.cpp:110:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for(int i = 1; i <= a.size(); i ++){
| ~~^~~~~~~~~~~
road.cpp:117:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for(int i = 1; i <= a.size(); i ++){
| ~~^~~~~~~~~~~
road.cpp: In function 'void file(std::string)':
road.cpp:9:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | freopen((name + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
road.cpp:10:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
12020 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12104 KB |
Output is correct |
8 |
Correct |
6 ms |
12084 KB |
Output is correct |
9 |
Correct |
6 ms |
12084 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12044 KB |
Output is correct |
14 |
Correct |
6 ms |
12116 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Correct |
6 ms |
12116 KB |
Output is correct |
17 |
Correct |
7 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12008 KB |
Output is correct |
19 |
Correct |
6 ms |
12116 KB |
Output is correct |
20 |
Correct |
6 ms |
12116 KB |
Output is correct |
21 |
Correct |
6 ms |
12028 KB |
Output is correct |
22 |
Correct |
6 ms |
12064 KB |
Output is correct |
23 |
Correct |
7 ms |
12116 KB |
Output is correct |
24 |
Correct |
7 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
12020 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12104 KB |
Output is correct |
8 |
Correct |
6 ms |
12084 KB |
Output is correct |
9 |
Correct |
6 ms |
12084 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12044 KB |
Output is correct |
14 |
Correct |
6 ms |
12116 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Correct |
6 ms |
12116 KB |
Output is correct |
17 |
Correct |
7 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12008 KB |
Output is correct |
19 |
Correct |
6 ms |
12116 KB |
Output is correct |
20 |
Correct |
6 ms |
12116 KB |
Output is correct |
21 |
Correct |
6 ms |
12028 KB |
Output is correct |
22 |
Correct |
6 ms |
12064 KB |
Output is correct |
23 |
Correct |
7 ms |
12116 KB |
Output is correct |
24 |
Correct |
7 ms |
12116 KB |
Output is correct |
25 |
Correct |
8 ms |
12736 KB |
Output is correct |
26 |
Correct |
9 ms |
13024 KB |
Output is correct |
27 |
Correct |
10 ms |
12992 KB |
Output is correct |
28 |
Correct |
8 ms |
13100 KB |
Output is correct |
29 |
Correct |
8 ms |
13012 KB |
Output is correct |
30 |
Correct |
8 ms |
13112 KB |
Output is correct |
31 |
Correct |
9 ms |
12984 KB |
Output is correct |
32 |
Correct |
8 ms |
12932 KB |
Output is correct |
33 |
Correct |
8 ms |
12884 KB |
Output is correct |
34 |
Correct |
9 ms |
13036 KB |
Output is correct |
35 |
Correct |
9 ms |
13060 KB |
Output is correct |
36 |
Correct |
9 ms |
12992 KB |
Output is correct |
37 |
Correct |
11 ms |
13140 KB |
Output is correct |
38 |
Correct |
9 ms |
13644 KB |
Output is correct |
39 |
Correct |
8 ms |
12860 KB |
Output is correct |
40 |
Correct |
10 ms |
12608 KB |
Output is correct |
41 |
Correct |
8 ms |
12552 KB |
Output is correct |
42 |
Correct |
8 ms |
12500 KB |
Output is correct |
43 |
Correct |
8 ms |
12372 KB |
Output is correct |
44 |
Correct |
8 ms |
12432 KB |
Output is correct |
45 |
Correct |
8 ms |
12472 KB |
Output is correct |
46 |
Correct |
7 ms |
12372 KB |
Output is correct |
47 |
Correct |
8 ms |
12628 KB |
Output is correct |
48 |
Correct |
8 ms |
12756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
12020 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
6 ms |
12104 KB |
Output is correct |
8 |
Correct |
6 ms |
12084 KB |
Output is correct |
9 |
Correct |
6 ms |
12084 KB |
Output is correct |
10 |
Correct |
6 ms |
12116 KB |
Output is correct |
11 |
Correct |
6 ms |
12116 KB |
Output is correct |
12 |
Correct |
6 ms |
12116 KB |
Output is correct |
13 |
Correct |
6 ms |
12044 KB |
Output is correct |
14 |
Correct |
6 ms |
12116 KB |
Output is correct |
15 |
Correct |
6 ms |
12116 KB |
Output is correct |
16 |
Correct |
6 ms |
12116 KB |
Output is correct |
17 |
Correct |
7 ms |
12116 KB |
Output is correct |
18 |
Correct |
6 ms |
12008 KB |
Output is correct |
19 |
Correct |
6 ms |
12116 KB |
Output is correct |
20 |
Correct |
6 ms |
12116 KB |
Output is correct |
21 |
Correct |
6 ms |
12028 KB |
Output is correct |
22 |
Correct |
6 ms |
12064 KB |
Output is correct |
23 |
Correct |
7 ms |
12116 KB |
Output is correct |
24 |
Correct |
7 ms |
12116 KB |
Output is correct |
25 |
Correct |
8 ms |
12736 KB |
Output is correct |
26 |
Correct |
9 ms |
13024 KB |
Output is correct |
27 |
Correct |
10 ms |
12992 KB |
Output is correct |
28 |
Correct |
8 ms |
13100 KB |
Output is correct |
29 |
Correct |
8 ms |
13012 KB |
Output is correct |
30 |
Correct |
8 ms |
13112 KB |
Output is correct |
31 |
Correct |
9 ms |
12984 KB |
Output is correct |
32 |
Correct |
8 ms |
12932 KB |
Output is correct |
33 |
Correct |
8 ms |
12884 KB |
Output is correct |
34 |
Correct |
9 ms |
13036 KB |
Output is correct |
35 |
Correct |
9 ms |
13060 KB |
Output is correct |
36 |
Correct |
9 ms |
12992 KB |
Output is correct |
37 |
Correct |
11 ms |
13140 KB |
Output is correct |
38 |
Correct |
9 ms |
13644 KB |
Output is correct |
39 |
Correct |
8 ms |
12860 KB |
Output is correct |
40 |
Correct |
10 ms |
12608 KB |
Output is correct |
41 |
Correct |
8 ms |
12552 KB |
Output is correct |
42 |
Correct |
8 ms |
12500 KB |
Output is correct |
43 |
Correct |
8 ms |
12372 KB |
Output is correct |
44 |
Correct |
8 ms |
12432 KB |
Output is correct |
45 |
Correct |
8 ms |
12472 KB |
Output is correct |
46 |
Correct |
7 ms |
12372 KB |
Output is correct |
47 |
Correct |
8 ms |
12628 KB |
Output is correct |
48 |
Correct |
8 ms |
12756 KB |
Output is correct |
49 |
Correct |
507 ms |
98376 KB |
Output is correct |
50 |
Correct |
440 ms |
107136 KB |
Output is correct |
51 |
Correct |
469 ms |
114628 KB |
Output is correct |
52 |
Correct |
439 ms |
88012 KB |
Output is correct |
53 |
Correct |
425 ms |
111600 KB |
Output is correct |
54 |
Correct |
429 ms |
120796 KB |
Output is correct |
55 |
Correct |
400 ms |
98124 KB |
Output is correct |
56 |
Correct |
424 ms |
108020 KB |
Output is correct |
57 |
Correct |
427 ms |
118824 KB |
Output is correct |
58 |
Correct |
425 ms |
108932 KB |
Output is correct |
59 |
Correct |
470 ms |
108784 KB |
Output is correct |
60 |
Correct |
427 ms |
104268 KB |
Output is correct |
61 |
Correct |
650 ms |
175072 KB |
Output is correct |
62 |
Correct |
649 ms |
152368 KB |
Output is correct |
63 |
Correct |
665 ms |
86392 KB |
Output is correct |
64 |
Correct |
628 ms |
70608 KB |
Output is correct |
65 |
Correct |
589 ms |
60488 KB |
Output is correct |
66 |
Correct |
542 ms |
55276 KB |
Output is correct |
67 |
Correct |
543 ms |
52324 KB |
Output is correct |
68 |
Correct |
555 ms |
51276 KB |
Output is correct |
69 |
Correct |
584 ms |
50860 KB |
Output is correct |
70 |
Correct |
557 ms |
50600 KB |
Output is correct |
71 |
Correct |
543 ms |
50488 KB |
Output is correct |
72 |
Correct |
560 ms |
50796 KB |
Output is correct |
73 |
Correct |
527 ms |
51216 KB |
Output is correct |
74 |
Correct |
540 ms |
51928 KB |
Output is correct |
75 |
Correct |
513 ms |
54072 KB |
Output is correct |
76 |
Correct |
496 ms |
57664 KB |
Output is correct |
77 |
Correct |
437 ms |
67992 KB |
Output is correct |
78 |
Correct |
298 ms |
85908 KB |
Output is correct |