[OC]LCA部分例题题解

问题 A: Dis

[命题人 : admin]

时间限制 : 1.000 sec  内存限制 : 128 MB

提交问题列表解决: 23提交量: 44统计

题目描述

给出 nn 个点的一棵树,多次询问两点之间的最短距离。

注意:边是双向的。

输入

第一行为两个整数 nn 和 mm。nn 表示点数,mm 表示询问次数;

下来 n-1n−1 行,每行三个整数 x ,y, kx,y,k,表示点 xx 和点 yy 之间存在一条边长度为 kk;

再接下来 mm 行,每行两个整数 x, yx,y,表示询问点 xx 到点 yy 的最短距离。

输出

输出 mm 行。对于每次询问,输出一行。

样例输入 Copy

【样例输入1】
2 2 
1 2 100 
1 2 
2 1
【样例输入2】
3 2
1 2 10
3 1 15
1 2
3 2

样例输出 Copy

【样例输出1】
100
100
【样例输出2】
10
25

提示

数据范围与提示

对于全部数据,2\le n\le 10^4,1\le m\le 2\times 10^4,0\lt k\le 100,1\le x,y\le n2≤n≤104,1≤m≤2×104,0<k≤100,1≤x,y≤n。

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int dis[N],father[N],tot,head[N],dep[N],f[N][21];
int n,m,s;
struct node {
    int next,to,dis;
} p[N<<1];
void add(int a,int b,int len) {
    p[++tot].to=b;
    p[tot].next=head[a];
    p[tot].dis=len;
    head[a]=tot;
}
void dfs(int u,int fa) {
    f[u][0]=fa;
    for(int i=1; (1<<i)<=dep[u]; ++i)
        f[u][i]=f[f[u][i-1]][i-1];
    for(int i=head[u]; i; i=p[i].next) {
        if(p[i].to==fa) continue;
        dep[p[i].to]=dep[u]+1;
        dis[p[i].to]=dis[u]+p[i].dis;
        dfs(p[i].to,u);
    }
}
int lca(int x,int y) {
    if(dep[x]>dep[y]) swap(x,y);
    for(int i=20; i>=0; --i)
        if(dep[x]<=dep[y]-(1<<i))
            y=f[y][i];
    if(x==y) return x;
    for(int i=20; i>=0; --i) {
        if(f[x][i]==f[y][i]) continue;
        x=f[x][i];
        y=f[y][i];
    }
    return f[x][0];
}
int main() {
    cin>>n>>m;
    for(int i=1; i<=n-1; ++i) {
        int x,y,l;
        cin>>x>>y>>l;
        add(x,y,l);
        add(y,x,l);
    }
    dfs(1,0);
    for(int i=1; i<=m; ++i) {
        int x,y;
        cin>>x>>y;
        cout<<dis[x]+dis[y]-2*dis[lca(x,y)]<<endl;
    }
    return 0;
}

问题 B: 祖孙询问

[命题人 : admin]

时间限制 : 1.000 sec  内存限制 : 128 MB

提交问题列表解决: 20提交量: 38统计

题目描述

已知一棵 nn 个节点的有根树。有 mm 个询问,每个询问给出了一对节点的编号 xx 和 yy,询问 xx 与 yy 的祖孙关系。

输入

输入第一行包括一个整数 nn 表示节点个数;

接下来 nn 行每行一对整数对 aa 和 bb 表示 aa 和 bb 之间有连边。如果 bb 是 -1−1,那么 aa 就是树的根;

第 n+2n+2 行是一个整数 mm 表示询问个数;

接下来 mm 行,每行两个正整数 xx 和 yy,表示一个询问。

输出

对于每一个询问,若 xx 是 yy 的祖先则输出 11,若 yy 是 xx 的祖先则输出 22,否则输出 00。

样例输入 Copy

10
234 -1
12 234
13 234
14 234
15 234
16 234
17 234
18 234
19 234
233 19
5
234 233
233 12
233 13
233 15
233 19

样例输出 Copy

1
0
0
0
2

提示

数据范围与提示

对于 30\%30% 的数据,1\le n,m\le 10^31≤n,m≤103;
对于 100\%100% 的数据,1\le n,m\le 4\times 10^41≤n,m≤4×104,每个节点的编号都不超过 4\times 10^44×104。

#include<bits/stdc++.h>
#define maxn 40005
using namespace std;
struct node {
    int u,v,nxt;
} e[maxn*4],p[maxn*4];
int n,m,dfn[maxn],fa[maxn],belong[maxn],root,maxid;
int head[maxn],heap[maxn],vis[maxn*4],visid[maxn];
int ord[maxn*4];
int tot;
void adde(int u,int v) {
    e[tot]=(node) {
        u,v,head[u]
    };
    head[u]=tot++;
}
int tpt;
void addp(int u,int v,int pos) {
    p[tpt]=(node) {
        u,v,heap[u]
    };
    ord[tpt]=pos;
    heap[u]=tpt++;
}
int find(int x) {
    if(fa[x]==x)return x;
    return fa[x]=find(fa[x]);
}
int num;
void tarjan(int u,int from) {
    dfn[u]=++num;
    for(int i=head[u]; i!=-1; i=e[i].nxt ) {
        int v=e[i].v;
        if(!dfn[v]) {
            tarjan(v,u);
            fa[v]=u;
            visid[v]=1;
        }
    }
    for(int i=heap[u]; i!=-1; i=p[i].nxt) {
        int vv=p[i].v;
        if(visid[vv]&&!vis[ord[i]]) {
            vis[ord[i]]=find(vv);
        }
    }
}
int main() {
    memset(head,-1,sizeof(head));
    memset(heap,-1,sizeof(heap));
    scanf("%d",&n);
    for(int i=1; i<=n; i++) {
        int u,v;
        scanf("%d%d",&u,&v);
        if(v==-1) {
            root=u;
            continue;
        }
        adde(u,v);
        adde(v,u);
        maxid=max(maxid,max(u,v));
    }
    for(int i=1; i<=maxid; i++)fa[i]=i;
    scanf("%d",&m);
    for(int i=1; i<=m; i++) {
        int u,v;
        scanf("%d%d",&u,&v);
        addp(u,v,i);
        addp(v,u,i);
    }
    visid[root]=1;
    tarjan(root,0);
    for(int i=1; i<=m; i++) {
        int lca=vis[i],pos=(i-1)*2;
        if(lca==p[pos].u ) puts("1");
        else if(lca==p[pos].v )puts("2");
        else puts("0");
    }
}

