QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#190995 | #7103. Red Black Tree | Geospiza# | TL | 0ms | 3836kb | C++20 | 2.5kb | 2023-09-29 16:34:00 | 2023-09-29 16:34:00 |
Judging History
answer
//#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int T=1;
cin>>T;
while(T--){
ll n,m,q;
cin>>n>>m>>q;
vector<ll>color(n+5),dis(n+5,1e16),dep(n+5),des(n+5);
for(int i=1;i<=m;i++){
int r; cin>>r;
color[r]=1;
dis[r]=0;
}
vector<vector<pll>>ed(n+5);
vector<vector<ll>>fa(n+5,vector<ll>(20));
for(int i=1;i<n;i++){
int u,v,w;
cin>>u>>v>>w;
ed[u].push_back({v,w});
ed[v].push_back({u,w});
}
function<void(ll)>dfs1=[&](int u){
for(int i=1;i<=19;i++)
fa[u][i]=fa[fa[u][i-1]][i-1];
for(auto [v,w]:ed[u]){
if(v==fa[u][0])
continue;
fa[v][0]=u,dep[v]=dep[u]+1,des[v]=des[u]+w;
dfs1(v);
}
};
auto lca=[&](int u,int v)->ll
{
if(dep[u]<dep[v])
swap(u,v);
for(int i=19;i>=0;i--)
if(dep[fa[u][i]]>=dep[v])
u=fa[u][i];
if(u==v)
return u;
for(int i=19;i>=0;i--){
if(fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
}
return fa[u][0];
};
auto dist=[&](int u,int v)->ll
{
return des[u]+des[v]-2*des[lca(u,v)];
};
fa[1][0]=0;
dfs1(1);
function<void(ll fa,ll u,ll now)>dfs=[&](ll fa,ll u,ll now)
{
if(color[u]==1){
now=0;
}
dis[u]=now;
for(auto [v,w]:ed[u]){
if(v==fa)
continue;
dfs(u,v,now+w);
}
};
dfs(0,1,0);
while(q--){
ll k;
cin>>k;
vector<ll>v(k+5);
vector<pll>val(k+5);
for(int i=1;i<=k;i++){
cin>>v[i];
val[i]={dis[v[i]],v[i]};
}
sort(val.begin()+1,val.begin()+1+k,[&](pll x,pll y){
return x.first>y.first;
});
auto check=[&](ll x)->bool
{
int p=val[1].second;
//cout<<x<<" "<<p<<"\n";
for(int i=2;i<=k;i++){
if(val[i].first<=x){
break;
}
//cout<<val[i].second<<" "<<p<<"\n";
p=lca(p,val[i].second);
}
//cout<<"\n";
for(int i=2;i<=k;i++){
if(val[i].first<=x){
break;
}
if(dist(val[i].second,p)>x){
return 0;
}
}
return 1;
};
//vector<ll>vis(k+5);
ll l=0,r=val[1].first;
while(l<=r){
ll mid=(l+r)>>1;
if(check(mid)){
r=mid-1;
}
else{
l=mid+1;
}
}
cout<<l<<"\n";
}
}
}
/*
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3836kb
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:
129225499 129225499 0 317070839 317070839 255904892 317070839 1096238166 1096238166 873176972 546644500 455103436 285643094 285643094 285643094 317919339 0 712913890 514087057 401528523 479058444 371688030 280460858 476229270 910180170 910180170 898824885 121535083 166647862 166647862 166647862 1666...