QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#402191 | #7103. Red Black Tree | miaokx | WA | 1691ms | 29384kb | C++17 | 3.3kb | 2024-04-30 06:30:15 | 2024-04-30 06:30:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
vector<long long> cost, d;
vector<bool> red;
vector<vector<int>> adj;
vector<vector<pair<int, long long>>> adj2;
// using LCA------------------------------------------------
#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)];}
};
// ---------------------------------------------------------------
void dfs(int cur, int par) {
for (auto &[v, w] : adj2[cur]) {
if (v == par) continue;
d[v] = d[cur] + w;
if (!red[v]) {
cost[v] = cost[cur] + w;
}
dfs(v, cur);
}
}
void solve() {
int n, r, q;
cin >> n >> r >> q;
adj.assign(n, vector<int>());
adj2.assign(n, vector<pair<int,long long>>());
cost.assign(n, 0);
d.assign(n, 0);
red.assign(n, false);
for (int i = 0; i < r; i++) {
int x;
cin >> x;
x--;
red[x] = true;
}
for (int i = 0; i < n-1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
adj2[u].push_back({v, w});
adj2[v].push_back({u, w});
}
dfs(0, -1);
/*
for (int i = 0; i < n; i++) {
cout << cost[i] << " \n"[i==n-1];
}
for (int i = 0; i < n; i++) {
cout << d[i] << " \n"[i==n-1];
}
*/
LCA lca(adj);
while (q--) {
int k;
cin >> k;
vector<int> query(k);
for (int j = 0; j < k; j++) {
cin >> query[j];
query[j]--;
}
query.push_back(0);
sort(begin(query), end(query),
[&](int i, int j) {
return cost[i] > cost[j];
});
int node = query[0];
long long mxd = d[query[0]];
long long ans = cost[query[1]];
for (int i = 1; i + 1 < k; i++) {
node = lca.lca(node, query[i]);
mxd = max(mxd, d[query[i]]);
ans = min(ans, max(mxd - d[node], cost[query[i+1]]));
}
cout << ans << '\n';
}
adj.clear();
adj2.clear();
cost.clear();
d.clear();
red.clear();
}
int main() {
int t;
cin >> t;
while (t--)
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3544kb
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: 1691ms
memory: 29384kb
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 38th lines differ - expected: '22932276', found: '38363450'