#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define sp << " " <<
#define endl << '\n'
#ifdef L
ifstream input("input.txt");
#define cin input
#else
// #define USACO
#ifdef USACO
ifstream input("atlarge.in");
ofstream output("atlarge.out");
#define cin input
#define cout output
#endif
#endif
vector<vector<int>> graph;
vector<long long int> dist;
vector<long long int> distind;
vector<long long int> distmul;
long long int hard = 0;
long long int cnt = 0;
int n;
void dp(int node = 0, int parent = -1){
for (auto go : graph[node]){
if (go == parent)
continue;
dp(go, node);
if (dist[node] < dist[go] + 1){
dist[node] = dist[go] + 1;
distmul[node] = distmul[go];
}else if (dist[node] == dist[go] + 1){
distmul[node] += distmul[go];
}
}
}
void reroot(int node = 0, int parent = -1, long long int parentdist = -1, long long int parentmul = 1){
long long int big1 = parentdist + 1, big2 = 0, big3 = 0;
long long int mul1 = parentmul, mul2 = 0, mul3 = 0;
for (int go : graph[node]){
if (go == parent)
continue;
if (dist[go] + 1 > big1){
big3 = big2;
mul3 = mul2;
big2 = big1;
mul2 = mul1;
big1 = dist[go] + 1;
mul1 = distmul[go];
}else if (dist[go] + 1 > big2){
big3 = big2;
mul3 = mul2;
big2 = dist[go] + 1;
mul2 = distmul[go];
}else if (dist[go] + 1 > big3){
big3 = dist[go] + 1;
mul3 = distmul[go];
}
}
long long int deg1 = big1 * (big2 + big3);
if (graph[node].size() > 2 && deg1 > hard){
hard = deg1;
}
for (int go : graph[node]){
if (go == parent)
continue;
if (big1 == dist[go] + 1){
reroot(go, node, big2, mul2);
}else{
reroot(go, node, big1, mul1);
}
}
}
void reroot2(int node = 0, int parent = -1, long long int parentdist = -1, long long int parentmul = 0, bool parentused = true){
if (graph[node].size() == 0)
return;
long long int big1 = parentdist + 1, big2 = 0, big3 = 0;
long long int mul1 = max(parentmul, 1LL), mul2 = 0, mul3 = 0;
long long int smul1 = parentmul, smul2 = 0;
long long int num1 = !parentused, num2 = 0, num3 = 0;
if (parentused){
big1 = 0;
mul1 = 0;
}
for (int go : graph[node]){
if (go == parent)
continue;
if (dist[go] + 1 == big1){
mul1 += distmul[go];
smul1 += distmul[go] * distmul[go];
num1++;
}else if (dist[go] + 1 == big2){
mul2 += distmul[go];
smul2 += distmul[go] * distmul[go];
num2++;
}else if (dist[go] + 1 == big3){
mul3 += distmul[go];
num3++;
}else if (dist[go] + 1 > big1){
big3 = big2;
mul3 = mul2;
num3 = num2;
big2 = big1;
mul2 = mul1;
num2 = num1;
smul2 = smul1;
big1 = dist[go] + 1;
mul1 = distmul[go];
num1 = 1;
smul1 = mul1 * mul1;
}else if (dist[go] + 1 > big2){
big3 = big2;
mul3 = mul2;
num3 = num2;
big2 = dist[go] + 1;
mul2 = distmul[go];
smul2 = mul2 * mul2;
num2 = 1;
}else if (dist[go] + 1 > big3){
big3 = dist[go] + 1;
mul3 = distmul[go];
num3 = 1;
}
}
// cerr << node sp "|" sp big1 sp mul1 sp num1 sp "|" sp big2 sp mul2 sp num2 sp "|" sp big3 sp mul3 sp num3 sp "|" sp parentused sp "pd:"sp parentdist + 1 sp parentmul endl;
bool flag1 = false, flag2 = false;
if (parentused && (big1 + big2) * (parentdist + 1) == hard){
cnt += mul2 * mul1;
if (((parentdist + 1) + big2) * big1 == hard)
cnt += parentmul * mul2;
flag1 = true;
flag2 = true;
}else if (parentused && (num1 > 1 && (2 * big1 * (parentdist + 1)) == hard)){
cnt += (mul1 * mul1 - smul1) / 2;
if (((parentdist + 1) + big2) * big1 == hard)
cnt += parentmul * mul2;
flag1 = true;
flag2 = true;
}else if (big1 * (big2 + big3) == hard || (num2 > 1 && big1 * big2 * 2 == hard) || (num1 > 1 && big1 * (big1 + big2) == hard) || (num1 > 2 && big1 * big1 * 2 == hard)){
if (num1 > 2){
cnt += (mul1 * mul1 - smul1) / 2;
flag2 = true;
flag1 = true;
}else if (num1 > 1){
cnt += mul2 * mul1;
flag2 = true;
flag1 = true;
}else if (num2 > 1){
cnt += (mul2 * mul2 - smul2) / 2;
flag1 = true;
}else{
cnt += mul2 * mul3;
flag1 = true;
}
}
for (int go : graph[node]){
if (go == parent)
continue;
if (parentused){
if (big1 == dist[go] + 1 && num1 > 1 && big1 >= parentdist + 1)
reroot2(go, node, big1, mul1 - distmul[go], flag2);
else if (big1 != dist[go] + 1 && big1 >= parentdist + 1){
reroot2(go, node, big1, mul1, flag2);
}else if (big2 >= parentdist + 1){
reroot2(go, node, big2, mul2, flag1);
}else{
reroot2(go, node, parentdist + 1, parentmul, true);
}
}else{
if (big1 == dist[go] + 1 && num1 == 1){
reroot2(go, node, big2, mul2, flag1);
}else if (big1 == dist[go] + 1 && num1 > 1)
reroot2(go, node, big1, mul1 - distmul[go], flag2);
else
reroot2(go, node, big1, mul1, flag2);
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
graph.resize(n);
dist.resize(n, 0);
distind.resize(n);
distmul.resize(n, 1);
for (int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
a--; b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
dp();
reroot();
reroot2();
cout << hard sp max(1LL, cnt);
}
Compilation message
road.cpp: In function 'void reroot(int, int, long long int, long long int)':
road.cpp:52:44: warning: variable 'mul3' set but not used [-Wunused-but-set-variable]
52 | long long int mul1 = parentmul, mul2 = 0, mul3 = 0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
3 ms |
1104 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1104 KB |
Output is correct |
28 |
Correct |
3 ms |
1104 KB |
Output is correct |
29 |
Correct |
2 ms |
1104 KB |
Output is correct |
30 |
Correct |
3 ms |
1104 KB |
Output is correct |
31 |
Correct |
2 ms |
1104 KB |
Output is correct |
32 |
Correct |
2 ms |
1104 KB |
Output is correct |
33 |
Correct |
2 ms |
1036 KB |
Output is correct |
34 |
Correct |
3 ms |
1312 KB |
Output is correct |
35 |
Correct |
3 ms |
1104 KB |
Output is correct |
36 |
Correct |
2 ms |
1080 KB |
Output is correct |
37 |
Correct |
3 ms |
1360 KB |
Output is correct |
38 |
Correct |
3 ms |
1616 KB |
Output is correct |
39 |
Correct |
3 ms |
1104 KB |
Output is correct |
40 |
Correct |
2 ms |
848 KB |
Output is correct |
41 |
Correct |
2 ms |
848 KB |
Output is correct |
42 |
Correct |
2 ms |
848 KB |
Output is correct |
43 |
Correct |
3 ms |
848 KB |
Output is correct |
44 |
Correct |
2 ms |
884 KB |
Output is correct |
45 |
Correct |
2 ms |
848 KB |
Output is correct |
46 |
Correct |
2 ms |
592 KB |
Output is correct |
47 |
Correct |
2 ms |
848 KB |
Output is correct |
48 |
Correct |
2 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
3 ms |
1104 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1104 KB |
Output is correct |
28 |
Correct |
3 ms |
1104 KB |
Output is correct |
29 |
Correct |
2 ms |
1104 KB |
Output is correct |
30 |
Correct |
3 ms |
1104 KB |
Output is correct |
31 |
Correct |
2 ms |
1104 KB |
Output is correct |
32 |
Correct |
2 ms |
1104 KB |
Output is correct |
33 |
Correct |
2 ms |
1036 KB |
Output is correct |
34 |
Correct |
3 ms |
1312 KB |
Output is correct |
35 |
Correct |
3 ms |
1104 KB |
Output is correct |
36 |
Correct |
2 ms |
1080 KB |
Output is correct |
37 |
Correct |
3 ms |
1360 KB |
Output is correct |
38 |
Correct |
3 ms |
1616 KB |
Output is correct |
39 |
Correct |
3 ms |
1104 KB |
Output is correct |
40 |
Correct |
2 ms |
848 KB |
Output is correct |
41 |
Correct |
2 ms |
848 KB |
Output is correct |
42 |
Correct |
2 ms |
848 KB |
Output is correct |
43 |
Correct |
3 ms |
848 KB |
Output is correct |
44 |
Correct |
2 ms |
884 KB |
Output is correct |
45 |
Correct |
2 ms |
848 KB |
Output is correct |
46 |
Correct |
2 ms |
592 KB |
Output is correct |
47 |
Correct |
2 ms |
848 KB |
Output is correct |
48 |
Correct |
2 ms |
1016 KB |
Output is correct |
49 |
Correct |
356 ms |
75784 KB |
Output is correct |
50 |
Correct |
342 ms |
81228 KB |
Output is correct |
51 |
Correct |
330 ms |
85928 KB |
Output is correct |
52 |
Correct |
298 ms |
69300 KB |
Output is correct |
53 |
Correct |
258 ms |
78412 KB |
Output is correct |
54 |
Correct |
287 ms |
83528 KB |
Output is correct |
55 |
Correct |
279 ms |
70992 KB |
Output is correct |
56 |
Correct |
273 ms |
76616 KB |
Output is correct |
57 |
Correct |
360 ms |
89420 KB |
Output is correct |
58 |
Correct |
349 ms |
83272 KB |
Output is correct |
59 |
Correct |
372 ms |
83016 KB |
Output is correct |
60 |
Correct |
375 ms |
80200 KB |
Output is correct |
61 |
Correct |
679 ms |
124488 KB |
Output is correct |
62 |
Correct |
633 ms |
110204 KB |
Output is correct |
63 |
Correct |
619 ms |
69012 KB |
Output is correct |
64 |
Correct |
533 ms |
59168 KB |
Output is correct |
65 |
Correct |
523 ms |
52732 KB |
Output is correct |
66 |
Correct |
532 ms |
49480 KB |
Output is correct |
67 |
Correct |
504 ms |
47688 KB |
Output is correct |
68 |
Correct |
523 ms |
47176 KB |
Output is correct |
69 |
Correct |
374 ms |
46920 KB |
Output is correct |
70 |
Correct |
344 ms |
46664 KB |
Output is correct |
71 |
Correct |
314 ms |
46408 KB |
Output is correct |
72 |
Correct |
330 ms |
46672 KB |
Output is correct |
73 |
Correct |
390 ms |
46748 KB |
Output is correct |
74 |
Correct |
473 ms |
46920 KB |
Output is correct |
75 |
Correct |
605 ms |
46788 KB |
Output is correct |
76 |
Correct |
515 ms |
46916 KB |
Output is correct |
77 |
Correct |
359 ms |
47624 KB |
Output is correct |
78 |
Correct |
209 ms |
48684 KB |
Output is correct |