QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#524001 | #5102. Dungeon Crawler | Andycipation | WA | 1ms | 3632kb | C++20 | 3.0kb | 2024-08-19 07:19:22 | 2024-08-19 07:19:22 |
Judging History
answer
/*
* author: ADMathNoob
* created: 08/18/24 10:47:30
* problem: https://qoj.ac/problem/5102
*/
/*
Comments about problem:
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2005;
const int H = 13;
int n, q;
vector<pair<int, int>> g[N];
int pv[N];
int pre[N], post[N];
vector<int> order;
long long d[N];
long long maxD[N];
void Dfs(int v, int p) {
pv[v] = p;
pre[v] = order.size();
order.push_back(v);
maxD[v] = d[v];
for (auto [u, w] : g[v]) {
if (u == p) continue;
d[u] = d[v] + w;
Dfs(u, v);
maxD[v] = max(maxD[v], maxD[u]);
}
post[v] = order.size() - 1;
}
int pr[N][H];
long long pref[N];
long long suf[N];
void DfsFrom(int v) {
d[v] = 0;
order.clear();
Dfs(v, -1);
for (int i = 0; i < n; i++) {
fill(pr[i], pr[i] + H, -1);
}
for (int i = 0; i < n; i++) {
pr[i][0] = pv[i];
}
for (int j = 1; j < H; j++) {
for (int i = 0; i < n; i++) {
pr[i][j] = (pr[i][j - 1] == -1 ? -1 : pr[pr[i][j - 1]][j - 1]);
}
}
for (int i = 0; i < n; i++) {
pref[i + 1] = max(pref[i], d[order[i]]);
}
for (int i = n - 1; i >= 0; i--) {
suf[i] = max(suf[i + 1], d[order[i]]);
}
}
long long MaxDist(int ex) {
// farthest excluding subtree rooted at `ex`
return max(pref[pre[ex]], suf[post[ex] + 1]);
}
bool Anc(int x, int y) {
return pre[x] <= pre[y] && post[y] <= post[x];
}
int Lca(int x, int y) {
if (Anc(x, y)) return x;
if (Anc(y, x)) return y;
for (int j = H - 1; j >= 0; j--) {
if (pr[x][j] != -1 && !Anc(pr[x][j], y)) {
x = pr[x][j];
}
}
return pv[x];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
long long total = 0;
for (int i = 0; i < n - 1; i++) {
int x, y, w;
cin >> x >> y >> w;
--x; --y;
g[x].emplace_back(y, w);
g[y].emplace_back(x, w);
total += w;
}
vector<int> s(q), k(q), t(q);
for (int i = 0; i < q; i++) {
cin >> s[i] >> k[i] >> t[i];
--s[i]; --k[i]; --t[i];
}
vector<vector<int>> phase1(n);
for (int i = 0; i < q; i++) {
phase1[k[i]].push_back(i);
}
const long long inf = 1e18;
vector<long long> ret(q, inf);
vector<vector<pair<int, int>>> phase2(n);
for (int kk = 0; kk < n; kk++) {
DfsFrom(kk);
for (int qid : phase1[kk]) {
int L = Lca(s[qid], t[qid]);
if (L == t[qid]) continue;
ret[qid] = 2 * total - MaxDist(L);
int ex = (pv[L] != -1 ? pv[L] : -1);
phase2[s[qid]].emplace_back(qid, ex);
}
}
for (int ss = 0; ss < n; ss++) {
DfsFrom(ss);
for (auto [qid, ex] : phase2[ss]) {
long long res;
if (ex == -1) {
res = maxD[ss];
} else {
res = MaxDist(ex);
}
ret[qid] = min(ret[qid], 2 * total - res);
}
}
for (long long r : ret) {
if (r == inf) {
cout << "impossible\n";
} else {
cout << r << '\n';
}
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3632kb
input:
22 5 1 2 7 2 3 5 3 4 8 3 6 6 3 7 3 2 5 6 5 8 2 8 9 11 2 10 16 1 11 12 11 12 4 11 13 9 1 14 25 14 15 4 15 16 5 15 17 6 15 18 1 15 19 8 14 20 7 20 21 9 20 22 17 1 19 9 1 9 19 1 8 9 1 9 8 2 22 11
output:
306 293 293 impossible 306
result:
wrong answer 1st lines differ - expected: '316', found: '306'