Submission #64792856
Source Code Expand
Copy
#include <bits/stdc++.h>using namespace std;typedef long long ll;vector<int> bfs(int start, const vector<vector<int>> &g) {int n = g.size();vector<int> dist(n, -1);queue<int> q;dist[start] = 0;q.push(start);while(!q.empty()){int cur = q.front();q.pop();for (int nx : g[cur]){if(dist[nx] == -1){dist[nx] = dist[cur] + 1;q.push(nx);}}}return dist;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> bfs(int start, const vector<vector<int>> &g) {
int n = g.size();
vector<int> dist(n, -1);
queue<int> q;
dist[start] = 0;
q.push(start);
while(!q.empty()){
int cur = q.front();
q.pop();
for (int nx : g[cur]){
if(dist[nx] == -1){
dist[nx] = dist[cur] + 1;
q.push(nx);
}
}
}
return dist;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N1;
cin >> N1;
vector<vector<int>> tree1(N1+1);
for (int i = 1; i <= N1-1; i++){
int u, v;
cin >> u >> v;
tree1[u].push_back(v);
tree1[v].push_back(u);
}
int N2;
cin >> N2;
vector<vector<int>> tree2(N2+1);
for (int i = 1; i <= N2-1; i++){
int u, v; cin >> u >> v;
tree2[u].push_back(v);
tree2[v].push_back(u);
}
auto d1 = bfs(1, tree1);
int A = 1;
for (int i = 1; i <= N1; i++){
if(d1[i] > d1[A]) A = i;
}
auto dA = bfs(A, tree1);
int B = A;
for (int i = 1; i <= N1; i++){
if(dA[i] > dA[B]) B = i;
}
auto dB = bfs(B, tree1);
vector<int> F1(N1+1);
int D1 = 0;
for (int i = 1; i <= N1; i++){
F1[i] = max(dA[i], dB[i]);
D1 = max(D1, F1[i]);
}
auto d2 = bfs(1, tree2);
int C_end = 1;
for (int j = 1; j <= N2; j++){
if(d2[j] > d2[C_end]) C_end = j;
}
auto dC = bfs(C_end, tree2);
int D_end = C_end;
for (int j = 1; j <= N2; j++){
if(dC[j] > dC[D_end]) D_end = j;
}
auto dD = bfs(D_end, tree2);
vector<int> F2(N2+1);
int D2 = 0;
for (int j = 1; j <= N2; j++){
F2[j] = max(dC[j], dD[j]);
D2 = max(D2, F2[j]);
}
int C_val = max(D1, D2);
vector<int> arrF2;
arrF2.reserve(N2);
for (int j = 1; j <= N2; j++){
arrF2.push_back(F2[j]);
}
sort(arrF2.begin(), arrF2.end());
vector<ll> prefix(N2+1, 0);
for (int j = 0; j < N2; j++){
prefix[j+1] = prefix[j] + arrF2[j];
}
ll res = 0;
for (int i = 1; i <= N1; i++){
int x = F1[i];
int threshold = C_val - 1 - x;
int pos = lower_bound(arrF2.begin(), arrF2.end(), threshold + 1) - arrF2.begin();
ll cnt_low = pos;
ll cnt_high = N2 - pos;
ll sum_high = prefix[N2] - prefix[pos];
ll contrib = cnt_low * (ll)C_val + cnt_high * (ll)(x + 1) + sum_high;
res += contrib;
}
cout << res << "\n";
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | F - Add One Edge 3 |
| User | OYU__0YU |
| Language | C++ 20 (gcc 12.2) |
| Score | 500 |
| Code Size | 2704 Byte |
| Status | AC |
| Exec Time | 191 ms |
| Memory | 35744 KB |
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 | 3504 KB |
| 00_sample_01.txt | AC | 1 ms | 3636 KB |
| 01_random_00.txt | AC | 151 ms | 34224 KB |
| 01_random_01.txt | AC | 27 ms | 10124 KB |
| 01_random_02.txt | AC | 15 ms | 7080 KB |
| 01_random_03.txt | AC | 2 ms | 3900 KB |
| 01_random_04.txt | AC | 130 ms | 31588 KB |
| 01_random_05.txt | AC | 79 ms | 20776 KB |
| 01_random_06.txt | AC | 54 ms | 17100 KB |
| 01_random_07.txt | AC | 45 ms | 14652 KB |
| 01_random_08.txt | AC | 58 ms | 17608 KB |
| 01_random_09.txt | AC | 48 ms | 15384 KB |
| 01_random_10.txt | AC | 93 ms | 35744 KB |
| 01_random_11.txt | AC | 39 ms | 18240 KB |
| 01_random_12.txt | AC | 76 ms | 28884 KB |
| 01_random_13.txt | AC | 16 ms | 9348 KB |
| 01_random_14.txt | AC | 68 ms | 27492 KB |
| 01_random_15.txt | AC | 21 ms | 12184 KB |
| 01_random_16.txt | AC | 53 ms | 23104 KB |
| 01_random_17.txt | AC | 34 ms | 14588 KB |
| 01_random_18.txt | AC | 95 ms | 35432 KB |
| 01_random_19.txt | AC | 77 ms | 28816 KB |
| 01_random_20.txt | AC | 147 ms | 33648 KB |
| 01_random_21.txt | AC | 85 ms | 23108 KB |
| 01_random_22.txt | AC | 30 ms | 11260 KB |
| 01_random_23.txt | AC | 30 ms | 11040 KB |
| 01_random_24.txt | AC | 64 ms | 18884 KB |
| 01_random_25.txt | AC | 72 ms | 20604 KB |
| 01_random_26.txt | AC | 77 ms | 22360 KB |
| 01_random_27.txt | AC | 64 ms | 17600 KB |
| 01_random_28.txt | AC | 63 ms | 18268 KB |
| 01_random_29.txt | AC | 30 ms | 10924 KB |
| 01_random_30.txt | AC | 147 ms | 33648 KB |
| 01_random_31.txt | AC | 156 ms | 32324 KB |
| 01_random_32.txt | AC | 90 ms | 23380 KB |
| 01_random_33.txt | AC | 114 ms | 26936 KB |
| 01_random_34.txt | AC | 64 ms | 16720 KB |
| 01_random_35.txt | AC | 24 ms | 9128 KB |
| 01_random_36.txt | AC | 53 ms | 15392 KB |
| 01_random_37.txt | AC | 37 ms | 12016 KB |
| 01_random_38.txt | AC | 42 ms | 13040 KB |
| 01_random_39.txt | AC | 43 ms | 13304 KB |
| 01_random_40.txt | AC | 149 ms | 33932 KB |
| 01_random_41.txt | AC | 175 ms | 33660 KB |
| 01_random_42.txt | AC | 191 ms | 33680 KB |
| 01_random_43.txt | AC | 191 ms | 33648 KB |
| 01_random_44.txt | AC | 167 ms | 33648 KB |
| 01_random_45.txt | AC | 1 ms | 3432 KB |
| 01_random_46.txt | AC | 78 ms | 19444 KB |