#include<bits/stdc++.h>
#define F first
#define S second
#define int long long
using namespace std;
typedef pair<int , int> ipair;
const int N = 5e5 + 5;
int n;
vector<int> adj[N];
vector<ipair> D[N];
ipair dpD[N] , dpU[N];
int mx , cnt;
void dfsD(int u = 1 , int p = 0){
for(int v : adj[u]){
if(v==p)continue;
dfsD(v , u);
if(dpD[v].F + 1 > dpD[u].F) dpD[u] = dpD[v] , dpD[u].F++;
else if(dpD[v].F + 1 == dpD[u].F) dpD[u].S += dpD[v].S;
}
if(adj[u].size() == 1 && u != 1) dpD[u].S = 1;
for(int v : adj[u]) if(v != p)D[u].emplace_back(dpD[v].F + 1, dpD[v].S);
}
void dfsU(int u = 1, int p = 0){
pair<int , int > d1 , d2;
d1.F = d1.S = d2.F= d2.S = 0;
d1 = dpD[u];
///d1.F--;
//cout << d1.F << " " << d1.S << " kaaa " << endl;
for(int v : adj[u]){
if(v==p || d1.F == dpD[v].F + 1)continue;
if(d2.F < dpD[v].F + 1)d2 = dpD[v] , d2.F++;
else if(d2.F == dpD[v].F + 1) d2.S += dpD[v].S;
}
//cout << d2.F << " " << d2.S << " uaaaa "<< endl;
if(u == 1) dpU[u].S = 1;
for(int v : adj[u]){
if(v==p)continue;
if(d1.F == dpD[v].F + 1 && d1.S == dpD[v].S) dpU[v] = d2;
else if(d1.F == dpD[v].F + 1 && d1.S > dpD[v].S) dpU[v].F = d1.F , dpU[v].S = d1.S - dpD[v].S;
else dpU[v] = d1;
if(dpU[v].F < dpU[u].F) dpU[v] = dpU[u];
else if(dpU[v].F == dpU[u].F) dpU[v].S += dpU[u].S;
dpU[v].F++;
dfsU(v , u);
}
}
void solve(){
//cout << "HOO" << endl;
for(int u = 1; u <= n; ++u){
if(D[u].size() < 3) continue;
sort(D[u].rbegin() , D[u].rend());
int x1 = D[u][0].F , x2 = D[u][1].F , x3 = D[u][2].F;
//cout << x1 << " " << x2 << " " << x3 << endl;
mx = max(mx , x1 * (x2 + x3));
}
//cout << mx << " mx "<<endl;
for(int u = 1; u <= n; ++u){
if(D[u].size() < 3)continue;
int x1 = D[u][0].F , x2 = D[u][1].F , x3 = D[u][2].F;
if(mx != x1 * (x2 + x3)) continue;
///
int Y = 0;
if(x1 == x3){
for(int x = 0; x < D[u].size(); ++x) if(D[u][x].F ==x1)Y += D[u][x].S;
for(int x = 0; x < D[u].size(); ++x){
if(D[u][x].F == x1) cnt += (Y - D[u][x].S) * D[u][x].S , Y -= D[u][x].S;
}
continue;
}
///
if(x1 == x2){
for(int x = 0; x < D[u].size(); ++x) if(D[u][x].F == x3)Y += D[u][x].S;
cnt += (D[u][0].S + D[u][1].S) * Y;
continue;
}
if(x2 == x3){
for(int x = 0; x< D[u].size(); ++x)if(D[u][x].F == x2) Y += D[u][x].S;
for(int x= 0; x < D[u].size(); ++x){
if(D[u][x].F == x2) cnt += D[u][x].S * (Y - D[u][x].S), Y-= D[u][x].S;
}
continue;
}
for(int x = 0;x < D[u].size(); ++x)if(D[u][x].F == x3)Y+= D[u][x].S;
cnt += D[u][1].S * Y;
}
}
main (){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 1; i < n; ++i){
int u , v; cin >> u >> v;
adj[u].emplace_back(v); adj[v].emplace_back(u);
}
dfsD();
//for(int i = 1; i <= n; ++i)cout << dpD[i].F << " "; cout << endl;
dfsU();
for(int i = 1; i <= n; ++i)if(dpU[i].F)D[i].emplace_back(dpU[i]);
//for(int i= 1; i <= n; ++i)cout << dpU[i].F << " "; cout << endl;
solve();
if(!cnt)cnt = 1;
cout << mx << " " << cnt;
}
Compilation message
road.cpp: In function 'void solve()':
road.cpp:67:30: 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]
67 | for(int x = 0; x < D[u].size(); ++x) if(D[u][x].F ==x1)Y += D[u][x].S;
| ~~^~~~~~~~~~~~~
road.cpp:68:30: 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]
68 | for(int x = 0; x < D[u].size(); ++x){
| ~~^~~~~~~~~~~~~
road.cpp:75:30: 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]
75 | for(int x = 0; x < D[u].size(); ++x) if(D[u][x].F == x3)Y += D[u][x].S;
| ~~^~~~~~~~~~~~~
road.cpp:80:29: 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]
80 | for(int x = 0; x< D[u].size(); ++x)if(D[u][x].F == x2) Y += D[u][x].S;
| ~^~~~~~~~~~~~~
road.cpp:81:29: 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]
81 | for(int x= 0; x < D[u].size(); ++x){
| ~~^~~~~~~~~~~~~
road.cpp:86:25: 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]
86 | for(int x = 0;x < D[u].size(); ++x)if(D[u][x].F == x3)Y+= D[u][x].S;
| ~~^~~~~~~~~~~~~
road.cpp: At global scope:
road.cpp:91:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
91 | main (){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23864 KB |
Output is correct |
2 |
Correct |
14 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23788 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
14 ms |
23788 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
15 ms |
23788 KB |
Output is correct |
9 |
Correct |
16 ms |
23788 KB |
Output is correct |
10 |
Correct |
14 ms |
23788 KB |
Output is correct |
11 |
Correct |
14 ms |
23788 KB |
Output is correct |
12 |
Correct |
15 ms |
23788 KB |
Output is correct |
13 |
Correct |
14 ms |
23788 KB |
Output is correct |
14 |
Correct |
14 ms |
23788 KB |
Output is correct |
15 |
Correct |
17 ms |
23800 KB |
Output is correct |
16 |
Correct |
14 ms |
23788 KB |
Output is correct |
17 |
Correct |
15 ms |
23788 KB |
Output is correct |
18 |
Correct |
14 ms |
23788 KB |
Output is correct |
19 |
Correct |
14 ms |
23788 KB |
Output is correct |
20 |
Correct |
15 ms |
23908 KB |
Output is correct |
21 |
Correct |
14 ms |
23788 KB |
Output is correct |
22 |
Correct |
15 ms |
23788 KB |
Output is correct |
23 |
Correct |
14 ms |
23788 KB |
Output is correct |
24 |
Correct |
15 ms |
23788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23864 KB |
Output is correct |
2 |
Correct |
14 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23788 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
14 ms |
23788 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
15 ms |
23788 KB |
Output is correct |
9 |
Correct |
16 ms |
23788 KB |
Output is correct |
10 |
Correct |
14 ms |
23788 KB |
Output is correct |
11 |
Correct |
14 ms |
23788 KB |
Output is correct |
12 |
Correct |
15 ms |
23788 KB |
Output is correct |
13 |
Correct |
14 ms |
23788 KB |
Output is correct |
14 |
Correct |
14 ms |
23788 KB |
Output is correct |
15 |
Correct |
17 ms |
23800 KB |
Output is correct |
16 |
Correct |
14 ms |
23788 KB |
Output is correct |
17 |
Correct |
15 ms |
23788 KB |
Output is correct |
18 |
Correct |
14 ms |
23788 KB |
Output is correct |
19 |
Correct |
14 ms |
23788 KB |
Output is correct |
20 |
Correct |
15 ms |
23908 KB |
Output is correct |
21 |
Correct |
14 ms |
23788 KB |
Output is correct |
22 |
Correct |
15 ms |
23788 KB |
Output is correct |
23 |
Correct |
14 ms |
23788 KB |
Output is correct |
24 |
Correct |
15 ms |
23788 KB |
Output is correct |
25 |
Correct |
17 ms |
24684 KB |
Output is correct |
26 |
Correct |
18 ms |
24812 KB |
Output is correct |
27 |
Correct |
18 ms |
24832 KB |
Output is correct |
28 |
Correct |
17 ms |
24812 KB |
Output is correct |
29 |
Correct |
18 ms |
24684 KB |
Output is correct |
30 |
Correct |
21 ms |
24832 KB |
Output is correct |
31 |
Correct |
19 ms |
24812 KB |
Output is correct |
32 |
Correct |
17 ms |
24684 KB |
Output is correct |
33 |
Correct |
17 ms |
24812 KB |
Output is correct |
34 |
Correct |
17 ms |
24812 KB |
Output is correct |
35 |
Correct |
18 ms |
24812 KB |
Output is correct |
36 |
Correct |
17 ms |
24812 KB |
Output is correct |
37 |
Correct |
18 ms |
24812 KB |
Output is correct |
38 |
Correct |
17 ms |
25068 KB |
Output is correct |
39 |
Correct |
18 ms |
24812 KB |
Output is correct |
40 |
Correct |
18 ms |
24556 KB |
Output is correct |
41 |
Correct |
18 ms |
24556 KB |
Output is correct |
42 |
Correct |
18 ms |
24556 KB |
Output is correct |
43 |
Correct |
17 ms |
24556 KB |
Output is correct |
44 |
Correct |
18 ms |
24556 KB |
Output is correct |
45 |
Correct |
17 ms |
24428 KB |
Output is correct |
46 |
Correct |
17 ms |
24428 KB |
Output is correct |
47 |
Correct |
17 ms |
24428 KB |
Output is correct |
48 |
Correct |
17 ms |
24428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23864 KB |
Output is correct |
2 |
Correct |
14 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23788 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
14 ms |
23788 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
15 ms |
23788 KB |
Output is correct |
9 |
Correct |
16 ms |
23788 KB |
Output is correct |
10 |
Correct |
14 ms |
23788 KB |
Output is correct |
11 |
Correct |
14 ms |
23788 KB |
Output is correct |
12 |
Correct |
15 ms |
23788 KB |
Output is correct |
13 |
Correct |
14 ms |
23788 KB |
Output is correct |
14 |
Correct |
14 ms |
23788 KB |
Output is correct |
15 |
Correct |
17 ms |
23800 KB |
Output is correct |
16 |
Correct |
14 ms |
23788 KB |
Output is correct |
17 |
Correct |
15 ms |
23788 KB |
Output is correct |
18 |
Correct |
14 ms |
23788 KB |
Output is correct |
19 |
Correct |
14 ms |
23788 KB |
Output is correct |
20 |
Correct |
15 ms |
23908 KB |
Output is correct |
21 |
Correct |
14 ms |
23788 KB |
Output is correct |
22 |
Correct |
15 ms |
23788 KB |
Output is correct |
23 |
Correct |
14 ms |
23788 KB |
Output is correct |
24 |
Correct |
15 ms |
23788 KB |
Output is correct |
25 |
Correct |
17 ms |
24684 KB |
Output is correct |
26 |
Correct |
18 ms |
24812 KB |
Output is correct |
27 |
Correct |
18 ms |
24832 KB |
Output is correct |
28 |
Correct |
17 ms |
24812 KB |
Output is correct |
29 |
Correct |
18 ms |
24684 KB |
Output is correct |
30 |
Correct |
21 ms |
24832 KB |
Output is correct |
31 |
Correct |
19 ms |
24812 KB |
Output is correct |
32 |
Correct |
17 ms |
24684 KB |
Output is correct |
33 |
Correct |
17 ms |
24812 KB |
Output is correct |
34 |
Correct |
17 ms |
24812 KB |
Output is correct |
35 |
Correct |
18 ms |
24812 KB |
Output is correct |
36 |
Correct |
17 ms |
24812 KB |
Output is correct |
37 |
Correct |
18 ms |
24812 KB |
Output is correct |
38 |
Correct |
17 ms |
25068 KB |
Output is correct |
39 |
Correct |
18 ms |
24812 KB |
Output is correct |
40 |
Correct |
18 ms |
24556 KB |
Output is correct |
41 |
Correct |
18 ms |
24556 KB |
Output is correct |
42 |
Correct |
18 ms |
24556 KB |
Output is correct |
43 |
Correct |
17 ms |
24556 KB |
Output is correct |
44 |
Correct |
18 ms |
24556 KB |
Output is correct |
45 |
Correct |
17 ms |
24428 KB |
Output is correct |
46 |
Correct |
17 ms |
24428 KB |
Output is correct |
47 |
Correct |
17 ms |
24428 KB |
Output is correct |
48 |
Correct |
17 ms |
24428 KB |
Output is correct |
49 |
Correct |
677 ms |
111468 KB |
Output is correct |
50 |
Correct |
684 ms |
115308 KB |
Output is correct |
51 |
Correct |
691 ms |
118504 KB |
Output is correct |
52 |
Correct |
691 ms |
106988 KB |
Output is correct |
53 |
Correct |
597 ms |
119788 KB |
Output is correct |
54 |
Correct |
587 ms |
123500 KB |
Output is correct |
55 |
Correct |
584 ms |
114540 KB |
Output is correct |
56 |
Correct |
586 ms |
118396 KB |
Output is correct |
57 |
Correct |
652 ms |
120400 KB |
Output is correct |
58 |
Correct |
661 ms |
116008 KB |
Output is correct |
59 |
Correct |
652 ms |
115884 KB |
Output is correct |
60 |
Correct |
654 ms |
113856 KB |
Output is correct |
61 |
Correct |
933 ms |
145012 KB |
Output is correct |
62 |
Correct |
932 ms |
134940 KB |
Output is correct |
63 |
Correct |
894 ms |
106080 KB |
Output is correct |
64 |
Correct |
892 ms |
99140 KB |
Output is correct |
65 |
Correct |
890 ms |
94832 KB |
Output is correct |
66 |
Correct |
893 ms |
92536 KB |
Output is correct |
67 |
Correct |
863 ms |
91272 KB |
Output is correct |
68 |
Correct |
849 ms |
90708 KB |
Output is correct |
69 |
Correct |
856 ms |
90732 KB |
Output is correct |
70 |
Correct |
851 ms |
90220 KB |
Output is correct |
71 |
Correct |
843 ms |
90144 KB |
Output is correct |
72 |
Correct |
869 ms |
90312 KB |
Output is correct |
73 |
Correct |
835 ms |
90476 KB |
Output is correct |
74 |
Correct |
828 ms |
90236 KB |
Output is correct |
75 |
Correct |
804 ms |
90040 KB |
Output is correct |
76 |
Correct |
761 ms |
89948 KB |
Output is correct |
77 |
Correct |
636 ms |
87724 KB |
Output is correct |
78 |
Correct |
410 ms |
89928 KB |
Output is correct |