Submission #64791673
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef long long i64;
int main() {
cin.tie(nullptr) -> sync_with_stdio(false);
// freopen("x.in", "r", stdin);
// freopen("x.out", "w", stdout);
int n1, n2;
cin >> n1;
vector<VI> G1(n1 + 1);
for(int i = 1, u, v; i < n1; i++) {
cin >> u >> v;
G1[u].push_back(v), G1[v].push_back(u);
}
cin >> n2;
vector<VI> G2(n2 + 1);
for(int i = 1, u, v; i < n2; i++) {
cin >> u >> v;
G2[u].push_back(v), G2[v].push_back(u);
}
int d1 = 0, d2 = 0;
VI h1(n1 + 1), h2(n2 + 1);
auto prework = [&](vector<VI> &G, int &d, VI &h) {
// 计算直径和树的高度 h
int s;
function<void(int, int, int)> dfs = [&](int u, int fa, int dis) {
if(dis > d) {
d = dis, s = u;
}
for(int v: G[u]) {
if(v != fa) {
dfs(v, u, dis + 1);
}
}
};
dfs(1, 0, 0), d = 0, dfs(s, 0, 0);
function<void(int, int)> dp = [&](int u, int fa) {
for(int v: G[u]) {
if(v == fa) continue;
dp(v, u);
h[u] = max(h[u], h[v] + 1);
}
};
dp(1, 0);
};
prework(G1, d1, h1);
prework(G2, d2, h2);
// cerr << "d1 = " << d1 << ", d2 = " << d2 << '\n';
// for(int i = 1; i <= n1; i++) {
// cerr << h1[i] << " \n"[i == n1];
// }
// for(int i = 1; i <= n2; i++) {
// cerr << h2[i] << " \n"[i == n2];
// }
VI f(n1 + 1), g(n2 + 1);
// f[u]: T1 中离 u 最远的点到 u 的距离
// g[u]: T2 中离 u 最远的点到 u 的距离
auto calc = [&](vector<VI> &G, VI &ht, VI &dis) {
auto del = [&](int u, int v) {
ht[u] = 0;
for(int _: G[u]) {
if(_ == v) continue;
ht[u] = max(ht[u], ht[_] + 1);
}
};
auto add = [&](int u, int v) {
ht[u] = max(ht[u], ht[v] + 1);
// cerr << "after add: " << u << ' ' << v << ": ht = " << ht[u] << '\n';
};
function<void(int, int)> dp2 = [&](int u, int fa) {
dis[u] = ht[u];
// cerr << "In dp2: dis[" << u << "] = " << dis[u] << '\n';
multiset<int> sonh;
for(int v: G[u]) {
sonh.insert(ht[v]);
}
for(int v: G[u]) {
if(v == fa) continue;
// del(u, v);
sonh.erase(sonh.find(ht[v]));
int old = ht[v];
if(sonh.empty()) {
ht[u] = 0;
} else {
ht[u] = 1 + *sonh.rbegin();
}
// cerr << "v = " << v << ", ht[u] = " << ht[u] << '\n';
add(v, u);
dp2(v, u);
// del(v, u);
ht[v] = old;
sonh.insert(old);
// add(u, v);
if(sonh.empty()) {
ht[u] = 0;
} else {
ht[u] = 1 + *sonh.rbegin();
}
}
};
dp2(1, 0);
// for(int i = 1; i < (int)dis.size(); i++) {
// cerr << dis[i] << ' ';
// }
// cerr << '\n';
};
calc(G1, h1, f), calc(G2, h2, g);
sort(g.begin() + 1, g.end());
// cerr << "OK\n";
vector<i64> sum(n2 + 1);
for(int i = 1; i <= n2; i++) {
sum[i] = g[i] + sum[i - 1];
}
// for(i64 x: sum) { cerr << x << " "; } cerr << '\n';
int d = max(d1, d2);
i64 ans = 0;
for(int i = 1; i <= n1; i++) {
int x = d - (1 + f[i]);
// g[v] > x 时,直径跨越两棵树
auto it = upper_bound(g.begin() + 1, g.end(), x);
int p = (int)(it - g.begin());
i64 add = 1LL * (p - 1) * d;
if(p != n2 + 1) {
add += (sum[n2] - sum[p - 1]) + 1LL * (n2 - p + 1) * (f[i] + 1);
}
ans += add;
// cerr << i << ' ' << p << ' ' << add << '\n';
}
cout << ans << '\n';
return 0;
}
Submission Info
Submission Time
2025-04-12 22:36:46+0900
Task
F - Add One Edge 3
User
dengstar
Language
C++ 20 (gcc 12.2)
Score
500
Code Size
4333 Byte
Status
AC
Exec Time
329 ms
Memory
78324 KiB
Compile Error
Main.cpp: In lambda function:
Main.cpp:72:14: warning: variable ‘del’ set but not used [-Wunused-but-set-variable]
72 | auto del = [&](int u, int v) {
| ^~~
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
500 / 500
Status
Set Name
Test Cases
Sample
00_sample_00.txt, 00_sample_01.txt
All
00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt
Case Name
Status
Exec Time
Memory
00_sample_00.txt
AC
1 ms
3444 KiB
00_sample_01.txt
AC
1 ms
3500 KiB
01_random_00.txt
AC
234 ms
29992 KiB
01_random_01.txt
AC
40 ms
9552 KiB
01_random_02.txt
AC
21 ms
6516 KiB
01_random_03.txt
AC
3 ms
3860 KiB
01_random_04.txt
AC
203 ms
27820 KiB
01_random_05.txt
AC
120 ms
18628 KiB
01_random_06.txt
AC
83 ms
15116 KiB
01_random_07.txt
AC
68 ms
13008 KiB
01_random_08.txt
AC
92 ms
15756 KiB
01_random_09.txt
AC
72 ms
13556 KiB
01_random_10.txt
AC
255 ms
38932 KiB
01_random_11.txt
AC
94 ms
20760 KiB
01_random_12.txt
AC
191 ms
32652 KiB
01_random_13.txt
AC
36 ms
10796 KiB
01_random_14.txt
AC
189 ms
32816 KiB
01_random_15.txt
AC
53 ms
14776 KiB
01_random_16.txt
AC
138 ms
27112 KiB
01_random_17.txt
AC
88 ms
20016 KiB
01_random_18.txt
AC
251 ms
38856 KiB
01_random_19.txt
AC
182 ms
31584 KiB
01_random_20.txt
AC
244 ms
29784 KiB
01_random_21.txt
AC
126 ms
20476 KiB
01_random_22.txt
AC
46 ms
9928 KiB
01_random_23.txt
AC
48 ms
9928 KiB
01_random_24.txt
AC
101 ms
16760 KiB
01_random_25.txt
AC
110 ms
18136 KiB
01_random_26.txt
AC
121 ms
19664 KiB
01_random_27.txt
AC
105 ms
16144 KiB
01_random_28.txt
AC
94 ms
16288 KiB
01_random_29.txt
AC
46 ms
10176 KiB
01_random_30.txt
AC
239 ms
78096 KiB
01_random_31.txt
AC
228 ms
76632 KiB
01_random_32.txt
AC
150 ms
60588 KiB
01_random_33.txt
AC
180 ms
64016 KiB
01_random_34.txt
AC
106 ms
49000 KiB
01_random_35.txt
AC
42 ms
21700 KiB
01_random_36.txt
AC
89 ms
40608 KiB
01_random_37.txt
AC
60 ms
25784 KiB
01_random_38.txt
AC
70 ms
38496 KiB
01_random_39.txt
AC
70 ms
29948 KiB
01_random_40.txt
AC
248 ms
78324 KiB
01_random_41.txt
AC
290 ms
78092 KiB
01_random_42.txt
AC
329 ms
63960 KiB
01_random_43.txt
AC
322 ms
58756 KiB
01_random_44.txt
AC
285 ms
49028 KiB
01_random_45.txt
AC
1 ms
3512 KiB
01_random_46.txt
AC
133 ms
65680 KiB