问题 C: 跳跳棋

[命题人 : admin]

时间限制 : 1.000 sec  内存限制 : 128 MB

提交问题列表解决: 11提交量: 15统计

题目描述

原题来自:BZOJ 2144

跳跳棋是在一条数轴上进行的。棋子只能摆在整点上。每个点不能摆超过一个棋子。我们用跳跳棋来做一个简单的游戏:棋盘上有三颗棋子,分别在 a,b,ca,b,c 这三个位置。我们要通过最少的跳动把他们的位置移动成 x,y,zx,y,z(注意:棋子是没有区别的)。

跳动的规则很简单,任意选一颗棋子,对一颗中轴棋子跳动。跳动后两颗棋子距离不变。一次只允许跳过一颗棋子。

写一个程序,首先判断是否可以完成任务。如果可以,输出最少需要的跳动次数。

输入

第一行包含三个整数,表示当前棋子的位置 a, b, ca,b,c。第二行包含三个整数,表示目标位置 x, y, zx,y,z。

输出

如果无解,输出一行 NO。如果可以到达,第一行输出 YES,第二行输出最少步数。

样例输入 Copy

1 2 3
0 3 5

样例输出 Copy

YES
2

提示

数据范围与提示

对于 20\%20% 的数据,输入整数的绝对值均不超过 1010;
对于 40\%40% 的数据,输入整数的绝对值均不超过 10^4104;
对于 100\%100% 的数据,输入整数的绝对值不超过 10^9109。保证 a,b,ca,b,c 互不相同,x,y,zx,y,z 互不相同。

#include<bits/stdc++.h>
using namespace std;
int t1[5],t2[5];
struct G{
    int x,y,z,times;
}A,B;
int dfs(int a1,int b1,int c1,int k){
    int cnt=0;
    while(1){
        int d1=b1-a1,d2=c1-b1;
        if(d1==d2)break;
        if(d1>d2){
            int t=(d1-1)/d2;
            cnt+=t,b1-=t*d2,c1-=t*d2;
        }else{
            int t=(d2-1)/d1;
            cnt+=t,a1=a1+t*d1,b1=b1+t*d1;
        }
    }
    if(k){
        A.x=a1,A.y=b1,A.z=c1,A.times=cnt;
    }else{
        B.x=a1,B.y=b1,B.z=c1,B.times=cnt;
    }
}
bool check(int t,int a,int b,int c,int x,int y,int z){
    int tot=t;
     
    while(tot){
        int d1=b-a,d2=c-b;
        if(d1==d2)break;
        if(d1>d2){
            int t=min((d1-1)/d2,tot);
            tot=max(0,tot-t),b-=t*d2,c-=t*d2;
        }else{
            int t=min(tot,(d2-1)/d1);
            tot=max(tot-t,0),a=a+t*d1,b=b+t*d1;
        }
    }
    tot=t;
    while(tot){
        int d1=y-x,d2=z-y;
        if(d1==d2)break;
        if(d1>d2){
            int t=min((d1-1)/d2,tot);
            tot=max(tot-t,0),y-=t*d2,z-=t*d2;
        }else{
            int t=min((d2-1)/d1,tot);
            tot=max(tot-t,0),x=x+t*d1,y=y+t*d1;
        }
    }
    if(a==x&&y==b&&z==c)return 1;
    else return 0;
}
int main(){
    int a,b,c,x,y,z;
    for(int i=1;i<=3;i++)scanf("%d",&t1[i]);
    for(int i=1;i<=3;i++)scanf("%d",&t2[i]);
    sort(t1+1,t1+4);
    sort(t2+1,t2+4);
    a=t1[1],b=t1[2],c=t1[3];
    x=t2[1],y=t2[2],z=t2[3];
    dfs(a,b,c,1),dfs(x,y,z,0);
    if(A.x!=B.x||A.y!=B.y||A.z!=B.z){
        printf("NO");
        return 0;
    }else{
        printf("YES\n");
    }
    if(A.times<B.times){
        swap(a,x),swap(b,y),swap(c,z); 
        swap(A.times,B.times);
    }
    int tot=A.times-B.times;
    while(tot){
        int d1=b-a,d2=c-b;
        if(d1==d2)break;
        if(d1>d2){
            int t=min((d1-1)/d2,tot);
            tot-=t,b-=t*d2,c-=t*d2;
        }else{
            int t=min((d2-1)/d1,tot);
            tot-=t,a=a+t*d1,b=b+t*d1;
        }
    }
    int l=0,r=max(A.times,B.times),cnt1=0;
    while(l<=r){
        int mid=(l+r)>>1;
        if(check(mid,a,b,c,x,y,z)){
            r=mid-1;
            cnt1=mid;
        }else{
            l=mid+1;
        }
    }
    printf("%d",cnt1*2+A.times-B.times);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值