#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = (int)5e5 + 7;
int n;
pair < int, int > refresh(pair < int, int > A, pair < int, int > B) {
if (A.first < B.first) {
return B;
} else if (A.first > B.first) {
return A;
} else {
return {A.first, A.second + B.second};
}
}
struct T {
pair < int, int > tree[4 * N];
T() {
}
void upd(int pos, int val, int v = 1, int l = 1, int r = n) {
if (l == r) {
tree[v] = {val, 1};
return ;
}
int mid = (l + r) >> 1;
if (pos <= mid) {
upd(pos, val, v + v, l, mid);
} else {
upd(pos, val, v + v + 1, mid + 1, r);
}
tree[v] = refresh(tree[v + v], tree[v + v + 1]);
}
pair < int, int > getmax(int l, int r, int v = 1, int tl = 1, int tr = n) {
if (tl > r || tr < l) return {0, 0};
if (l <= tl && tr <= r) return tree[v];
int mid = (tl + tr) >> 1;
return refresh(getmax(l, r, v + v, tl, mid), getmax(l, r, v + v + 1, mid + 1, tr));
}
};
T tr;
vector < int > gr[N];
int tin[N], tout[N], tiktak;
int h[N];
pair < ll, ll > ans, ans1;
pair < int, int > asd;
void dfs(int v, int pr) {
tin[v] = ++tiktak;
tr.upd(tin[v], h[v]);
for (int to : gr[v]) {
if (to == pr) continue;
h[to] = h[v] + 1;
dfs(to, v);
}
tout[v] = tiktak;
}
void dfs1(int v, int pr, pair < int, int > mxx) {
pair < int, int > fir, sec;
for (int to : gr[v]) {
if (to == pr) continue;
pair < int, int > res = mxx;
fir = tr.getmax(tin[to], tout[to]);
sec = refresh(tr.getmax(tin[v] + 1, tin[to] - 1), tr.getmax(tout[to] + 1, tout[v]));
sec.first -= h[v];
fir.first -= h[v];
if (mxx.first > 0) {
if (sec.first > 0) {
if ((fir.first + sec.first) * 1LL * mxx.first > ans.first) {
ans.first = (fir.first + sec.first) * 1LL * mxx.first;
ans.second = fir.second * 1LL * sec.second;
} else if ((fir.first + sec.first) * 1LL * mxx.first == ans.first) {
ans.second += fir.second * 1LL * sec.second;
}
if ((fir.first + mxx.first) * 1LL * sec.first > ans1.first) {
ans1.first = (fir.first + mxx.first) * 1LL * sec.first;
ans1.second = fir.second * 1LL * mxx.second;
} else if ((fir.first + mxx.first) * 1LL * sec.first == ans1.first) {
ans1.second += fir.second * 1LL * mxx.second;
}
}
}
if (sec.first >= res.first) {
res = sec;
}
res.second = max(res.second, 1);
res.first++;
dfs1(to, v, res);
}
}
main() {
//freopen("road.in", "r", stdin);
//freopen("road.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
gr[u].push_back(v);
gr[v].push_back(u);
}
ans = ans1 = {0, 1};
dfs(1, 1);
dfs1(1, 1, {0, 1});
ans.second /= 2;
ans = refresh(ans, ans1);
cout << ans.first << ' ' << ans.second;
}
/*
8
1 2
2 3
3 4
2 5
5 6
6 8
5 7
*/
Compilation message
road.cpp:99:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
road.cpp: In function 'int main()':
road.cpp:102:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
road.cpp:105:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
27768 KB |
Output is correct |
2 |
Correct |
24 ms |
27768 KB |
Output is correct |
3 |
Correct |
24 ms |
27784 KB |
Output is correct |
4 |
Correct |
24 ms |
27768 KB |
Output is correct |
5 |
Correct |
24 ms |
27768 KB |
Output is correct |
6 |
Correct |
24 ms |
27768 KB |
Output is correct |
7 |
Correct |
24 ms |
27768 KB |
Output is correct |
8 |
Correct |
24 ms |
27768 KB |
Output is correct |
9 |
Correct |
24 ms |
27776 KB |
Output is correct |
10 |
Correct |
24 ms |
27828 KB |
Output is correct |
11 |
Correct |
25 ms |
27704 KB |
Output is correct |
12 |
Correct |
20 ms |
27768 KB |
Output is correct |
13 |
Correct |
20 ms |
27768 KB |
Output is correct |
14 |
Correct |
20 ms |
27768 KB |
Output is correct |
15 |
Correct |
21 ms |
27768 KB |
Output is correct |
16 |
Correct |
24 ms |
27768 KB |
Output is correct |
17 |
Correct |
23 ms |
27768 KB |
Output is correct |
18 |
Correct |
24 ms |
27768 KB |
Output is correct |
19 |
Correct |
24 ms |
27768 KB |
Output is correct |
20 |
Correct |
24 ms |
27768 KB |
Output is correct |
21 |
Correct |
24 ms |
27768 KB |
Output is correct |
22 |
Correct |
24 ms |
27664 KB |
Output is correct |
23 |
Correct |
21 ms |
27768 KB |
Output is correct |
24 |
Correct |
24 ms |
27796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
27768 KB |
Output is correct |
2 |
Correct |
24 ms |
27768 KB |
Output is correct |
3 |
Correct |
24 ms |
27784 KB |
Output is correct |
4 |
Correct |
24 ms |
27768 KB |
Output is correct |
5 |
Correct |
24 ms |
27768 KB |
Output is correct |
6 |
Correct |
24 ms |
27768 KB |
Output is correct |
7 |
Correct |
24 ms |
27768 KB |
Output is correct |
8 |
Correct |
24 ms |
27768 KB |
Output is correct |
9 |
Correct |
24 ms |
27776 KB |
Output is correct |
10 |
Correct |
24 ms |
27828 KB |
Output is correct |
11 |
Correct |
25 ms |
27704 KB |
Output is correct |
12 |
Correct |
20 ms |
27768 KB |
Output is correct |
13 |
Correct |
20 ms |
27768 KB |
Output is correct |
14 |
Correct |
20 ms |
27768 KB |
Output is correct |
15 |
Correct |
21 ms |
27768 KB |
Output is correct |
16 |
Correct |
24 ms |
27768 KB |
Output is correct |
17 |
Correct |
23 ms |
27768 KB |
Output is correct |
18 |
Correct |
24 ms |
27768 KB |
Output is correct |
19 |
Correct |
24 ms |
27768 KB |
Output is correct |
20 |
Correct |
24 ms |
27768 KB |
Output is correct |
21 |
Correct |
24 ms |
27768 KB |
Output is correct |
22 |
Correct |
24 ms |
27664 KB |
Output is correct |
23 |
Correct |
21 ms |
27768 KB |
Output is correct |
24 |
Correct |
24 ms |
27796 KB |
Output is correct |
25 |
Correct |
28 ms |
28280 KB |
Output is correct |
26 |
Correct |
28 ms |
28408 KB |
Output is correct |
27 |
Correct |
28 ms |
28380 KB |
Output is correct |
28 |
Correct |
29 ms |
28408 KB |
Output is correct |
29 |
Correct |
28 ms |
28280 KB |
Output is correct |
30 |
Correct |
29 ms |
28408 KB |
Output is correct |
31 |
Correct |
28 ms |
28384 KB |
Output is correct |
32 |
Correct |
29 ms |
28280 KB |
Output is correct |
33 |
Correct |
28 ms |
28372 KB |
Output is correct |
34 |
Correct |
28 ms |
28408 KB |
Output is correct |
35 |
Correct |
29 ms |
28408 KB |
Output is correct |
36 |
Correct |
27 ms |
28408 KB |
Output is correct |
37 |
Correct |
27 ms |
28408 KB |
Output is correct |
38 |
Correct |
30 ms |
28792 KB |
Output is correct |
39 |
Correct |
29 ms |
28280 KB |
Output is correct |
40 |
Correct |
29 ms |
28152 KB |
Output is correct |
41 |
Correct |
27 ms |
28024 KB |
Output is correct |
42 |
Correct |
28 ms |
28024 KB |
Output is correct |
43 |
Correct |
28 ms |
28024 KB |
Output is correct |
44 |
Correct |
28 ms |
28008 KB |
Output is correct |
45 |
Correct |
28 ms |
28024 KB |
Output is correct |
46 |
Correct |
28 ms |
27948 KB |
Output is correct |
47 |
Correct |
28 ms |
28028 KB |
Output is correct |
48 |
Correct |
28 ms |
28024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
27768 KB |
Output is correct |
2 |
Correct |
24 ms |
27768 KB |
Output is correct |
3 |
Correct |
24 ms |
27784 KB |
Output is correct |
4 |
Correct |
24 ms |
27768 KB |
Output is correct |
5 |
Correct |
24 ms |
27768 KB |
Output is correct |
6 |
Correct |
24 ms |
27768 KB |
Output is correct |
7 |
Correct |
24 ms |
27768 KB |
Output is correct |
8 |
Correct |
24 ms |
27768 KB |
Output is correct |
9 |
Correct |
24 ms |
27776 KB |
Output is correct |
10 |
Correct |
24 ms |
27828 KB |
Output is correct |
11 |
Correct |
25 ms |
27704 KB |
Output is correct |
12 |
Correct |
20 ms |
27768 KB |
Output is correct |
13 |
Correct |
20 ms |
27768 KB |
Output is correct |
14 |
Correct |
20 ms |
27768 KB |
Output is correct |
15 |
Correct |
21 ms |
27768 KB |
Output is correct |
16 |
Correct |
24 ms |
27768 KB |
Output is correct |
17 |
Correct |
23 ms |
27768 KB |
Output is correct |
18 |
Correct |
24 ms |
27768 KB |
Output is correct |
19 |
Correct |
24 ms |
27768 KB |
Output is correct |
20 |
Correct |
24 ms |
27768 KB |
Output is correct |
21 |
Correct |
24 ms |
27768 KB |
Output is correct |
22 |
Correct |
24 ms |
27664 KB |
Output is correct |
23 |
Correct |
21 ms |
27768 KB |
Output is correct |
24 |
Correct |
24 ms |
27796 KB |
Output is correct |
25 |
Correct |
28 ms |
28280 KB |
Output is correct |
26 |
Correct |
28 ms |
28408 KB |
Output is correct |
27 |
Correct |
28 ms |
28380 KB |
Output is correct |
28 |
Correct |
29 ms |
28408 KB |
Output is correct |
29 |
Correct |
28 ms |
28280 KB |
Output is correct |
30 |
Correct |
29 ms |
28408 KB |
Output is correct |
31 |
Correct |
28 ms |
28384 KB |
Output is correct |
32 |
Correct |
29 ms |
28280 KB |
Output is correct |
33 |
Correct |
28 ms |
28372 KB |
Output is correct |
34 |
Correct |
28 ms |
28408 KB |
Output is correct |
35 |
Correct |
29 ms |
28408 KB |
Output is correct |
36 |
Correct |
27 ms |
28408 KB |
Output is correct |
37 |
Correct |
27 ms |
28408 KB |
Output is correct |
38 |
Correct |
30 ms |
28792 KB |
Output is correct |
39 |
Correct |
29 ms |
28280 KB |
Output is correct |
40 |
Correct |
29 ms |
28152 KB |
Output is correct |
41 |
Correct |
27 ms |
28024 KB |
Output is correct |
42 |
Correct |
28 ms |
28024 KB |
Output is correct |
43 |
Correct |
28 ms |
28024 KB |
Output is correct |
44 |
Correct |
28 ms |
28008 KB |
Output is correct |
45 |
Correct |
28 ms |
28024 KB |
Output is correct |
46 |
Correct |
28 ms |
27948 KB |
Output is correct |
47 |
Correct |
28 ms |
28028 KB |
Output is correct |
48 |
Correct |
28 ms |
28024 KB |
Output is correct |
49 |
Correct |
1134 ms |
85844 KB |
Output is correct |
50 |
Correct |
1123 ms |
91096 KB |
Output is correct |
51 |
Correct |
1148 ms |
95964 KB |
Output is correct |
52 |
Correct |
1095 ms |
79388 KB |
Output is correct |
53 |
Incorrect |
995 ms |
88392 KB |
Output isn't correct |
54 |
Halted |
0 ms |
0 KB |
- |