#include <bits/stdc++.h>
using namespace std;
#define forn(i, a, b) for(int i=a; i<=b; ++i)
#define ford(i, a, b) for(int i=a; i>=b; --i)
#define repn(i, a, b) for(int i=a; i <b; ++i)
#define repd(i, a, b) for(int i=(int)a -1; i>=b; --i)
#define llong long long
#define ii pair<llong, llong>
const llong INF = 1e10 + 1;
const int maxN = 5e5 + 10;
int N;
vector<ii> D[maxN];
vector<int> E[maxN];
ii F[maxN], out[maxN];
void maximize(ii &a, ii b) {
if (a.first < b.first + 1) {
a.first = b.first + 1;
a.second = b.second;
} else if (a.first == b.first + 1) a.second += b.second;
}
void dfs1(int u, int prv = 0) {
F[u] = {-INF, 0};
bool leaf = true;
for(auto v: E[u]) if (v != prv) {
leaf = false;
dfs1(v, u);
maximize(F[u], F[v]);
D[u].push_back(F[v]);
}
sort(D[u].begin(), D[u].end());
reverse(D[u].begin(), D[u].end());
D[u].push_back({-INF, 0});
if (leaf) F[u] = {0, 1};
}
void dfs2(int u, int prv = 0) {
out[u] = {-INF, 0};
if (!prv) out[u] = {0, 1};
else {
maximize(out[u], out[prv]);
llong sum2 = 0, maxDist2 = -INF;
repn(i, 0, D[prv].size()) if (D[prv][i].first + 1 != F[prv].first) {
maxDist2 = D[prv][i].first + 1;
repn(j, i, D[prv].size()) if (D[prv][j].first == D[prv][i].first) sum2 += D[prv][j].second;
else break;
break;
}
if (F[u].first + 1 == F[prv].first) {
if (F[u].second != F[prv].second) maximize(out[u], {F[prv].first, F[prv].second -F[u].second});
else maximize(out[u], {maxDist2, sum2});
} else maximize(out[u], F[prv]);
}
for(auto v: E[u]) if (v != prv) dfs2(v, u);
}
inline int ReadInt()
{
int sign = 1;
char c;
do
{
c = getchar();
if (c == '-') sign = -sign;
}
while (c < '0' || c > '9');
int res;
for (res = 0; c >= '0' && c <= '9'; c = getchar()) res = res * 10 + (c - '0');
return res * sign;
}
int main() {
//freopen("a.inp", "r", stdin);
N = ReadInt();
repn(i, 1, N) {
int u, v;
u = ReadInt(); v = ReadInt();
E[u].push_back(v);
E[v].push_back(u);
}
dfs1(1);
dfs2(1);
llong result = 0, ways = 0;
forn(u, 1, N) {
for(auto &x: D[u]) x.first ++;
D[u].push_back(out[u]);
sort(D[u].begin(), D[u].end());
reverse(D[u].begin(), D[u].end());
if (D[u].size() <= 3 || D[u][2].first <= 0) continue;
while (D[u].size() && D[u].back().first != D[u][2].first) D[u].pop_back();
long long paths, a, ab, abc;
paths = a = ab = abc = 0;
if (D[u][0].first == D[u][2].first) {
for(auto v: D[u]) {
ab += v.second * a;
a += v.second;
}
paths = ab;
} else {
if (D[u][1].first == D[u][2].first) {
for(auto v: D[u]) if (v.first != D[u][0].first) {
ab += v.second * a;
a += v.second;
}
paths = ab;
} else {
for(auto v: D[u]) if (v.first == D[u][2].first)
a += v.second;
if (D[u][0].first == D[u][1].first)
paths = (D[u][0].second + D[u][1].second) * a;
else paths = D[u][1].second * a;
}
}
llong Dist = D[u][0].first * (D[u][1].first + D[u][2].first);
if (result < Dist) {
result = Dist;
ways = paths;
} else if (result == Dist) ways += paths;
}
if (result == 0) ways = 1;
cout << result << ' ' << ways << '\n';
return 0;
}
Compilation message
road.cpp: In function 'void dfs2(int, int)':
road.cpp:5:52: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define repn(i, a, b) for(int i=a; i <b; ++i)
road.cpp:41:18:
repn(i, 0, D[prv].size()) if (D[prv][i].first + 1 != F[prv].first) {
~~~~~~~~~~~~~~~~~~~
road.cpp:41:13: note: in expansion of macro 'repn'
repn(i, 0, D[prv].size()) if (D[prv][i].first + 1 != F[prv].first) {
^~~~
road.cpp:5:52: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define repn(i, a, b) for(int i=a; i <b; ++i)
road.cpp:43:22:
repn(j, i, D[prv].size()) if (D[prv][j].first == D[prv][i].first) sum2 += D[prv][j].second;
~~~~~~~~~~~~~~~~~~~
road.cpp:43:17: note: in expansion of macro 'repn'
repn(j, i, D[prv].size()) if (D[prv][j].first == D[prv][i].first) sum2 += D[prv][j].second;
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23760 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23848 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
23 ms |
23792 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
23 ms |
23800 KB |
Output is correct |
21 |
Correct |
24 ms |
23800 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23760 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23848 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
23 ms |
23792 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
23 ms |
23800 KB |
Output is correct |
21 |
Correct |
24 ms |
23800 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
25 |
Correct |
27 ms |
24700 KB |
Output is correct |
26 |
Correct |
27 ms |
24824 KB |
Output is correct |
27 |
Correct |
27 ms |
24824 KB |
Output is correct |
28 |
Correct |
27 ms |
24824 KB |
Output is correct |
29 |
Correct |
27 ms |
24800 KB |
Output is correct |
30 |
Correct |
27 ms |
24824 KB |
Output is correct |
31 |
Correct |
28 ms |
24824 KB |
Output is correct |
32 |
Correct |
27 ms |
24696 KB |
Output is correct |
33 |
Correct |
28 ms |
24824 KB |
Output is correct |
34 |
Correct |
33 ms |
24952 KB |
Output is correct |
35 |
Correct |
30 ms |
24828 KB |
Output is correct |
36 |
Correct |
28 ms |
24824 KB |
Output is correct |
37 |
Correct |
28 ms |
24952 KB |
Output is correct |
38 |
Correct |
29 ms |
25212 KB |
Output is correct |
39 |
Correct |
28 ms |
24952 KB |
Output is correct |
40 |
Correct |
28 ms |
24696 KB |
Output is correct |
41 |
Correct |
28 ms |
24696 KB |
Output is correct |
42 |
Correct |
27 ms |
24572 KB |
Output is correct |
43 |
Correct |
33 ms |
24568 KB |
Output is correct |
44 |
Correct |
27 ms |
24568 KB |
Output is correct |
45 |
Correct |
27 ms |
24568 KB |
Output is correct |
46 |
Correct |
27 ms |
24440 KB |
Output is correct |
47 |
Correct |
33 ms |
24572 KB |
Output is correct |
48 |
Correct |
53 ms |
24568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23760 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23848 KB |
Output is correct |
6 |
Correct |
23 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
23 ms |
23792 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
23 ms |
23800 KB |
Output is correct |
21 |
Correct |
24 ms |
23800 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
25 |
Correct |
27 ms |
24700 KB |
Output is correct |
26 |
Correct |
27 ms |
24824 KB |
Output is correct |
27 |
Correct |
27 ms |
24824 KB |
Output is correct |
28 |
Correct |
27 ms |
24824 KB |
Output is correct |
29 |
Correct |
27 ms |
24800 KB |
Output is correct |
30 |
Correct |
27 ms |
24824 KB |
Output is correct |
31 |
Correct |
28 ms |
24824 KB |
Output is correct |
32 |
Correct |
27 ms |
24696 KB |
Output is correct |
33 |
Correct |
28 ms |
24824 KB |
Output is correct |
34 |
Correct |
33 ms |
24952 KB |
Output is correct |
35 |
Correct |
30 ms |
24828 KB |
Output is correct |
36 |
Correct |
28 ms |
24824 KB |
Output is correct |
37 |
Correct |
28 ms |
24952 KB |
Output is correct |
38 |
Correct |
29 ms |
25212 KB |
Output is correct |
39 |
Correct |
28 ms |
24952 KB |
Output is correct |
40 |
Correct |
28 ms |
24696 KB |
Output is correct |
41 |
Correct |
28 ms |
24696 KB |
Output is correct |
42 |
Correct |
27 ms |
24572 KB |
Output is correct |
43 |
Correct |
33 ms |
24568 KB |
Output is correct |
44 |
Correct |
27 ms |
24568 KB |
Output is correct |
45 |
Correct |
27 ms |
24568 KB |
Output is correct |
46 |
Correct |
27 ms |
24440 KB |
Output is correct |
47 |
Correct |
33 ms |
24572 KB |
Output is correct |
48 |
Correct |
53 ms |
24568 KB |
Output is correct |
49 |
Correct |
926 ms |
121120 KB |
Output is correct |
50 |
Correct |
1052 ms |
125524 KB |
Output is correct |
51 |
Correct |
913 ms |
129180 KB |
Output is correct |
52 |
Correct |
859 ms |
115960 KB |
Output is correct |
53 |
Correct |
842 ms |
116424 KB |
Output is correct |
54 |
Correct |
860 ms |
123512 KB |
Output is correct |
55 |
Correct |
822 ms |
112296 KB |
Output is correct |
56 |
Correct |
825 ms |
117076 KB |
Output is correct |
57 |
Correct |
936 ms |
134320 KB |
Output is correct |
58 |
Correct |
1066 ms |
129428 KB |
Output is correct |
59 |
Correct |
900 ms |
129472 KB |
Output is correct |
60 |
Correct |
836 ms |
126968 KB |
Output is correct |
61 |
Correct |
1125 ms |
162468 KB |
Output is correct |
62 |
Correct |
1129 ms |
151196 KB |
Output is correct |
63 |
Correct |
1064 ms |
118136 KB |
Output is correct |
64 |
Correct |
1049 ms |
110300 KB |
Output is correct |
65 |
Correct |
1056 ms |
105220 KB |
Output is correct |
66 |
Correct |
1068 ms |
102576 KB |
Output is correct |
67 |
Correct |
1053 ms |
101136 KB |
Output is correct |
68 |
Correct |
1050 ms |
100648 KB |
Output is correct |
69 |
Correct |
1012 ms |
100152 KB |
Output is correct |
70 |
Correct |
1006 ms |
100252 KB |
Output is correct |
71 |
Correct |
1048 ms |
99944 KB |
Output is correct |
72 |
Correct |
1123 ms |
100188 KB |
Output is correct |
73 |
Correct |
1156 ms |
99940 KB |
Output is correct |
74 |
Correct |
1451 ms |
99928 KB |
Output is correct |
75 |
Execution timed out |
2057 ms |
78816 KB |
Time limit exceeded |
76 |
Halted |
0 ms |
0 KB |
- |