QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#426117 | #3047. Wind of Change | Ew_Cors | TL | 0ms | 0kb | C++20 | 2.7kb | 2024-05-30 21:08:18 | 2024-05-30 21:08:20 |
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2.5e5 + 5;
using i64 = long long;
using E = vector<pair<int, int> >;
using V = vector<pair<int, i64> >;
int vis[N];
int siz[N];
void gsiz(int u, int fa, E *e) {
siz[u] = 1;
for (auto [v, w] : e[u]) {
if (v == fa || vis[v]) {
continue;
}
gsiz(v, u, e);
siz[u] += siz[v];
}
}
void frt(int u, int fa, int sm, int &rt, int &rsm, E *e) {
int mxn = 0;
for (auto [v, w] : e[u]) {
if (v == fa || vis[v]) {
continue;
}
frt(v, u, sm, rt, rsm, e);
mxn = max(mxn, siz[v]);
}
if (u == fa) {
if (mxn > rsm) {
rsm = mxn;
rt = u;
}
}
if (min(mxn, sm - siz[u]) > rsm) {
rsm = min(mxn, sm - siz[u]);
rt = u;
}
}
void fdp(int u, int fa, i64 dep, int rt, V *s, E *e) {
s[u].push_back({rt, dep});
for (auto [v, w] : e[u]) {
if (v == fa || vis[v]) {
continue;
}
fdp(v, u, dep + w, rt, s, e);
}
}
void dfs(int u, V *s, E *e) {
gsiz(u, u, e);
int sm = siz[u], rt = 0, rsm = 0;
// cerr << sm << '\n';
frt(u, u, sm, rt, rsm, e);
if (rt == 0) {
s[u].push_back({u, 0});
return;
}
// cerr << rt << '\n';
fdp(rt, rt, 0, rt, s, e);
vis[rt] = 1;
for (auto [v, w] : e[rt]) {
if (!vis[v]) {
dfs(v, s, e);
}
}
}
int n;
E e[2][N];
V s[2][N];
void bld(int x) {
for (int i = 1; i <= n; i++) {
vis[i] = 0;
}
dfs(1, s[x], e[x]);
}
struct M {
i64 mx, sm;
int id;
};
inline i64 D(pair<int, int> x) {
return 1LL * x.first * n + x.second;
}
unordered_map<i64, M> mm;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int o = 0; o < 2; o++) {
for (int i = 1, u, v, w; i < n; i++) {
cin >> u >> v >> w;
e[o][u].push_back({v, w});
e[o][v].push_back({u, w});
}
}
bld(0);
bld(1);
for (int i = 1; i <= n; i++) {
for (auto [f0, d0] : s[0][i]) {
for (auto [f1, d1] : s[1][i]) {
if (!mm.count(D({f0, f1}))) {
mm[D({f0, f1})] = {(i64)1e18, (i64)1e18, 0};
}
auto &[mx, sm, id] = mm[D({f0, f1})];
if (d0 + d1 <= mx) {
sm = mx;
mx = d0 + d1;
id = i;
} else if (d0 + d1 < sm) {
sm = d0 + d1;
}
}
}
}
for (int i = 1; i <= n; i++) {
i64 mn = 1e18;
for (auto [f0, d0] : s[0][i]) {
for (auto [f1, d1] : s[1][i]) {
auto &[mx, sm, id] = mm[D({f0, f1})];
if (i == id) {
mn = min(mn, d0 + d1 + sm);
} else {
mn = min(mn, d0 + d1 + mx);
}
}
}
cout << mn << '\n';
}
return 0;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
250000 137466 116241 624409157 188961 163586 148491970 13958 122239 46409636 186120 44010 919858866 72320 102560 92679302 158544 236582 882613876 22331 114267 646741536 210782 42861 679450428 56693 45397 898958944 71188 114724 904191407 15203 225401 210626781 31071 144225 278121829 110354 83972 4557...