QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#178723 | #7103. Red Black Tree | PlentyOfPenalty# | AC ✓ | 602ms | 28832kb | C++20 | 2.3kb | 2023-09-14 11:44:44 | 2023-09-14 11:44:44 |
Judging History
answer
#include<bits/stdc++.h>
#ifndef memset0
#define endl '\n'
#endif
using namespace std;
const int N=1e5+9;
int T,n,m,q,tot,anc[N],fa[N],dfn[N],dep[N],a[N],b[N],top[N],cur[N],siz[N],son[N],rnkl[N],rnkr[N];
long long len[N],dp[N][2],pre[N],suf[N],dis[N];
bool col[N],foc[N];
vector<int> red,F[N];
vector<pair<int,int>> G[N];
template<class T> inline void updmin(T &x,T y){
if(y<x)x=y;
}
template<class T> inline void updmax(T &x,T y){
if(y>x)x=y;
}
inline int cmp_by_dfn(int u,int v){
return dfn[u]<dfn[v];
}
void dfs(int u,int u_anc){
if(col[u]){
u_anc=u;
}
dfn[u]=++tot;
anc[u]=u_anc;
siz[u]=1;
son[u]=0;
// cerr<<"!!! dfs "<<u<<" "<<u_anc<<endl;
for(auto [v,w]:G[u])
if(v!=fa[u]){
// cerr<<u<<" >> "<<v<<endl;
fa[v]=u;
dep[v]=dep[u]+1;
len[v]=len[u]+w;
dfs(v,u_anc);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]){
son[u]=v;
}
}
}
void dfs2(int u,int tpt){
top[u]=tpt;
if(siz[u]==1)return;
dfs2(son[u],tpt);
for(auto [v,w]:G[u])
if(v!=fa[u]&&v!=son[u]){
dfs2(v,v);
}
}
inline int lca(int u,int v){
while(top[u]!=top[v]){
if(dep[top[u]]>dep[top[v]])swap(u,v);
v=fa[top[v]];
}
return dep[u]<dep[v]?u:v;
}
int main(){
#ifdef memset0
freopen("B.in","r",stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
cin>>T;
while(T--){
cin>>n>>m>>q;
fill_n(col+1,n,false);
for(int i=1;i<=n;i++){
G[i].clear();
}
for(int x,i=1;i<=m;i++){
cin>>x;
red.push_back(x);
col[x]=true;
}
for(int u,v,w,i=1;i<n;i++){
cin>>u>>v>>w;
G[u].emplace_back(v,w);
G[v].emplace_back(u,w);
}
tot=0;
dep[1]=1;
dfs(1,1);
dfs2(1,1);
for(int i=1;i<=n;i++){
dis[i]=len[i]-len[anc[i]];
}
for(int k,i=1;i<=q;i++){
cin>>k;
for(int i=1;i<=k;i++)cin>>b[i];
sort(b+1,b+k+1,[&](int u,int v){
return dis[u]>dis[v];
});
int lc=b[1];
long long max_len=0;
b[k+1]=0;
long long ans=dis[b[1]];
for(int i=1;i<=k;i++){
lc=lca(lc,b[i]);
updmax(max_len,len[b[i]]);
updmin(ans,max(max_len-len[lc],dis[b[i+1]]));
}
cout<<ans<<endl;
}
}
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 13024kb
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: 0
Accepted
time: 602ms
memory: 28832kb
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:
ok 577632 lines
Extra Test:
score: 0
Extra Test Passed