QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#695420 | #5439. Meet in the Middle | hyxle | RE | 0ms | 0kb | C++14 | 4.2kb | 2024-10-31 20:00:07 | 2024-10-31 20:00:09 |
answer
#include<bits/stdc++.h>
#define ll long long
#define int ll
#define R register
using namespace std;
const int N=1e5+5;
int n;
namespace Tree1{
int st[N][22],dep[N],cur,dfn[N],lg[N];
struct edge{int u,v,w;}e[N<<1];
int head[N],tot;
inline void add(int u,int v,int w){e[++tot]={v,head[u],w},head[u]=tot;}
namespace LCA{
inline void dfs_lca(int u,int fa){
dfn[u]=++cur;
st[dfn[u]][0]=fa;
for(R int i=head[u];i;i=e[i].v){
int v=e[i].u;
if(v==fa)continue;
dep[v]=dep[u]+e[i].w;
dfs_lca(v,u);
}
}
inline int getmin(int x,int y){return dep[x]<dep[y]?x:y;}
inline void work(){
lg[0]=-1;for(R int i=1;i<=n;++i)lg[i]=lg[i>>1]+1;
for(R int j=1;(1<<j)<=n;++j){
for(R int i=1;i+(1<<j)-1<=n;++i){
st[i][j]=getmin(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
}
}
inline int lca(int u,int v){
if(u==v)return u;
if(dfn[u]>dfn[v])swap(u,v);
int k=lg[dfn[v]-dfn[u]];
return getmin(st[dfn[u]+1][k],st[dfn[v]-(1<<k)+1][k]);
}
}
using namespace LCA;
inline ll dist(int x,int y){return dep[x]+dep[y]-2*dep[lca(x,y)];}
inline void clear(){
for(R int i=1;i<=n;++i)head[i]=0;tot=0;cur=0;
}
};
namespace Tree2{
int st[N][22],dep[N],cur,dfn[N],lg[N],rt=1;
struct edge{int u,v,w;}e[N<<1];
int head[N],tot;
inline void add(int u,int v,int w){e[++tot]={v,head[u],w},head[u]=tot;}
namespace LCA{
inline void dfs_lca(int u,int fa){
dfn[u]=++cur;
st[dfn[u]][0]=fa;
for(R int i=head[u];i;i=e[i].v){
int v=e[i].u;
if(v==fa)continue;
dep[v]=dep[u]+e[i].w;
dfs_lca(v,u);
}
}
inline int getmin(int x,int y){return dep[x]<dep[y]?x:y;}
inline void work(){
lg[0]=-1;for(R int i=1;i<=n;++i)lg[i]=lg[i>>1]+1;
for(R int j=1;(1<<j)<=n;++j){
for(R int i=1;i+(1<<j)-1<=n;++i){
st[i][j]=getmin(st[i][j-1],st[i+(1<<j-1)][j-1]);
}
}
}
inline int lca(int u,int v){
if(u==v)return u;
if(dfn[u]>dfn[v])swap(u,v);
int k=lg[dfn[v]-dfn[u]];
return getmin(st[dfn[u]+1][k],st[dfn[v]-(1<<k)+1][k]);
}
}
using namespace LCA;
inline ll dist(int x,int y){return dep[x]+dep[y]+dep[rt]*2-2*dep[lca(x,rt)]-2*dep[lca(y,rt)]+Tree1::dist(x,y);}
struct node{
int x,y,dis;
node(){x=y=0,dis=-1;}
node(int u){x=y=u;dis=0;}
node(int u,int v){x=u,y=v;dis=dist(u,v);}
}f[N][2];
struct node Max(node x,node y){return (x.dis<y.dis?y:x);}
inline node operator+(node x,node y){
if(x.dis==-1||y.dis==-1)return Max(x,y);
return Max(Max(node(x.x,x.y),node(y.x,y.y)),Max(Max(node(x.y,y.x),node(x.y,y.y)),Max(node(x.x,y.x),node(x.x,y.y))));
}
inline void dfs1(int u,int fa){
f[u][0]=node(u);
for(R int i=head[u];i;i=e[i].v){
int v=e[i].u;
if(v==fa)continue;
dfs1(v,u);
f[u][0]=f[u][0]+f[v][0];
}
}
node pre[N];
inline void dfs2(int u,int fa){
rt=u;int cnt=0;
vector<int> son;
for(R int i=head[u];i;i=e[i].v){
int v=e[i].u;
if(v==fa)continue;
son.emplace_back(v);
++cnt;pre[cnt]=pre[cnt-1]+f[v][0];
}
int cntt=0;node suf;
for(R int i=cnt-1;i>=0;--i){
int sn=son[i];
rt=sn;
++cntt;
f[sn][1]=suf+pre[cnt-cntt]+f[u][1]+node(u);
rt=u;
suf=f[sn][0]+suf;
}
for(R int i=head[u];i;i=e[i].v){
int v=e[i].u;
if(v==fa)continue;
dfs2(v,u);
}
}
inline ll qry(int x,int y){
rt=y;
node D=f[y][0]+f[y][1];
ll ls=dep[y]+dep[D.x]-dep[lca(y,D.x)]*2+Tree1::dist(x,D.x);
ll rs=dep[y]+dep[D.y]-dep[lca(y,D.y)]*2+Tree1::dist(x,D.y);
return max(ls,rs);
}
inline void clear(){
for(R int i=1;i<=n;++i)head[i]=0,f[i][0]=f[i][1]=node();tot=0,cur=0;
rt=1;
}
};
inline void solve(){
cin>>n;
for(R int i=1;i<n;++i){
int u,v,w;cin>>u>>v>>w;
Tree1::add(u,v,w);
Tree1::add(v,u,w);
}
for(R int i=1;i<n;++i){
int u,v,w;cin>>u>>v>>w;
Tree2::add(u,v,w);
Tree2::add(v,u,w);
}
Tree1::dfs_lca(1,0);
Tree2::dfs_lca(1,0);
Tree1::work();
Tree2::work();Tree2::dfs1(1,0);Tree2::dfs2(1,0);
int q;cin>>q;
while(q--){
int x,y;cin>>x>>y;cout<<Tree2::qry(x,y)<<'\n';
}
Tree1::clear();Tree2::clear();
}
signed main(){
freopen("move.in","r",stdin);
freopen("move.out","w",stdout);
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
int _;cin>>_;while(_--)solve();
return 0;
}
詳細信息
Test #1:
score: 0
Dangerous Syscalls
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2