QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#191060 | #7103. Red Black Tree | Geospiza# | WA | 0ms | 3828kb | C++20 | 2.3kb | 2023-09-29 17:20:49 | 2023-09-29 17:20:50 |
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),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++){
ll 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]-2ll*des[lca(u,v)];
};
fa[1][0]=0;
dep[1]=1;
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;
});
ll ans=val[1].first,p=val[1].second;
for(int l=1;l<=k;l++){
//cout<<l<<"\n";
p=lca(val[l].second,p);
while(l+1<=k&&lca(val[l+1].second,p)==p){
//cout<<l<<endl;
l++;
}
ll now=min(val[1].first,dist(p,val[1].second));
ans=min(ans,max(now,val[l+1].first));
l++;
}
cout<<ans<<"\n";
}
}
}
/*
2
12 2 5
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
1 1
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
*/
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3828kb
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 7 4 8 0 0 0
result:
wrong answer 2nd lines differ - expected: '5', found: '7'