QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#191084 | #7103. Red Black Tree | Geospiza | WA | 1787ms | 71272kb | C++20 | 3.1kb | 2023-09-29 17:33:32 | 2023-09-29 17:33:33 |
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;
const ll N=2e5+5;
vector<int>dfn,pos,mp;
vector<int>vec[N];
void DFS(int u,int pre)
{
dfn.push_back(u);
pos[u]=dfn.size()-1;
mp[dfn.size()-1]=u;
for(auto v:vec[u])if(v!=pre)
{
DFS(v,u);
dfn.push_back(u);
}
}
int n;
#define rep(i,a,b) for(int i=a;i<=b;i++)
int st[N][21];
void init(){
rep(i,1,2*n)st[i][0] = dfn[i-1];
rep(j,1,20)
for(int i=1;i+(1<<j)-1<=n;i++)
st[i][j] = min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
int query(int l, int r)
{
int renk = log2(r - l + 1);
return min(st[l][renk], st[r - (1 << renk) + 1][renk]);
}
int LCA(int x,int y)
{
--x,--y;
x=pos[x],y=pos[y];
if(x==y)return mp[x]+1;
return mp[query(min(x,y)+1,max(x,y)+1)]+1;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int T=1;
cin>>T;
while(T--){
ll m,q;
cin>>n>>m>>q;
dfn.clear();
pos.clear();
mp.clear();
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;
}
for(int i=0;i<=n;++i)vec[i].clear();
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});
--u,--v;
vec[u].push_back(v);
vec[v].push_back(u);
}
pos.resize(n);
mp.resize(2*n);
DFS(0,-1);
for(auto &x:dfn)x=pos[x];
init();
function<void(ll)>dfs1=[&](int u){
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 dist=[&](int u,int v)->ll
{
//cout<<LCA(u,v,st)<<endl;
//cout<<u<<" "<<v<<endl;
return des[u]+des[v]-2*des[LCA(u,v)];
};
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<<"p="<<p<<endl;
}
//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";
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 10148kb
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: 1787ms
memory: 71272kb
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:
151364130 151364130 0 493591722 493591722 255904892 493591722 1265145897 1265145897 1168076167 1168076167 587526565 285643094 285643094 285643094 317919339 0 785245841 691421476 605409472 479058444 371688030 280460858 592119466 903923034 562681002 741465572 52784081 166647862 166647862 166647862 166...
result:
wrong answer 1st lines differ - expected: '148616264', found: '151364130'