QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#338210 | #7103. Red Black Tree | Lain | WA | 771ms | 28168kb | C++23 | 3.1kb | 2024-02-25 19:08:03 | 2024-02-25 19:08:03 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<class T>
struct RMQ {
vector<vector<T>> jmp;
RMQ(const vector<T>& V) : jmp(1, V) {
for (int pw = 1, k = 1; pw * 2 <= sz(V); pw *= 2, ++k) {
jmp.emplace_back(sz(V) - pw * 2 + 1);
rep(j,0,sz(jmp[k]))
jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
}
}
T query(int a, int b) {
assert(a < b); // or return inf if a == b
int dep = 31 - __builtin_clz(b - a);
return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
}
};
struct LCA {
int T = 0;
vi time, path, ret;
RMQ<int> rmq;
LCA(vector<vi>& C) : time(sz(C)), rmq((dfs(C,0,-1), ret)) {}
void dfs(vector<vi>& C, int v, int par) {
time[v] = T++;
for (int y : C[v]) if (y != par) {
path.push_back(v), ret.push_back(time[v]);
dfs(C, y, v);
}
}
int lca(int a, int b) {
if (a == b) return a;
tie(a, b) = minmax(time[a], time[b]);
return path[rmq.query(a, b)];
}
//dist(a,b){return depth[a] + depth[b] - 2*depth[lca(a,b)];}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt;
cin >> tt;
while(tt--) {
int n, m, q;
cin >> n >> m >> q;
vector<bool> is_red(n);
for (int i =0; i < m; i++) {
int x;
cin >> x;
x--;
is_red[x] = 1;
}
vector<vi> lca_g(n);
vector<vector<pair<int, int>>> g(n);
rep(i, 1, n) {
int u, v, w;
cin >>u >> v >> w;
u--, v--;
lca_g[u].push_back(v);
lca_g[v].push_back(u);
g[u].push_back({v, w});
g[v].push_back({u, w});
}
LCA L(lca_g);
vector<int64_t> cost(n), total_weight(n);
vi red_parent(n);
auto cost_dfs = [&](auto& self, int s, int p)->void {
if (is_red[s]) {
cost[s] = 0;
red_parent[s] = s;
}
for (auto& [u, w] : g[s]) {
if (u == p) continue;
cost[u] = cost[s] + w;
total_weight[u] = total_weight[s] + w;
red_parent[u] = red_parent[s];
self(self, u, s);
}
};
cost_dfs(cost_dfs, 0, 0);
while(q--) {
int k;
cin >> k;
vi v(k);
for (auto& x : v) cin >> x, x--;
sort(all(v), [&](int x, int y)->bool {
return cost[x] > cost[y];
});
if (v.size() == 1) {
cout << 0 << '\n';
continue;
}
int64_t ans = cost[v[1]];
int curr_lca = v[0];
rep(i, 1, k) {
int new_lca = L.lca(curr_lca, v[i]);
// THe new LCA is an ancestor of the red parent of the costliest
// node, so the maximum cost would not change.
if (L.lca(new_lca, red_parent[v[0]]) == new_lca)
break;
int64_t newans = total_weight[v[0]] - total_weight[new_lca];
if (i+1 < k) {
newans = max(newans, cost[v[i+1]]);
}
ans = min(ans, newans);
curr_lca = new_lca;
}
cout << ans << '\n';
}
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3548kb
input:
2 12 2 4 1 9 1 2 1 2 3 4 3 4 3 3 5 2 2 6 2 6 7 1 6 8 2 2 9 5 9 10 2 9 11 3 1 12 10 3 3 7 8 4 4 5 7 8 4 7 8 10 11 3 4 5 12 3 2 3 1 2 1 2 1 1 3 1 1 1 2 1 2 3 1 2 3
output:
4 5 3 8 0 0 0
result:
ok 7 lines
Test #2:
score: -100
Wrong Answer
time: 771ms
memory: 28168kb
input:
522 26 1 3 1 1 4 276455 18 6 49344056 18 25 58172365 19 9 12014251 2 1 15079181 17 1 50011746 8 9 2413085 23 24 23767115 22 2 26151339 26 21 50183935 17 14 16892041 9 26 53389093 1 20 62299200 24 18 56114328 11 2 50160143 6 26 14430542 16 7 32574577 3 16 59227555 3 15 8795685 4 12 5801074 5 20 57457...
output:
148616264 148616264 0 319801028 319801028 255904892 317070839 1265145897 1265145897 1072765445 667742619 455103436 285643094 285643094 285643094 317919339 0 785245841 691421476 605409472 479058444 371688030 303203698 493383271 919185207 910180170 919185207 121535083 181713164 181713164 181713164 181...
result:
wrong answer 691st lines differ - expected: '549790714', found: '374667774'