#include <bits/stdc++.h>
using namespace std;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;
const int MXN = 5005;
int n;
vector<int> edge[MXN];
pii dp[MXN][2];
struct STACK {
vector<int> v;
void init() {
v.clear();
v.push_back(0);
}
void push(int x) {
v.push_back(max(v.back(), x));
}
void pop() {
v.pop_back();
}
int get() {
return v.back();
}
} st;
pii operator+(pii a, pii b) {
if (a < b) swap(a, b);
if (a.fs == b.fs) a.sc += b.sc;
return a;
}
pii &operator+=(pii &a, pii b) {
return (a = a + b);
}
void push_pii(int id, pii p) {
if (p > dp[id][0]) {
dp[id][1] = dp[id][0];
dp[id][0] = p;
} else dp[id][1] = max(dp[id][1], p);
}
void DFS_PRE(int id, int par) {
dp[id][0] = mp(0LL, 0LL);
dp[id][1] = mp(0LL, 0LL);
for (auto &i : edge[id]) {
if (i == par) continue;
DFS_PRE(i, id);
push_pii(id, mp(1 + dp[i][0].fs, i));
}
}
void DFS(int id, int par, int dep, pii &ans) {
if (edge[id].size() == 1) {
ans = ans + mp(dep * st.get(), 1LL);
return;
}
st.push(dp[id][1].fs);
DFS(dp[id][0].sc, id, dep + 1, ans);
st.pop();
st.push(dp[id][0].fs);
for (auto &i : edge[id]) {
if (i == par) continue;
if (i == dp[id][0].sc) continue;
DFS(i, id, dep + 1, ans);
}
st.pop();
}
pii solve(int rt) {
DFS_PRE(rt, 0);
st.init();
pii ans = mp(0LL, 0LL);
for (auto &i : edge[rt]) {
DFS(i, rt, 1, ans);
}
return ans;
}
void miku() {
int x, y;
cin >> n;
FOR(i, 1, n) {
cin >> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
pii ans = mp(0LL, 0LL);
FOR(i, 1, n + 1) {
if (edge[i].size() >= 2) continue;
ans = ans + solve(i);
}
cout << ans.fs << ' ' << ans.sc / 2 << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
840 KB |
Output is correct |
2 |
Correct |
1 ms |
580 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
584 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
584 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
840 KB |
Output is correct |
2 |
Correct |
1 ms |
580 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
584 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
584 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
164 ms |
1232 KB |
Output is correct |
26 |
Correct |
166 ms |
1116 KB |
Output is correct |
27 |
Correct |
173 ms |
1116 KB |
Output is correct |
28 |
Correct |
166 ms |
1220 KB |
Output is correct |
29 |
Correct |
215 ms |
1116 KB |
Output is correct |
30 |
Correct |
256 ms |
1244 KB |
Output is correct |
31 |
Correct |
215 ms |
1248 KB |
Output is correct |
32 |
Correct |
223 ms |
1116 KB |
Output is correct |
33 |
Correct |
4 ms |
1112 KB |
Output is correct |
34 |
Correct |
3 ms |
1116 KB |
Output is correct |
35 |
Correct |
3 ms |
1356 KB |
Output is correct |
36 |
Correct |
3 ms |
1100 KB |
Output is correct |
37 |
Correct |
2 ms |
1372 KB |
Output is correct |
38 |
Correct |
2 ms |
1372 KB |
Output is correct |
39 |
Correct |
4 ms |
1116 KB |
Output is correct |
40 |
Correct |
4 ms |
860 KB |
Output is correct |
41 |
Correct |
7 ms |
856 KB |
Output is correct |
42 |
Correct |
11 ms |
848 KB |
Output is correct |
43 |
Correct |
19 ms |
860 KB |
Output is correct |
44 |
Correct |
46 ms |
1108 KB |
Output is correct |
45 |
Correct |
82 ms |
856 KB |
Output is correct |
46 |
Correct |
96 ms |
860 KB |
Output is correct |
47 |
Correct |
258 ms |
860 KB |
Output is correct |
48 |
Correct |
298 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
840 KB |
Output is correct |
2 |
Correct |
1 ms |
580 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
584 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
584 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
164 ms |
1232 KB |
Output is correct |
26 |
Correct |
166 ms |
1116 KB |
Output is correct |
27 |
Correct |
173 ms |
1116 KB |
Output is correct |
28 |
Correct |
166 ms |
1220 KB |
Output is correct |
29 |
Correct |
215 ms |
1116 KB |
Output is correct |
30 |
Correct |
256 ms |
1244 KB |
Output is correct |
31 |
Correct |
215 ms |
1248 KB |
Output is correct |
32 |
Correct |
223 ms |
1116 KB |
Output is correct |
33 |
Correct |
4 ms |
1112 KB |
Output is correct |
34 |
Correct |
3 ms |
1116 KB |
Output is correct |
35 |
Correct |
3 ms |
1356 KB |
Output is correct |
36 |
Correct |
3 ms |
1100 KB |
Output is correct |
37 |
Correct |
2 ms |
1372 KB |
Output is correct |
38 |
Correct |
2 ms |
1372 KB |
Output is correct |
39 |
Correct |
4 ms |
1116 KB |
Output is correct |
40 |
Correct |
4 ms |
860 KB |
Output is correct |
41 |
Correct |
7 ms |
856 KB |
Output is correct |
42 |
Correct |
11 ms |
848 KB |
Output is correct |
43 |
Correct |
19 ms |
860 KB |
Output is correct |
44 |
Correct |
46 ms |
1108 KB |
Output is correct |
45 |
Correct |
82 ms |
856 KB |
Output is correct |
46 |
Correct |
96 ms |
860 KB |
Output is correct |
47 |
Correct |
258 ms |
860 KB |
Output is correct |
48 |
Correct |
298 ms |
856 KB |
Output is correct |
49 |
Runtime error |
1 ms |
856 KB |
Execution killed with signal 11 |
50 |
Halted |
0 ms |
0 KB |
- |