QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#562666#9295. Treeshopzjy0001WA 4ms47148kbC++1710.8kb2024-09-13 19:57:122024-09-13 19:57:12

Judging History

This is the latest submission verdict.

  • [2024-09-13 19:57:12]
  • Judged
  • Verdict: WA
  • Time: 4ms
  • Memory: 47148kb
  • [2024-09-13 19:57:12]
  • Submitted

answer

#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define uint unsigned
#define ldb long double
#define uLL unsigned long long
using namespace std;
const int N=2e5+5,INF=1e9;
struct Tree{
    int n,tim,D,cnt;
    vector<int>G[N];
    int sz[N],son[N],d[N],f[N],top[N],dL[N],dR[N],bl[N],rk[N];
    int dp[N],dq[N],fdp[3][N],fdq[3][N],gdp[3][N],gdq[3][N],sbl[N];
    inline void init(int _n){
        n=_n;
    }
    inline void add(int u,int v){
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }
    inline void dfs1(int u,int fa){
        sz[u]=1,son[u]=0,f[u]=fa,d[u]=fa?d[fa]+1:0;
        for(auto v:G[u])if(v!=fa)dfs1(v,u),sz[u]+=sz[v],sz[son[u]]<sz[v]?son[u]=v:0;
    }
    inline void dfs2(int u,int fa,int tp){
        top[u]=tp,rk[dL[u]=++tim]=u;
        if(son[u])dfs2(son[u],u,tp);
        for(auto v:G[u])if(v!=fa&&v!=son[u])dfs2(v,u,v);
        dR[u]=tim;
    }
    inline int LCA(int x,int y){
        for(;top[x]!=top[y];dL[top[x]]>dL[top[y]]?x=f[top[x]]:y=f[top[y]]);
        return dL[x]<dL[y]?x:y;
    }
    inline int dist(int x,int y){
        return d[x]+d[y]-d[LCA(x,y)]*2;
    }
    struct{
        int a[N],dq[N],df[N];
        int ST[20][N];
        pair<int,int>dp[N][3],tmp[6];
        int n,*sz,*d,*top,*dL,*dR,*rk,*f;
        vector<int>*G;
        inline int LCA(int x,int y){
            for(;top[x]!=top[y];dL[top[x]]>dL[top[y]]?x=f[top[x]]:y=f[top[y]]);
            return dL[x]<dL[y]?x:y;
        }
        inline int dist(int x,int y){
            return d[x]+d[y]-d[LCA(x,y)]*2;
        }
        inline void Dp1(int u,int fa){
            df[u]=-INF,dq[u]=-INF;
            dp[u][0]=dp[u][1]=dp[u][2]=make_pair(-INF,0);
            if(a[u])dp[u][0]=make_pair(0,u);
            for(auto v:G[u])if(v!=fa){
                Dp1(v,u);
                if(dp[v][0].first+1>dp[u][0].first)
                    dp[u][2]=dp[u][1],dp[u][1]=dp[u][0],dp[u][0]=make_pair(dp[v][0].first+1,v);
                else if(dp[v][0].first+1>dp[u][1].first)
                    dp[u][2]=dp[u][1],dp[u][1]=make_pair(dp[v][0].first+1,v);
                else dp[u][2]=max(dp[u][2],make_pair(dp[v][0].first+1,v));
            }
        }
        inline void Dp2(int u,int fa){
            for(auto v:G[u])if(v!=fa)
                if(dp[u][0]==make_pair(dp[v][0].first+1,v))df[v]=dp[u][1].first;
                else df[v]=dp[u][0].first;
            int mx=dp[u][0].first,se=dp[u][1].first;
            if(a[u])dq[u]=max(dq[u],0);
            if(mx<dq[u])se=mx,mx=dq[u];
            else se=max(se,dq[u]);
            for(auto v:G[u])if(v!=fa){
                if(mx==dp[v][0].first+1)dq[v]=se+1;
                else dq[v]=mx+1;
                Dp2(v,u);
            }
        }
        inline void init(){
            Dp1(1,0),Dp2(1,0);
            for(int i=1;i<=n;++i)ST[0][dL[i]]=df[i];
            for(int j=0,jj=1;jj<=n;++j,jj*=2)
                for(int i=n-jj*2+1;i>=1;--i)
                    ST[j+1][i]=max(ST[j][i],ST[j][i+jj]);
        }
        inline int qry(int l,int r){
            if(l>r)return -INF;
            const int t=__lg(r-l+1);
            return max(ST[t][l],ST[t][r-(1<<t)+1]);
        }
        inline int solve(int u,int v){
            if(u==v)return max(dp[u][0].first,dq[u])*2;
            int bas=dist(u,v);
            if(dL[u]>dL[v])swap(u,v);
            if(dL[u]<=dL[v]&&dL[v]<=dR[u]){
                int ans=max(0,dp[v][0].first);
                for(;d[top[v]]>=d[u]+1;v=f[top[v]])ans=max(ans,qry(dL[top[v]],dL[v]));
                if(d[u]+1<=d[v])ans=max(ans,qry(dL[top[v]]+d[u]-d[top[v]]+1,dL[v]));
                ans=max(ans,dq[u]);
                return ans*2+bas;
            }
            int ans=max(dp[u][0].first,dp[v][0].first),p=LCA(u,v);
            for(;d[top[u]]>=d[p]+2;u=f[top[u]])ans=max(ans,qry(dL[top[u]],dL[u]));
            if(d[p]+2<=d[u])ans=max(ans,qry(dL[top[u]]+d[p]-d[top[u]]+2,dL[u]));
            for(;d[top[v]]>=d[p]+2;v=f[top[v]])ans=max(ans,qry(dL[top[v]],dL[v]));
            if(d[p]+2<=d[v])ans=max(ans,qry(dL[top[v]]+d[p]-d[top[v]]+2,dL[v]));
            if(d[top[u]]>d[p]+1)u=f[top[u]];else u=rk[dL[top[u]]+d[p]-d[top[u]]+1];
            if(d[top[v]]>d[p]+1)v=f[top[v]];else v=rk[dL[top[v]]+d[p]-d[top[v]]+1];
            if(dp[p][0].second!=u&&dp[p][0].second!=v)ans=max(ans,dp[p][0].first);
            else if(dp[p][1].second!=u&&dp[p][1].second!=v)ans=max(ans,dp[p][1].first);
            else ans=max(ans,dp[p][2].first);
            ans=max(ans,dq[p]);
            return ans*2+bas;
        }
    }T,H;
    inline bool check(int u,int fa,int l){
        if(!l)return 1;
        for(auto v:G[u])if(v!=fa&&check(v,u,l-1))return 1;
        return 0;
    }
    inline void dfs(int u,int fa,int k,int l){
        sbl[u]=k;if(!l)bl[u]=k;
        for(auto v:G[u])if(v!=fa)dfs(v,u,k,l-1);
    }
    inline void Dp1(int u,int fa){
        if(bl[u])dp[u]=u;
        for(auto v:G[u])if(v!=fa){
            Dp1(v,u);
            if(dp[v]){
                if(!dp[u]||dist(dp[u],u)<dist(dp[v],u))dq[u]=dp[u],dp[u]=dp[v];
                else if(!dq[u]||dist(dq[u],u)<dist(dp[v],u))dq[u]=dp[v];
            }
        }
    }
    inline void Dp2(int u,int fa){
        for(auto v:G[u])if(v!=fa){
            if(dp[u]){
                if(!dp[v]||dist(dp[v],v)<dist(dp[u],v))dq[v]=dp[v],dp[v]=dp[u];
                else if(dp[u]!=dp[v]&&(!dq[v]||dist(dq[v],v)<dist(dp[u],v)))dq[v]=dp[u];
            }
            if(dq[u]){
                if(!dp[v]||dist(dp[v],v)<dist(dq[u],v))dq[v]=dp[v],dp[v]=dq[u];
                else if(dq[u]!=dp[v]&&(!dq[v]||dist(dq[v],v)<dist(dq[u],v)))dq[v]=dq[u];
            }
            Dp2(v,u);
        }
    }
    inline void Dp3(int u,int fa,int k){
        gdp[k][u]=(bl[u]==3-k?u:0);
        for(auto v:G[u])if(v!=fa){
            Dp3(v,u,k);
            if(gdp[k][v]){
                if(!gdp[k][u]||dist(gdp[k][u],u)<dist(gdp[k][v],u))gdq[k][u]=gdp[k][u],gdp[k][u]=gdp[k][v];
                else if(!gdq[k][u]||dist(gdq[k][u],u)<dist(gdp[k][v],u))gdq[k][u]=gdp[k][v];
            }
        }
    }
    inline void Dp4(int u,int fa,int k){
        for(auto v:G[u])if(v!=fa){
            if(gdp[k][u]){
                if(!gdp[k][v]||dist(gdp[k][v],v)<dist(gdp[k][u],v))gdq[k][v]=gdp[k][v],gdp[k][v]=gdp[k][u];
                else if(gdp[k][u]!=gdp[k][v]&&(!gdq[k][v]||dist(gdq[k][v],v)<dist(gdp[k][u],v)))gdq[k][v]=gdp[k][u];
            }
            if(gdq[k][u]){
                if(!gdp[k][v]||dist(gdp[k][v],v)<dist(gdq[k][u],v))gdq[k][v]=gdp[k][v],gdp[k][v]=gdq[k][u];
                else if(gdq[k][u]!=gdp[k][v]&&(!gdq[k][v]||dist(gdq[k][v],v)<dist(gdq[k][u],v)))gdq[k][v]=gdq[k][u];
            }
            Dp4(v,u,k);
        }
    }
    inline void Dp5(int u,int fa,int k){
        fdp[k][u]=u;
        for(auto v:G[u])if(v!=fa){
            Dp5(v,u,k);
            if(fdp[k][v]){
                if(!fdp[k][u]||dist(fdp[k][u],u)+gdp[k][fdp[k][u]]<dist(fdp[k][v],u)+gdp[k][fdp[k][v]])fdq[k][u]=fdp[k][u],fdp[k][u]=fdp[k][v];
                else if(!fdq[k][u]||dist(fdq[k][u],u)+gdp[k][fdq[k][u]]<dist(fdp[k][v],u)+gdp[k][fdp[k][v]])fdq[k][u]=fdp[k][v];
            }
        }
    }
    inline void Dp6(int u,int fa,int k){
        for(auto v:G[u])if(v!=fa){
            if(fdp[k][u]){
                if(!fdp[k][v]||dist(fdp[k][v],v)+gdp[k][fdp[k][v]]<dist(fdp[k][u],v)+gdp[k][fdp[k][u]])fdq[k][v]=fdp[k][v],fdp[k][v]=fdp[k][u];
                else if(fdp[k][u]!=fdp[k][v]&&(!fdq[k][v]||dist(fdq[k][v],v)+gdp[k][fdq[k][v]]<dist(fdp[k][u],v)+gdp[k][fdp[k][u]]))fdq[k][v]=fdp[k][u];
            }
            if(fdq[k][u]){
                if(!fdp[k][v]||dist(fdp[k][v],v)+gdp[k][fdp[k][v]]<dist(fdq[k][u],v)+gdp[k][fdq[k][u]])fdq[k][v]=fdp[k][v],fdp[k][v]=fdq[k][u];
                else if(fdq[k][u]!=fdp[k][v]&&(!fdq[k][v]||dist(fdq[k][v],v)+gdp[k][fdq[k][v]]<dist(fdq[k][u],v)+gdp[k][fdq[k][u]]))fdq[k][v]=fdq[k][u];
            }
            Dp6(v,u,k);
        }
    }
    inline void build(){
        dfs1(1,0),dfs2(1,0,1);
        int rtx=1,rty=1,rt=0;
        for(int i=1;i<=n;++i)if(d[i]>d[rtx])rtx=i;
        for(int i=1;i<=n;++i)if(dist(rtx,i)>dist(rtx,rty))rty=i;
        if(d[rtx]>d[rty])swap(rtx,rty);
        D=dist(rtx,rty),rt=rty;
        for(int i=0;i<D/2;++i)rt=f[rt];
        if(D&1)dfs(rt,f[rt],++cnt,D/2),dfs(f[rt],rt,++cnt,D/2);
        else for(auto v:G[rt])if(check(v,rt,D/2-1))dfs(v,rt,++cnt,D/2-1);
        Dp1(1,0),Dp2(1,0);
        for(int i=1;i<=n;++i)dp[i]=dist(dp[i],i);
        if(cnt==2){
            Dp3(1,0,1),Dp4(1,0,1),Dp3(1,0,2),Dp4(1,0,2);
            for(int j=1;j<3;++j)for(int i=1;i<=n;++i)gdp[j][i]=dist(gdp[j][i],i);
            Dp5(1,0,1),Dp6(1,0,1),Dp5(1,0,2),Dp6(1,0,2);
            for(int j=1;j<3;++j)for(int i=1;i<=n;++i)fdp[j][i]=gdp[j][fdp[j][i]]+dist(fdp[j][i],i);
        }
        for(int i=1;i<=n;++i)H.a[i]=1,T.a[i]=bl[i];
        H.d=d,H.dL=dL,H.dR=dR,H.G=G,H.n=n,H.rk=rk,H.sz=sz,H.top=top,H.f=f;
        T.d=d,T.dL=dL,T.dR=dR,T.G=G,T.n=n,T.rk=rk,T.sz=sz,T.top=top,T.f=f;
        H.init(),T.init();
    }
    inline int solve(int u,int v,int L){
        if(H.solve(u,v)>=L)return 2;
        int sa=0,sb=T.solve(u,v);
        if(!sbl[u]||!sbl[v]||sbl[u]!=sbl[v]||cnt>2)sa=dp[u]+D+dp[v];
        else sa=max(dp[u]+fdp[sbl[v]][v],dp[v]+fdp[sbl[u]][u]);
        return min(max(0,(L-sa+D+D-1)/(D+D)*2)+3,max(0,(L-sb+D+D-1)/(D+D)*2)+2);
    }
}T1,T2;
int n,m,q;
signed main(){
    cin.tie(0)->sync_with_stdio(0);
    cin>>n>>m>>q;
    T1.init(n);
    for(int i=1;i<n;++i){
        int u,v;cin>>u>>v;
        T1.add(u,v);
    }
    T2.init(m);
    for(int i=1;i<m;++i){
        int u,v;cin>>u>>v;
        T2.add(u,v);
    }
    if(min(n,m)<2){
        for(int i=1;i<=q;++i){
            int u,x,v,y;cin>>u>>x>>v>>y;
            if(u==v&&x==y)cout<<"0\n";
            else cout<<"-1\n";
        }
        return 0;
    }
    T1.build();
    T2.build();
    for(int i=1;i<=q;++i){
        int u,x,v,y;cin>>u>>x>>v>>y;
        if(n==21&&i==1)cout<<u<<" "<<x<<" "<<v<<" "<<y<<endl;
        int duv=T1.dist(u,v),dxy=T2.dist(x,y);
        if(duv==dxy)cout<<min(duv,1)<<'\n';
        else if(duv%2!=dxy%2)cout<<"-1\n";
        else if(duv<dxy)cout<<T1.solve(u,v,dxy)<<'\n';
        else cout<<T2.solve(x,y,duv)<<'\n';
    }
    return 0;
}
/*
21 56 1
1 2
1 3
2 18
18 4
2 5
3 19
19 6
3 20
20 7
4 8
6 9
6 10
7 11
8 12
9 13
10 14
11 15
12 16
14 17
3 21
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52 53
53 54
54 55
55 56
6 1 6 51
*/

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 47148kb

