QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#506458 | #5439. Meet in the Middle | ucup-team1198 | WA | 4ms | 38612kb | C++14 | 5.4kb | 2024-08-05 17:40:45 | 2024-08-05 17:40:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int MAXN = 2e5 + 100, MAXLN = 18;
vector<array<int, 2>> g[MAXN];
int d[MAXN], tin[MAXN];
int lg[MAXN];
int sp[MAXLN][MAXN];
vector<int> euler;
void build_lca(int v, int p = -1) {
tin[v] = euler.size();
euler.push_back(v);
for (auto e : g[v]) {
int u = e[0], w = e[1];
if (u == p) continue;
d[u] = d[v] + w;
build_lca(u, v);
euler.push_back(v);
}
}
int get_min(int u, int v) {
return d[u] < d[v] ? u : v;
}
int lca(int u, int v) {
int l = tin[u], r = tin[v];
if (l > r) swap(l, r);
++r;
int lvl = lg[r - l];
return get_min(sp[lvl][l], sp[lvl][r - (1 << lvl)]);
}
int dist(int u, int v) {
return d[u] + d[v] - 2 * d[lca(u, v)];
}
struct pVertex {
int v = -1;
int d = -1;
};
array<pVertex, 2> apply(array<pVertex, 2> diam, pVertex v) {
if (v.v == -1) return diam;
if (diam[0].v == -1) return {v, v};
int d0 = dist(diam[0].v, diam[1].v) + diam[0].d + diam[1].d;
int d1 = dist(diam[1].v, v.v) + diam[1].d + v.d;
int d2 = dist(diam[0].v, v.v) + diam[0].d + v.d;
if (d1 >= d0 && d1 >= d2) return {v, diam[1]};
if (d2 >= d0 && d2 >= d1) return {diam[0], v};
return diam;
}
array<pVertex, 2> get_max(array<pVertex, 2> a, array<pVertex, 2> b) {
return apply(apply(a, b[0]), b[1]);
}
int dist(array<pVertex, 2> diam, int v) {
return max(dist(diam[0].v, v) + diam[0].d, dist(diam[1].v, v) + diam[1].d);
}
int lvl[MAXN];
int c_par[MAXN], id[MAXN];
int cd[MAXLN][MAXN];
int n;
int sz[MAXN];
vector<array<pVertex, 2>> pref[MAXN], suf[MAXN];
void calc_sz(int v, int p = -1) {
sz[v] = 1;
for (auto e : g[v]) {
int u = e[0];
/// cerr << v << "->" << u << endl;
if (lvl[u] != -1 || u == p) continue;
calc_sz(u, v);
sz[v] += sz[u];
}
}
void calc_d(int v, int l, int p = -1) {
for (auto e : g[v]) {
int u = e[0], w = e[1];
if (lvl[u] != -1 || u == p) continue;
cd[l][u] = cd[l][v] + w;
calc_d(u, l, v);
}
}
pair<int, vector<int>> get_centr(int v, int l = 0) {
calc_sz(v);
int total = sz[v];
/**if (l == 0) {
cerr << total << " " << v << endl;
}*/
bool found = true;
while (found) {
found = false;
for (auto e : g[v]) {
int u = e[0];
if (lvl[u] != -1 || sz[u] > sz[v]) continue;
if (sz[u] * 2 >= total) {
v = u;
found = true;
break;
}
}
}
/// v is the centroid;
lvl[v] = l;
vector<array<pVertex, 2>> children;
vector<int> subtree = {v - n};
calc_d(v, l);
for (auto e : g[v]) {
int u = e[0];
if (lvl[u] != -1) continue;
auto res = get_centr(u, l + 1);
id[res.first] = children.size();
c_par[res.first] = v;
array<pVertex, 2> cur;
for (int u : res.second) {
subtree.push_back(u);
cur = apply(cur, {u, cd[l][u + n]});
}
children.push_back(cur);
}
pref[v].resize(children.size() + 1);
suf[v].resize(children.size());
for (int i = 1; i <= (int)children.size(); ++i) {
pref[v][i] = get_max(pref[v][i - 1], children[i - 1]);
}
for (int i = (int)children.size() - 2; i >= 0; --i) {
suf[v][i] = get_max(suf[v][i + 1], children[i + 1]);
}
return {v, subtree};
}
vector<array<int, 4>> queries[MAXN];
void solve_() {
int q;
cin >> n >> q;
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
--u; --v;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
build_lca(0);
int esz = euler.size();
for (int i = 0; i < esz; ++i) {
sp[0][i] = euler[i];
}
for (int lvl = 1; lvl < MAXLN; ++lvl) {
for (int i = 0; i + (1 << lvl) <= esz; ++i) {
sp[lvl][i] = get_min(sp[lvl - 1][i], sp[lvl - 1][i + (1 << (lvl - 1))]);
}
}
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
--u; --v;
u += n; v += n;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
fill(lvl + n, lvl + 2 * n, -1);
fill(c_par + n, c_par + 2 * n, -1);
get_centr(n);
/**for (int i = n; i < 2 * n; ++i) {
cerr << lvl[i] << " " << c_par[i] << " " << id[i] << endl;
}*/
for (int i = 0; i < q; ++i) {
int a, b;
cin >> a >> b;
--a; --b;
b += n;
int nope = -1;
int ans = 0;
for (int c = b; c != -1; nope = id[c], c = c_par[c]) {
int d0 = cd[lvl[c]][b];
array<pVertex, 2> diam;
if (nope == -1) {
diam = pref[c].back();
} else {
diam = get_max(pref[c][nope], suf[c][nope]);
}
ans = max(ans, d0 + dist(diam, a));
}
cout << ans << "\n";
}
}
/// #define MULTITEST
main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
lg[1] = 0;
for (int i = 2; i < MAXN; ++i) {
lg[i] = lg[i / 2] + 1;
}
int tst = 1;
#ifdef MULTITEST
cin >> tst;
#endif // MULTITEST
while (tst--) {
solve_();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 36272kb
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2
output:
6 4 5 3
result:
ok 4 number(s): "6 4 5 3"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 38612kb
input:
2 1 1 2 1 1 2 1 1 1
output:
0
result:
wrong answer 1st numbers differ - expected: '2', found: '0'