#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define c2(x) (x*(x-1))/2
struct dp{
ll tot, sep, badComb, d;
};
const int mx = 5e5+5;
int n, root = -1; vector<int> adj[mx];
dp in[mx][3], out[mx]; pair<ll, ll> ans;
void updIn(int c, int val, int inc){
if (val == in[c][0].d) in[c][0].tot += inc, in[c][0].sep++, in[c][0].badComb += c2(inc);
else if (val == in[c][1].d) in[c][1].tot += inc, in[c][1].sep++, in[c][1].badComb += c2(inc);
else if (val == in[c][2].d) in[c][2].tot += inc, in[c][2].sep++, in[c][2].badComb += c2(inc);
else if (val > in[c][0].d) in[c][2] = in[c][1], in[c][1] = in[c][0], in[c][0] = {inc, 1, c2(inc), val};
else if (val > in[c][1].d) in[c][2] = in[c][1], in[c][1] = {inc, 1, c2(inc), val};
else if (val > in[c][2].d) in[c][2] = {inc, 1, c2(inc), val};
}
void updOut(int node, int nxt){
dp cmpIn = in[node][0];
if (in[nxt][0].d+1 == in[node][0].d and in[node][0].sep == 1) cmpIn = in[node][1];
if (cmpIn.d > out[node].d) out[nxt] = {cmpIn.tot, 1, c2(cmpIn.tot), cmpIn.d+1};
else if (cmpIn.d < out[node].d) out[nxt] = {out[node].tot, 1, c2(out[node].tot), out[node].d+1};
else out[nxt] = {cmpIn.tot+out[node].tot, 1, c2(cmpIn.tot+out[node].tot), out[node].d+1};
}
void updAns(ll val, ll cnt){
if (val > ans.first) ans = {val, cnt};
else if (val == ans.first) ans.second += cnt;
}
void dfs1(int node, int p){
for (int nxt : adj[node])
if (nxt != p)
dfs1(nxt, node), updIn(node, in[nxt][0].d+1, in[nxt][0].tot);
if (adj[node].size() == 1) in[node][0].tot = 1;
}
void dfs2(int node, int p){
for (int nxt : adj[node])
if (nxt != p)
updOut(node, nxt), dfs2(nxt, node);
}
void solve(int node, int p){
dp cmp[3] = {in[node][0], in[node][1], in[node][2]};
for (int i = 0; i < 3; i++){
if (out[node].d == cmp[i].d and out[node].tot != 0){
cmp[i].tot += out[node].tot; cmp[i].sep += 1; cmp[i].badComb += out[node].badComb;
break;
}
if (out[node].d > cmp[i].d){
rotate(cmp+i, cmp+2, cmp+3); cmp[i] = out[node];
break;
}
}
if (cmp[0].sep > 2) updAns(cmp[0].d*(cmp[0].d*2), c2(cmp[0].tot)-cmp[0].badComb);
else if (cmp[0].sep == 2 and cmp[1].sep != 0) updAns(cmp[0].d*(cmp[0].d+cmp[1].d), cmp[0].tot*cmp[1].tot);
else if (cmp[0].sep == 1 and cmp[1].sep > 1) updAns(cmp[0].d*(cmp[1].d*2), c2(cmp[1].tot)-cmp[1].badComb);
else if (cmp[0].sep == 1 and cmp[1].sep == 1 and cmp[2].sep != 0) updAns(cmp[0].d*(cmp[1].d+cmp[2].d), cmp[1].tot*cmp[2].tot);
for (int nxt : adj[node])
if (nxt != p)
solve(nxt, node);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 0; i < n-1; i++){
int a, b; cin >> a >> b; a--; b--;
adj[a].push_back(b); adj[b].push_back(a);
}
for (int i = 0; i < n; i++) if (adj[i].size() > 2) { root = i; break; }
if (root == -1) { cout<<0<<" "<<1; return 0; }
fill(out, out+mx, dp({0, 0, -LLONG_MAX}));
dfs1(root, -1);
dfs2(root, -1);
solve(root, -1);
cout<<ans.first<<" "<<ans.second<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
27608 KB |
Output is correct |
2 |
Correct |
17 ms |
27704 KB |
Output is correct |
3 |
Correct |
10 ms |
12056 KB |
Output is correct |
4 |
Correct |
8 ms |
11980 KB |
Output is correct |
5 |
Correct |
9 ms |
11980 KB |
Output is correct |
6 |
Correct |
9 ms |
12072 KB |
Output is correct |
7 |
Correct |
15 ms |
27724 KB |
Output is correct |
8 |
Correct |
29 ms |
27680 KB |
Output is correct |
9 |
Correct |
18 ms |
27636 KB |
Output is correct |
10 |
Correct |
19 ms |
27672 KB |
Output is correct |
11 |
Correct |
22 ms |
27676 KB |
Output is correct |
12 |
Correct |
21 ms |
27684 KB |
Output is correct |
13 |
Correct |
16 ms |
27736 KB |
Output is correct |
14 |
Correct |
21 ms |
27684 KB |
Output is correct |
15 |
Correct |
17 ms |
27672 KB |
Output is correct |
16 |
Correct |
18 ms |
27728 KB |
Output is correct |
17 |
Correct |
14 ms |
27728 KB |
Output is correct |
18 |
Correct |
16 ms |
27724 KB |
Output is correct |
19 |
Correct |
9 ms |
12068 KB |
Output is correct |
20 |
Correct |
8 ms |
11980 KB |
Output is correct |
21 |
Correct |
15 ms |
27728 KB |
Output is correct |
22 |
Correct |
16 ms |
27724 KB |
Output is correct |
23 |
Correct |
14 ms |
27628 KB |
Output is correct |
24 |
Correct |
15 ms |
27684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
27608 KB |
Output is correct |
2 |
Correct |
17 ms |
27704 KB |
Output is correct |
3 |
Correct |
10 ms |
12056 KB |
Output is correct |
4 |
Correct |
8 ms |
11980 KB |
Output is correct |
5 |
Correct |
9 ms |
11980 KB |
Output is correct |
6 |
Correct |
9 ms |
12072 KB |
Output is correct |
7 |
Correct |
15 ms |
27724 KB |
Output is correct |
8 |
Correct |
29 ms |
27680 KB |
Output is correct |
9 |
Correct |
18 ms |
27636 KB |
Output is correct |
10 |
Correct |
19 ms |
27672 KB |
Output is correct |
11 |
Correct |
22 ms |
27676 KB |
Output is correct |
12 |
Correct |
21 ms |
27684 KB |
Output is correct |
13 |
Correct |
16 ms |
27736 KB |
Output is correct |
14 |
Correct |
21 ms |
27684 KB |
Output is correct |
15 |
Correct |
17 ms |
27672 KB |
Output is correct |
16 |
Correct |
18 ms |
27728 KB |
Output is correct |
17 |
Correct |
14 ms |
27728 KB |
Output is correct |
18 |
Correct |
16 ms |
27724 KB |
Output is correct |
19 |
Correct |
9 ms |
12068 KB |
Output is correct |
20 |
Correct |
8 ms |
11980 KB |
Output is correct |
21 |
Correct |
15 ms |
27728 KB |
Output is correct |
22 |
Correct |
16 ms |
27724 KB |
Output is correct |
23 |
Correct |
14 ms |
27628 KB |
Output is correct |
24 |
Correct |
15 ms |
27684 KB |
Output is correct |
25 |
Correct |
20 ms |
28832 KB |
Output is correct |
26 |
Correct |
21 ms |
28836 KB |
Output is correct |
27 |
Correct |
20 ms |
28876 KB |
Output is correct |
28 |
Correct |
19 ms |
28872 KB |
Output is correct |
29 |
Correct |
19 ms |
28680 KB |
Output is correct |
30 |
Correct |
20 ms |
28872 KB |
Output is correct |
31 |
Correct |
20 ms |
28740 KB |
Output is correct |
32 |
Correct |
20 ms |
28720 KB |
Output is correct |
33 |
Correct |
21 ms |
28844 KB |
Output is correct |
34 |
Correct |
21 ms |
28844 KB |
Output is correct |
35 |
Correct |
21 ms |
28928 KB |
Output is correct |
36 |
Correct |
17 ms |
28840 KB |
Output is correct |
37 |
Correct |
10 ms |
12236 KB |
Output is correct |
38 |
Correct |
13 ms |
12236 KB |
Output is correct |
39 |
Correct |
21 ms |
28588 KB |
Output is correct |
40 |
Correct |
22 ms |
28396 KB |
Output is correct |
41 |
Correct |
25 ms |
28320 KB |
Output is correct |
42 |
Correct |
21 ms |
28404 KB |
Output is correct |
43 |
Correct |
18 ms |
28396 KB |
Output is correct |
44 |
Correct |
20 ms |
28316 KB |
Output is correct |
45 |
Correct |
20 ms |
28200 KB |
Output is correct |
46 |
Correct |
19 ms |
28236 KB |
Output is correct |
47 |
Correct |
17 ms |
28392 KB |
Output is correct |
48 |
Correct |
18 ms |
28328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
27608 KB |
Output is correct |
2 |
Correct |
17 ms |
27704 KB |
Output is correct |
3 |
Correct |
10 ms |
12056 KB |
Output is correct |
4 |
Correct |
8 ms |
11980 KB |
Output is correct |
5 |
Correct |
9 ms |
11980 KB |
Output is correct |
6 |
Correct |
9 ms |
12072 KB |
Output is correct |
7 |
Correct |
15 ms |
27724 KB |
Output is correct |
8 |
Correct |
29 ms |
27680 KB |
Output is correct |
9 |
Correct |
18 ms |
27636 KB |
Output is correct |
10 |
Correct |
19 ms |
27672 KB |
Output is correct |
11 |
Correct |
22 ms |
27676 KB |
Output is correct |
12 |
Correct |
21 ms |
27684 KB |
Output is correct |
13 |
Correct |
16 ms |
27736 KB |
Output is correct |
14 |
Correct |
21 ms |
27684 KB |
Output is correct |
15 |
Correct |
17 ms |
27672 KB |
Output is correct |
16 |
Correct |
18 ms |
27728 KB |
Output is correct |
17 |
Correct |
14 ms |
27728 KB |
Output is correct |
18 |
Correct |
16 ms |
27724 KB |
Output is correct |
19 |
Correct |
9 ms |
12068 KB |
Output is correct |
20 |
Correct |
8 ms |
11980 KB |
Output is correct |
21 |
Correct |
15 ms |
27728 KB |
Output is correct |
22 |
Correct |
16 ms |
27724 KB |
Output is correct |
23 |
Correct |
14 ms |
27628 KB |
Output is correct |
24 |
Correct |
15 ms |
27684 KB |
Output is correct |
25 |
Correct |
20 ms |
28832 KB |
Output is correct |
26 |
Correct |
21 ms |
28836 KB |
Output is correct |
27 |
Correct |
20 ms |
28876 KB |
Output is correct |
28 |
Correct |
19 ms |
28872 KB |
Output is correct |
29 |
Correct |
19 ms |
28680 KB |
Output is correct |
30 |
Correct |
20 ms |
28872 KB |
Output is correct |
31 |
Correct |
20 ms |
28740 KB |
Output is correct |
32 |
Correct |
20 ms |
28720 KB |
Output is correct |
33 |
Correct |
21 ms |
28844 KB |
Output is correct |
34 |
Correct |
21 ms |
28844 KB |
Output is correct |
35 |
Correct |
21 ms |
28928 KB |
Output is correct |
36 |
Correct |
17 ms |
28840 KB |
Output is correct |
37 |
Correct |
10 ms |
12236 KB |
Output is correct |
38 |
Correct |
13 ms |
12236 KB |
Output is correct |
39 |
Correct |
21 ms |
28588 KB |
Output is correct |
40 |
Correct |
22 ms |
28396 KB |
Output is correct |
41 |
Correct |
25 ms |
28320 KB |
Output is correct |
42 |
Correct |
21 ms |
28404 KB |
Output is correct |
43 |
Correct |
18 ms |
28396 KB |
Output is correct |
44 |
Correct |
20 ms |
28316 KB |
Output is correct |
45 |
Correct |
20 ms |
28200 KB |
Output is correct |
46 |
Correct |
19 ms |
28236 KB |
Output is correct |
47 |
Correct |
17 ms |
28392 KB |
Output is correct |
48 |
Correct |
18 ms |
28328 KB |
Output is correct |
49 |
Correct |
739 ms |
152612 KB |
Output is correct |
50 |
Correct |
713 ms |
152460 KB |
Output is correct |
51 |
Correct |
819 ms |
152476 KB |
Output is correct |
52 |
Correct |
868 ms |
152476 KB |
Output is correct |
53 |
Correct |
619 ms |
151820 KB |
Output is correct |
54 |
Correct |
769 ms |
147564 KB |
Output is correct |
55 |
Correct |
619 ms |
135568 KB |
Output is correct |
56 |
Correct |
560 ms |
124964 KB |
Output is correct |
57 |
Correct |
763 ms |
152228 KB |
Output is correct |
58 |
Correct |
739 ms |
152232 KB |
Output is correct |
59 |
Correct |
752 ms |
152228 KB |
Output is correct |
60 |
Correct |
624 ms |
152228 KB |
Output is correct |
61 |
Correct |
501 ms |
34880 KB |
Output is correct |
62 |
Correct |
430 ms |
34748 KB |
Output is correct |
63 |
Correct |
1346 ms |
119340 KB |
Output is correct |
64 |
Correct |
1241 ms |
108464 KB |
Output is correct |
65 |
Correct |
1110 ms |
102908 KB |
Output is correct |
66 |
Correct |
1208 ms |
99780 KB |
Output is correct |
67 |
Correct |
1335 ms |
98532 KB |
Output is correct |
68 |
Correct |
892 ms |
97956 KB |
Output is correct |
69 |
Correct |
1035 ms |
97592 KB |
Output is correct |
70 |
Correct |
923 ms |
97420 KB |
Output is correct |
71 |
Correct |
1142 ms |
97252 KB |
Output is correct |
72 |
Correct |
1067 ms |
97472 KB |
Output is correct |
73 |
Correct |
1052 ms |
97624 KB |
Output is correct |
74 |
Correct |
1139 ms |
97536 KB |
Output is correct |
75 |
Correct |
1110 ms |
97668 KB |
Output is correct |
76 |
Correct |
1198 ms |
97652 KB |
Output is correct |
77 |
Correct |
797 ms |
98252 KB |
Output is correct |
78 |
Correct |
527 ms |
99524 KB |
Output is correct |