QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#562504 | #9295. Treeshop | zjy0001 | WA | 0ms | 41596kb | C++17 | 8.2kb | 2024-09-13 18:00:20 | 2024-09-13 18:00:20 |
Judging History
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];
pair<int,int>dp[N];
int sz[N],son[N],d[N],f[N],top[N],dL[N],dR[N],bl[N],rk[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);
copy(dp[u],dp[u]+3,tmp);
copy(dp[v],dp[v]+3,tmp+3);
for(int i=3;i<6;++i)++tmp[i].first;
sort(tmp,tmp+6,greater<>());
copy(tmp,tmp+3,dp[u]);
}
}
inline void Dp2(int u,int fa){
int mx=-INF,se=-INF;
for(auto v:G[u])if(v!=fa){
if(mx<dp[v][0].first+1)se=mx,mx=dp[v][0].first+1;
else se=max(se,dp[v][0].first+1);
}
for(auto v:G[u])if(v!=fa)
if(mx==dp[v][0].first+1)df[v]=se+1;
else df[v]=mx+1;
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=0;
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 void dfs(int u,int fa,int k){
if(G[u].size()==1)bl[u]=k;
for(auto v:G[u])if(v!=fa)dfs(v,u,k);
}
inline void Dp1(int u,int fa){
if(bl[u])dp[u].first=u;
for(auto v:G[u])if(v!=fa){
Dp1(v,u);
auto [a,b]=dp[u];auto [c,d]=dp[v];
if(c&&(!a||dist(a,u)<dist(c,u)))swap(a,c);
if(d&&(!a||dist(a,u)<dist(d,u)))swap(a,d);
if(c&&(!b||dist(b,u)<dist(c,u)))swap(b,c);
if(d&&(!b||dist(b,u)<dist(d,u)))swap(b,d);
if(d&&(!c||dist(c,u)<dist(d,u)))swap(c,d);
dp[u].first=a;
if(b&&bl[a]!=bl[b])dp[u].second=b;
else if(c&&bl[a]!=bl[c])dp[u].second=c;
else if(d&&bl[a]!=bl[d])dp[u].second=d;
}
}
inline void Dp2(int u,int fa){
for(auto v:G[u])if(v!=fa){
auto [a,b]=dp[u];auto [c,d]=dp[v];
if(c&&(!a||dist(a,v)<dist(c,v)))swap(a,c);
if(d&&(!a||dist(a,v)<dist(d,v)))swap(a,d);
if(c&&(!b||dist(b,v)<dist(c,v)))swap(b,c);
if(d&&(!b||dist(b,v)<dist(d,v)))swap(b,d);
if(d&&(!c||dist(c,v)<dist(d,v)))swap(c,d);
dp[v].first=a;
if(b&&bl[a]!=bl[b])dp[v].second=b;
else if(c&&bl[a]!=bl[c])dp[v].second=c;
else if(d&&bl[a]!=bl[d])dp[v].second=d;
Dp2(v,u);
}
}
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),dfs(f[rt],rt,++cnt);
else for(auto v:G[rt])dfs(v,rt,++cnt);
Dp1(1,0),Dp2(1,0);
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;
const auto [a,b]=dp[u];const auto [c,d]=dp[v];
int sa=max(dist(a,u)+H.solve(a,v),dist(c,v)+H.solve(u,c));
int sb=T.solve(u,v);cout<<sa<<endl;
return min(max(0,(L-sa+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;
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: 0
Wrong Answer
time: 0ms
memory: 41596kb
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 6 3 0 1
result:
wrong answer 5th numbers differ - expected: '3', found: '6'