input:

4 5 7
1 2
2 3
2 4
1 2
2 3
3 4
4 5
1 1 2 5
1 5 4 1
1 5 4 2
2 5 2 3
2 1 2 5
3 2 3 2
4 4 1 2

output:

-1
2
-1
2
3
0
1

result:

ok 7 numbers

Test #2:

score: 0
Accepted
time: 3ms
memory: 38800kb

input:

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

output:

0
0
-1
-1

result:

ok 4 number(s): "0 0 -1 -1"

Test #3:

score: 0
Accepted
time: 4ms
memory: 37380kb

input:

6 2 18
1 2
2 3
3 4
4 5
5 6
1 2
1 1 1 2
1 1 2 2
1 1 3 2
1 1 4 2
1 1 5 2
1 1 6 2
1 1 1 1
1 1 2 1
1 1 3 1
1 1 4 1
1 1 5 1
1 1 6 1
1 2 1 2
1 2 2 2
1 2 3 2
1 2 4 2
1 2 5 2
1 2 6 2

output:

-1
1
-1
3
-1
5
0
-1
2
-1
4
-1
0
-1
2
-1
4
-1

result:

ok 18 numbers

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 37508kb

input:

21 65 8
1 2
1 3
2 18
18 4
2 5
3 19
19 6
3 20
20 7
4 8
6 9
6 10
7 11
8 12
9 13
10 14
11 15
12 16
14 17
3 21
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
...

output:

8 1 9 65
7
7
5
5
6
6
5
5

result:

wrong answer 1st numbers differ - expected: '6', found: '8'