QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#230984 | #7103. Red Black Tree | UFRJ# | TL | 1ms | 3388kb | C++20 | 2.4kb | 2023-10-28 22:51:58 | 2023-10-28 22:51:59 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using lint = int64_t;
const lint linf = 1e18;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin>>t;
while(t--){
int n, m, q;
cin>>n>>m>>q;
vector<int>x(m);
vector<bool>w(n);
for(int i=0;i<m;i++) cin>>x[i], x[i]--, w[x[i]] = 1;
vector<vector<pair<int, int>>>g(n);
for(int i=1;i<n;i++){
int a, b, c;
cin>>a>>b>>c;
a--, b--;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
vector<lint>d(n), dp(n);
dp[0] = linf;
vector<int>tin(n), h(n);
int timer = 0;
vector<vector<int>>anc(17, vector<int>(n));
auto dfs = [&](auto& self, int u, int p)->void {
tin[u] = timer++;
anc[0][u] = p;
if(w[u]) dp[u] = 0;
for(auto [v, c] : g[u]){
if(v == p) continue;
h[v] = h[u] + 1;
d[v] = d[u] + c;
dp[v] = dp[u] + c;
self(self, v, u);
}
};
dfs(dfs, 0, 0);
for(int b=1;b<17;b++){
for(int i=0;i<n;i++) anc[b][i] = anc[b-1][anc[b-1][i]];
}
auto get_lca = [&](int u, int v)->int {
if(h[u] < h[v]) swap(u, v);
int c = h[u] - h[v];
for(int b=16;b>=0;b--) if(c>>b&1) u = anc[b][u];
for(int b=16;b>=0;b--) if(anc[b][u] != anc[b][v]) u = anc[b][u], v = anc[b][v];
if(u != v) u = anc[0][u];
return u;
};
while(q--){
int k;
cin>>k;
vector<int>v(k);
for(int i=0;i<k;i++) cin>>v[i], v[i]--;
sort(v.begin(), v.end(), [&](int i, int j){
return tin[i] < tin[j];
});
lint lo = 0, hi = 1e16;
while(lo <= hi){
lint mid = (lo + hi) >> 1;
vector<int>cur;
for(int i : v) if(dp[i] > mid) cur.push_back(i);
bool ok = true;
if(!cur.empty()){
int lca = get_lca(cur[0], cur.back());
for(int i : cur) if(d[i] - d[lca] > mid) ok = false;
}
if(ok) hi = mid - 1;
else lo = mid + 1;
}
cout<<lo<<"\n";
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3388kb
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
Time Limit Exceeded
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...