#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
int n, dp[500005], cnt[500005], res = 0, way = 1;
vector<int> adj[500005];
void init(int u, int p){
cnt[u] = 1;
for (auto x : adj[u]){
if (x == p) continue;
init(x, u);
if (dp[x] + 1 == dp[u]) cnt[u] += cnt[x];
if (dp[x] + 1 > dp[u]) cnt[u] = cnt[x];
dp[u] = max(dp[u], dp[x] + 1);
}
}
bool cmp (int& i, int& j){
return dp[i] > dp[j];
}
void rr(int u, int p){
sort(adj[u].begin(), adj[u].end(), cmp);
if (adj[u].size() >= 3){
int cur = 0, cn = 0;
int a = dp[adj[u][0]] + 1, b = dp[adj[u][1]] + 1, c = dp[adj[u][2]] + 1;
cur = a * (b + c);
int ca = 0;
for (auto x : adj[u]){
if (dp[x] + 1 == c) cn += ca * cnt[x];
if (dp[x] + 1 == b) ca += cnt[x];
}
if (cur > res) res = cur, way = cn;
else if (cur == res) way += cn;
}
pair<int, int> f = {0, 1}, s = {0, 0};
for (auto x : adj[u]){
if (dp[x] + 1 > f.ff) s = f, f = {dp[x] + 1, cnt[x]};
else if (dp[x] + 1 == f.ff) f.ss += cnt[x];
else if (dp[x] + 1 > s.ff) s = {dp[x] + 1, cnt[x]};
else if (dp[x] + 1 == s.ff) s.ss += cnt[x];
}
int tmpdp = dp[u], tmpcnt = cnt[u];
for (auto x : adj[u]){
if (x == p) continue;
dp[u] = f.ff, cnt[u] = f.ss;
if (dp[x] + 1 == f.ff && cnt[x] == f.ss) dp[u] = s.ff, cnt[u] = s.ss;
else if (dp[x] + 1 == f.ff && cnt[x] != f.ss) dp[u] = f.ff, cnt[u] = f.ss - cnt[x];
rr(x, u);
}
dp[u] = tmpdp, cnt[u] = tmpcnt;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for (int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
init(1, 0);
rr(1, 0);
cout << res << ' ' << way;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12948 KB |
Output is correct |
5 |
Correct |
3 ms |
12952 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12956 KB |
Output is correct |
10 |
Correct |
3 ms |
13056 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12904 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12888 KB |
Output is correct |
15 |
Correct |
3 ms |
12888 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12888 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
5 ms |
12948 KB |
Output is correct |
22 |
Correct |
4 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12948 KB |
Output is correct |
5 |
Correct |
3 ms |
12952 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12956 KB |
Output is correct |
10 |
Correct |
3 ms |
13056 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12904 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12888 KB |
Output is correct |
15 |
Correct |
3 ms |
12888 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12888 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
5 ms |
12948 KB |
Output is correct |
22 |
Correct |
4 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12892 KB |
Output is correct |
25 |
Correct |
5 ms |
13404 KB |
Output is correct |
26 |
Correct |
5 ms |
13492 KB |
Output is correct |
27 |
Correct |
5 ms |
13472 KB |
Output is correct |
28 |
Correct |
4 ms |
13400 KB |
Output is correct |
29 |
Correct |
4 ms |
13404 KB |
Output is correct |
30 |
Correct |
5 ms |
13480 KB |
Output is correct |
31 |
Correct |
4 ms |
13404 KB |
Output is correct |
32 |
Correct |
4 ms |
13404 KB |
Output is correct |
33 |
Correct |
4 ms |
13404 KB |
Output is correct |
34 |
Correct |
4 ms |
13404 KB |
Output is correct |
35 |
Correct |
5 ms |
13304 KB |
Output is correct |
36 |
Correct |
5 ms |
13428 KB |
Output is correct |
37 |
Correct |
4 ms |
13480 KB |
Output is correct |
38 |
Correct |
5 ms |
13660 KB |
Output is correct |
39 |
Correct |
4 ms |
13404 KB |
Output is correct |
40 |
Correct |
5 ms |
13344 KB |
Output is correct |
41 |
Correct |
4 ms |
13156 KB |
Output is correct |
42 |
Correct |
4 ms |
13148 KB |
Output is correct |
43 |
Correct |
4 ms |
13148 KB |
Output is correct |
44 |
Correct |
4 ms |
13148 KB |
Output is correct |
45 |
Correct |
4 ms |
13148 KB |
Output is correct |
46 |
Correct |
4 ms |
13148 KB |
Output is correct |
47 |
Correct |
4 ms |
13148 KB |
Output is correct |
48 |
Correct |
4 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12948 KB |
Output is correct |
5 |
Correct |
3 ms |
12952 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12956 KB |
Output is correct |
10 |
Correct |
3 ms |
13056 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12904 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12888 KB |
Output is correct |
15 |
Correct |
3 ms |
12888 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12888 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
5 ms |
12948 KB |
Output is correct |
22 |
Correct |
4 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12892 KB |
Output is correct |
25 |
Correct |
5 ms |
13404 KB |
Output is correct |
26 |
Correct |
5 ms |
13492 KB |
Output is correct |
27 |
Correct |
5 ms |
13472 KB |
Output is correct |
28 |
Correct |
4 ms |
13400 KB |
Output is correct |
29 |
Correct |
4 ms |
13404 KB |
Output is correct |
30 |
Correct |
5 ms |
13480 KB |
Output is correct |
31 |
Correct |
4 ms |
13404 KB |
Output is correct |
32 |
Correct |
4 ms |
13404 KB |
Output is correct |
33 |
Correct |
4 ms |
13404 KB |
Output is correct |
34 |
Correct |
4 ms |
13404 KB |
Output is correct |
35 |
Correct |
5 ms |
13304 KB |
Output is correct |
36 |
Correct |
5 ms |
13428 KB |
Output is correct |
37 |
Correct |
4 ms |
13480 KB |
Output is correct |
38 |
Correct |
5 ms |
13660 KB |
Output is correct |
39 |
Correct |
4 ms |
13404 KB |
Output is correct |
40 |
Correct |
5 ms |
13344 KB |
Output is correct |
41 |
Correct |
4 ms |
13156 KB |
Output is correct |
42 |
Correct |
4 ms |
13148 KB |
Output is correct |
43 |
Correct |
4 ms |
13148 KB |
Output is correct |
44 |
Correct |
4 ms |
13148 KB |
Output is correct |
45 |
Correct |
4 ms |
13148 KB |
Output is correct |
46 |
Correct |
4 ms |
13148 KB |
Output is correct |
47 |
Correct |
4 ms |
13148 KB |
Output is correct |
48 |
Correct |
4 ms |
13148 KB |
Output is correct |
49 |
Correct |
356 ms |
67500 KB |
Output is correct |
50 |
Correct |
313 ms |
71764 KB |
Output is correct |
51 |
Correct |
358 ms |
75380 KB |
Output is correct |
52 |
Correct |
356 ms |
62292 KB |
Output is correct |
53 |
Correct |
260 ms |
71936 KB |
Output is correct |
54 |
Correct |
315 ms |
76156 KB |
Output is correct |
55 |
Correct |
288 ms |
66124 KB |
Output is correct |
56 |
Correct |
317 ms |
70680 KB |
Output is correct |
57 |
Correct |
320 ms |
76972 KB |
Output is correct |
58 |
Correct |
312 ms |
72028 KB |
Output is correct |
59 |
Correct |
281 ms |
71952 KB |
Output is correct |
60 |
Correct |
304 ms |
69456 KB |
Output is correct |
61 |
Correct |
578 ms |
105056 KB |
Output is correct |
62 |
Correct |
535 ms |
93692 KB |
Output is correct |
63 |
Correct |
482 ms |
60500 KB |
Output is correct |
64 |
Correct |
502 ms |
52848 KB |
Output is correct |
65 |
Correct |
458 ms |
48052 KB |
Output is correct |
66 |
Correct |
469 ms |
44948 KB |
Output is correct |
67 |
Correct |
507 ms |
43720 KB |
Output is correct |
68 |
Correct |
495 ms |
43004 KB |
Output is correct |
69 |
Correct |
515 ms |
42976 KB |
Output is correct |
70 |
Correct |
456 ms |
42660 KB |
Output is correct |
71 |
Correct |
527 ms |
42700 KB |
Output is correct |
72 |
Correct |
493 ms |
42792 KB |
Output is correct |
73 |
Correct |
533 ms |
42860 KB |
Output is correct |
74 |
Correct |
519 ms |
42780 KB |
Output is correct |
75 |
Correct |
461 ms |
42956 KB |
Output is correct |
76 |
Correct |
476 ms |
43276 KB |
Output is correct |
77 |
Correct |
356 ms |
44348 KB |
Output is correct |
78 |
Correct |
226 ms |
46760 KB |
Output is correct |