QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#692948#5439. Meet in the Middlemsk_samaTL 0ms0kbC++143.8kb2024-10-31 15:16:432024-10-31 15:16:46

Judging History

你现在查看的是最新测评结果

  • [2024-10-31 15:16:46]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-10-31 15:16:43]
  • 提交

answer

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <ctime>
#include <cstdio>
#include <random>
#include <vector>
#include <bitset>
#include <cassert>
#include <cstring>
#include <algorithm>
#define fi first
#define se second
#define MISAKA main
#define ll long long
#define eb emplace_back
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define _rep(i,a,b) for(int i=a;i>=b;--i)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define FIO(FILE) freopen(FILE".in","r",stdin),freopen(FILE".out","w",stdout)
using namespace std;
inline int read(){
    char ch=getchar();int f=1,x=0;
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*f;
}
const int N=1e5+10,mod=998244353;
namespace lca{
    int k,n,st[N<<1][22],lg[N<<1],d[N],pos[N];
    ll dep[N];vector<pii> g[N];
    void dfs(int u,int f){
        st[++k][0]=u,pos[u]=k;
        for(auto [v,w]:g[u])if(v!=f)
            dep[v]=dep[u]+w,d[v]=d[u]+1,dfs(v,u),st[++k][0]=u;
    }
    inline int _min(int x,int y){return d[x]<d[y]?x:y;}
    inline int _mn(int l,int r){if(l>r)swap(l,r);int k=lg[r-l+1];return _min(st[l][k],st[r-(1<<k)+1][k]);}
    inline ll dis(int x,int y){return dep[x]+dep[y]-2ll*dep[_mn(pos[x],pos[y])];}
    void init(int _n){
        rep(i,1,n) g[i].clear();n=_n,k=0;
        rep(i,2,n){
            int u=read(),v=read(),w=read();
            g[u].eb(v,w),g[v].eb(u,w);
        }
        dfs(1,0);lg[0]=-1;
        rep(i,1,k) lg[i]=lg[i>>1]+1;
        rep(j,1,21)rep(i,1,k-(1<<j)+1)
            st[i][j]=_min(st[i][j-1],st[i+(1<<j-1)][j-1]);
    }
}
int siz[N],vis[N],mn,rt,n,m;ll dep[N],ans[N*5];
vector<pii> g[N],Q[N];pii pr;
void getsiz(int u,int fa){
    siz[u]=1;
    for(auto [v,w]:g[u])if(!vis[v]&&v!=fa)
        getsiz(v,u),siz[u]+=siz[v];
}
void findrt(int u,int fa,int n){
    int mx=n-siz[u];
    for(auto [v,w]:g[u])if(v!=fa&&!vis[v])
        findrt(v,u,n),mx=max(mx,siz[v]);
    if(mx<mn) rt=u,mn=mx;
}
void getdep(int u,int fa){
    for(auto [v,w]:g[u])if(v!=fa&&!vis[v]) 
        dep[v]=dep[u]+w,getdep(v,u);
}
inline ll dis(int x,int y){return lca::dis(x,y)+dep[x]+dep[y];}
inline pii mg(pii a,pii b){
    pii c=a;
    if(dis(a.fi,b.fi)>dis(c.fi,c.se)) c={a.fi,b.fi};
    if(dis(a.fi,b.se)>dis(c.fi,c.se)) c={a.fi,b.se};
    if(dis(a.se,b.fi)>dis(c.fi,c.se)) c={a.se,b.fi};
    if(dis(a.se,b.se)>dis(c.fi,c.se)) c={a.se,b.se};
    if(dis(b.se,b.se)>dis(c.fi,c.se)) c={b.se,b.se};
    return c;
}
void dfs1(int u,int fa){
    for(auto [v,id]:Q[u])
        ans[id]=max(ans[id],max(dis(v,pr.fi),dis(v,pr.se))-dep[v]+dep[u]);
    for(auto [v,w]:g[u])if(v!=fa&&!vis[v]) dfs1(v,u);
}
void dfs2(int u,int fa){
    pr=mg(pr,{u,u});
    for(auto [v,w]:g[u])if(v!=fa&&!vis[v]) dfs2(v,u);
}
void solve(int u){
    vis[u]=1;pr={u,u};dep[u]=0;
    for(auto [v,w]:g[u])if(!vis[v]) dep[v]=w,getdep(v,u);
    for(auto [v,w]:g[u])if(!vis[v]) dfs1(v,u),dfs2(v,u);
    reverse(g[u].begin(),g[u].end());pr={u,u};
    for(auto [v,w]:g[u])if(!vis[v]) dfs1(v,u),dfs2(v,u);
    for(auto [v,id]:Q[u])
        ans[id]=max(ans[id],max(dis(v,pr.fi),dis(v,pr.se))-dep[v]);
    for(auto [v,w]:g[u])if(!vis[v])
        mn=1e9,getsiz(v,0),findrt(v,u,siz[v]),solve(rt);
}
void misaka(){
    rep(i,1,n) g[i].clear(),Q[i].clear(),vis[i]=0;rep(i,1,m) ans[i]=0;
    n=read();
    rep(i,2,n){
        int u=read(),v=read(),w=read();
        g[u].eb(v,w),g[v].eb(u,w);
    }
    lca::init(n);
    m=read();
    rep(i,1,m){
        int u=read(),v=read();
        Q[u].eb(v,i);
    }
    mn=1e9,getsiz(1,0),findrt(1,0,n),solve(rt);
    rep(i,1,m) printf("%lld\n",ans[i]);
}
signed MISAKA(){
    // FIO("move");
    int T=read();
    while(T--) misaka();
    return 0;
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

3 4
1 2 1
2 3 2
1 2 2
2 3 1
1 1
1 2
2 1
2 2

output:


result: