QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#400952#5439. Meet in the MiddlehuzhaoyangWA 4ms19296kbC++143.1kb2024-04-27 18:59:402024-04-27 18:59:40

Judging History

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

  • [2024-04-27 18:59:40]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:19296kb
  • [2024-04-27 18:59:40]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef pair<int,int> pii;
const int N=100005;
int n,m,q,rt,x,y,z,lg[N],pos[N],a[N],vis[N],sz[N],fa[N],Dep[N];
ll ans,mn[N][20],dep[N],Dis[20][N];
pii pre[N],suf[N],mx[N],Mx[N];
vector<pii>e[N],E[N];
void dfs(int k,int f,ll s){
    mn[++m][0]=s,pos[k]=m,dep[k]=s;
    for(pii i:e[k])
        if (i.fi!=f)dfs(i.fi,k,s+i.se),mn[++m][0]=s;
}
ll dis(int x,int y){
    int l=pos[x],r=pos[y];
    if (l>r)swap(l,r);
    int m=lg[r-l+1];
    return dep[x]+dep[y]-(min(mn[l][m],mn[r-(1<<m)+1][m])<<1);
}
ll calc(int t,int x,int y){
    if ((!x)&&(!y))return -2;
    if ((!x)||(!y))return -1;
    return dis(x,y)+Dis[t][x]+Dis[t][y];
}
void add(int t,pii &x,int y){
    ll s=calc(t,x.fi,x.se),a=calc(t,x.fi,y),b=calc(t,x.se,y);
    if (s<max(a,b)){
        if (a>b)x.se=y;
        else x.fi=y;
    }
}
pii merge(int t,pii x,pii y){
    add(t,x,y.fi),add(t,x,y.se);
    return x;
}
ll query(int t,pii x,int y){
    return max(calc(t,x.fi,y),calc(t,x.se,y));
}
void get_sz(int k,int f){
    sz[k]=1;
    for(pii i:E[k])
        if ((!vis[i.fi])&&(i.fi!=f))get_sz(i.fi,k),sz[k]+=sz[i.fi];
}
void get_rt(int k,int f,int s){
    int mx=s-sz[k];
    for(pii i:E[k])
        if ((!vis[i.fi])&&(i.fi!=f))get_rt(i.fi,k,s),mx=max(mx,sz[i.fi]);
    if (mx<=(s>>1))rt=k;
}
void get_Dis(int t,int k,int f,ll s){
    Dis[t][k]=s;
    for(pii i:E[k])
        if ((!vis[i.fi])&&(i.fi!=f))get_Dis(t,i.fi,k,s+i.se);
}
void get_mx(int t,int k,int f,pii &mx){
    add(t,mx,k);
    for(pii i:E[k])
        if ((!vis[i.fi])&&(i.fi!=f))get_mx(t,i.fi,k,mx);
}
int Dfs(int t,int k){
    get_sz(k,0),get_rt(k,0,sz[k]);
    k=rt,vis[k]=1,Dep[k]=t,get_Dis(t,k,0,0);
    a[0]=0;
    for(pii i:E[k])
        if (!vis[i.fi])a[++a[0]]=i.fi;
    for(int i=1;i<=a[0];i++)pre[i]=pre[i-1],get_mx(t,a[i],0,pre[i]);
    mx[k]=pre[a[0]],add(t,mx[k],k);
    reverse(a+1,a+a[0]+1);
    for(int i=1;i<=a[0];i++)suf[i]=suf[i-1],get_mx(t,a[i],0,suf[i]);
    vector<pii>v(a[0]);
    for(int i=1;i<=a[0];i++)v[i-1]=merge(t,suf[i-1],pre[a[0]-i]);
    for(pii i:E[k])
        if (!vis[i.fi]){
            int s=Dfs(t+1,i.fi);
            fa[s]=k,Mx[s]=v.back(),v.pop_back();
        }
    return k;
}
int main(){
    scanf("%d%d",&n,&q);
    for(int i=1;i<n;i++){
        scanf("%d%d%d",&x,&y,&z);
        E[x].push_back(make_pair(y,z));
        E[y].push_back(make_pair(x,z));
    }
    for(int i=1;i<n;i++){
        scanf("%d%d%d",&x,&y,&z);
        e[x].push_back(make_pair(y,z));
        e[y].push_back(make_pair(x,z));
    }
    dfs(1,0,0);
    lg[0]=-1;
    for(int i=1;i<=m;i++)lg[i]=lg[i>>1]+1;
    for(int j=1;j<20;j++)
        for(int i=m-(1<<j)+1;i>0;i--)mn[i][j]=min(mn[i][j-1],mn[i+(1<<j-1)][j-1]);
    Dfs(0,1);
    while (q--){
        scanf("%d%d",&x,&y);
        ans=query(Dep[y],mx[y],x)-Dis[Dep[y]][x];
        for(int i=y;fa[i];i=fa[i])ans=max(ans,Dis[Dep[i]-1][y]+query(Dep[i]-1,Mx[i],x)-Dis[Dep[i]-1][x]);
        printf("%lld\n",ans);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 19296kb

input:

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

output:

6
5
4
3

result:

wrong answer 2nd numbers differ - expected: '4', found: '5'