#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 500005
#define fi first
#define se second
typedef pair<int, int> ii;
int ma[N], cnt[N], n, res, way = 1;
vector<int> adj[N];
void dfs(int u, int p){
cnt[u] = 1;
for (auto x : adj[u]){
if (x == p) continue;
dfs(x, u);
if (ma[x] + 1 == ma[u]){
cnt[u] += cnt[x];
}
else if (ma[x] + 1 > ma[u]){
ma[u] = ma[x] + 1;
cnt[u] = cnt[x];
}
}
}
bool cmp(int& a, int& b){
return ma[a] > ma[b];
}
void rr(int u, int p){
sort(adj[u].begin(), adj[u].end(), cmp);
if (adj[u].size() >= 3){
int a = ma[adj[u][0]] + 1, b = ma[adj[u][1]] + 1, c = ma[adj[u][2]] + 1;
int m = a * (b + c);
int w = 0;
int db = 0;
a--, b--, c--;
for (auto x : adj[u]){
if (ma[x] == c) w += cnt[x] * db;
if (ma[x] == b) db += cnt[x];
}
if (m > res) res = m, way = w;
else if (m == res) way += w;
}
ii fir = {0, 1}, sed = {0, 1};
int tma = ma[u], tmw = cnt[u];
for (auto x : adj[u]){
if (ma[x] + 1> fir.fi){
sed = fir;
fir = {ma[x] + 1, cnt[x]};
}
else if (ma[x] + 1 == fir.fi) fir.se += cnt[x];
else if (ma[x] + 1 > sed.fi){
sed = {ma[x] + 1, cnt[x]};
}
else if (ma[x] + 1 == sed.fi) sed.se += cnt[x];
}
for (auto x : adj[u]){
if (x == p) continue;
ma[u] = fir.fi, cnt[u] = fir.se;
if (fir.fi == ma[x] + 1 && fir.se == cnt[x]) ma[u] = sed.fi , cnt[u] = sed.se;
else if (fir.fi == ma[x] + 1) cnt[u] -= cnt[x];
rr(x, u);
}
ma[u] = tma, cnt[u] = tmw;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
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, 0);
rr(1, 0);
cout << res << " " << way;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12124 KB |
Output is correct |
2 |
Correct |
2 ms |
12124 KB |
Output is correct |
3 |
Correct |
3 ms |
12124 KB |
Output is correct |
4 |
Correct |
3 ms |
12124 KB |
Output is correct |
5 |
Correct |
3 ms |
12208 KB |
Output is correct |
6 |
Correct |
2 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
12124 KB |
Output is correct |
9 |
Correct |
3 ms |
12124 KB |
Output is correct |
10 |
Correct |
3 ms |
12120 KB |
Output is correct |
11 |
Correct |
3 ms |
12120 KB |
Output is correct |
12 |
Correct |
3 ms |
12204 KB |
Output is correct |
13 |
Correct |
2 ms |
12000 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
3 ms |
12124 KB |
Output is correct |
16 |
Correct |
3 ms |
12120 KB |
Output is correct |
17 |
Correct |
5 ms |
12120 KB |
Output is correct |
18 |
Correct |
5 ms |
12124 KB |
Output is correct |
19 |
Correct |
4 ms |
12124 KB |
Output is correct |
20 |
Correct |
4 ms |
12124 KB |
Output is correct |
21 |
Correct |
4 ms |
12124 KB |
Output is correct |
22 |
Correct |
7 ms |
12224 KB |
Output is correct |
23 |
Correct |
5 ms |
12124 KB |
Output is correct |
24 |
Correct |
4 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12124 KB |
Output is correct |
2 |
Correct |
2 ms |
12124 KB |
Output is correct |
3 |
Correct |
3 ms |
12124 KB |
Output is correct |
4 |
Correct |
3 ms |
12124 KB |
Output is correct |
5 |
Correct |
3 ms |
12208 KB |
Output is correct |
6 |
Correct |
2 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
12124 KB |
Output is correct |
9 |
Correct |
3 ms |
12124 KB |
Output is correct |
10 |
Correct |
3 ms |
12120 KB |
Output is correct |
11 |
Correct |
3 ms |
12120 KB |
Output is correct |
12 |
Correct |
3 ms |
12204 KB |
Output is correct |
13 |
Correct |
2 ms |
12000 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
3 ms |
12124 KB |
Output is correct |
16 |
Correct |
3 ms |
12120 KB |
Output is correct |
17 |
Correct |
5 ms |
12120 KB |
Output is correct |
18 |
Correct |
5 ms |
12124 KB |
Output is correct |
19 |
Correct |
4 ms |
12124 KB |
Output is correct |
20 |
Correct |
4 ms |
12124 KB |
Output is correct |
21 |
Correct |
4 ms |
12124 KB |
Output is correct |
22 |
Correct |
7 ms |
12224 KB |
Output is correct |
23 |
Correct |
5 ms |
12124 KB |
Output is correct |
24 |
Correct |
4 ms |
12124 KB |
Output is correct |
25 |
Correct |
6 ms |
12616 KB |
Output is correct |
26 |
Correct |
6 ms |
12636 KB |
Output is correct |
27 |
Correct |
6 ms |
12636 KB |
Output is correct |
28 |
Correct |
5 ms |
12620 KB |
Output is correct |
29 |
Correct |
6 ms |
12628 KB |
Output is correct |
30 |
Correct |
6 ms |
12636 KB |
Output is correct |
31 |
Correct |
8 ms |
12700 KB |
Output is correct |
32 |
Correct |
6 ms |
12636 KB |
Output is correct |
33 |
Correct |
5 ms |
12636 KB |
Output is correct |
34 |
Correct |
10 ms |
12632 KB |
Output is correct |
35 |
Correct |
6 ms |
12632 KB |
Output is correct |
36 |
Correct |
9 ms |
12636 KB |
Output is correct |
37 |
Correct |
7 ms |
12632 KB |
Output is correct |
38 |
Correct |
4 ms |
12892 KB |
Output is correct |
39 |
Correct |
4 ms |
12628 KB |
Output is correct |
40 |
Correct |
4 ms |
12376 KB |
Output is correct |
41 |
Correct |
6 ms |
12376 KB |
Output is correct |
42 |
Correct |
4 ms |
12380 KB |
Output is correct |
43 |
Correct |
5 ms |
12380 KB |
Output is correct |
44 |
Correct |
4 ms |
12352 KB |
Output is correct |
45 |
Correct |
4 ms |
12376 KB |
Output is correct |
46 |
Correct |
4 ms |
12380 KB |
Output is correct |
47 |
Correct |
4 ms |
12380 KB |
Output is correct |
48 |
Correct |
5 ms |
12380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12124 KB |
Output is correct |
2 |
Correct |
2 ms |
12124 KB |
Output is correct |
3 |
Correct |
3 ms |
12124 KB |
Output is correct |
4 |
Correct |
3 ms |
12124 KB |
Output is correct |
5 |
Correct |
3 ms |
12208 KB |
Output is correct |
6 |
Correct |
2 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
12124 KB |
Output is correct |
9 |
Correct |
3 ms |
12124 KB |
Output is correct |
10 |
Correct |
3 ms |
12120 KB |
Output is correct |
11 |
Correct |
3 ms |
12120 KB |
Output is correct |
12 |
Correct |
3 ms |
12204 KB |
Output is correct |
13 |
Correct |
2 ms |
12000 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
3 ms |
12124 KB |
Output is correct |
16 |
Correct |
3 ms |
12120 KB |
Output is correct |
17 |
Correct |
5 ms |
12120 KB |
Output is correct |
18 |
Correct |
5 ms |
12124 KB |
Output is correct |
19 |
Correct |
4 ms |
12124 KB |
Output is correct |
20 |
Correct |
4 ms |
12124 KB |
Output is correct |
21 |
Correct |
4 ms |
12124 KB |
Output is correct |
22 |
Correct |
7 ms |
12224 KB |
Output is correct |
23 |
Correct |
5 ms |
12124 KB |
Output is correct |
24 |
Correct |
4 ms |
12124 KB |
Output is correct |
25 |
Correct |
6 ms |
12616 KB |
Output is correct |
26 |
Correct |
6 ms |
12636 KB |
Output is correct |
27 |
Correct |
6 ms |
12636 KB |
Output is correct |
28 |
Correct |
5 ms |
12620 KB |
Output is correct |
29 |
Correct |
6 ms |
12628 KB |
Output is correct |
30 |
Correct |
6 ms |
12636 KB |
Output is correct |
31 |
Correct |
8 ms |
12700 KB |
Output is correct |
32 |
Correct |
6 ms |
12636 KB |
Output is correct |
33 |
Correct |
5 ms |
12636 KB |
Output is correct |
34 |
Correct |
10 ms |
12632 KB |
Output is correct |
35 |
Correct |
6 ms |
12632 KB |
Output is correct |
36 |
Correct |
9 ms |
12636 KB |
Output is correct |
37 |
Correct |
7 ms |
12632 KB |
Output is correct |
38 |
Correct |
4 ms |
12892 KB |
Output is correct |
39 |
Correct |
4 ms |
12628 KB |
Output is correct |
40 |
Correct |
4 ms |
12376 KB |
Output is correct |
41 |
Correct |
6 ms |
12376 KB |
Output is correct |
42 |
Correct |
4 ms |
12380 KB |
Output is correct |
43 |
Correct |
5 ms |
12380 KB |
Output is correct |
44 |
Correct |
4 ms |
12352 KB |
Output is correct |
45 |
Correct |
4 ms |
12376 KB |
Output is correct |
46 |
Correct |
4 ms |
12380 KB |
Output is correct |
47 |
Correct |
4 ms |
12380 KB |
Output is correct |
48 |
Correct |
5 ms |
12380 KB |
Output is correct |
49 |
Correct |
234 ms |
67340 KB |
Output is correct |
50 |
Correct |
225 ms |
71772 KB |
Output is correct |
51 |
Correct |
218 ms |
75552 KB |
Output is correct |
52 |
Correct |
234 ms |
62424 KB |
Output is correct |
53 |
Correct |
200 ms |
71908 KB |
Output is correct |
54 |
Correct |
197 ms |
76112 KB |
Output is correct |
55 |
Correct |
190 ms |
66100 KB |
Output is correct |
56 |
Correct |
183 ms |
70376 KB |
Output is correct |
57 |
Correct |
186 ms |
76880 KB |
Output is correct |
58 |
Correct |
201 ms |
72016 KB |
Output is correct |
59 |
Correct |
215 ms |
71828 KB |
Output is correct |
60 |
Correct |
189 ms |
69460 KB |
Output is correct |
61 |
Correct |
336 ms |
105044 KB |
Output is correct |
62 |
Correct |
368 ms |
93780 KB |
Output is correct |
63 |
Correct |
356 ms |
60724 KB |
Output is correct |
64 |
Correct |
308 ms |
52632 KB |
Output is correct |
65 |
Correct |
352 ms |
47816 KB |
Output is correct |
66 |
Correct |
256 ms |
44964 KB |
Output is correct |
67 |
Correct |
359 ms |
43604 KB |
Output is correct |
68 |
Correct |
264 ms |
43088 KB |
Output is correct |
69 |
Correct |
277 ms |
42816 KB |
Output is correct |
70 |
Correct |
279 ms |
42576 KB |
Output is correct |
71 |
Correct |
300 ms |
42708 KB |
Output is correct |
72 |
Correct |
280 ms |
42580 KB |
Output is correct |
73 |
Correct |
255 ms |
42832 KB |
Output is correct |
74 |
Correct |
303 ms |
42720 KB |
Output is correct |
75 |
Correct |
305 ms |
43096 KB |
Output is correct |
76 |
Correct |
259 ms |
43352 KB |
Output is correct |
77 |
Correct |
251 ms |
44480 KB |
Output is correct |
78 |
Correct |
152 ms |
46772 KB |
Output is correct |