zoukankan      html  css  js  c++  java
  • Lightoj 1128

    Gate

    倍增模板,在一个严格小根堆中,给定$x,y$,求$x$的祖先中$≥y$的最高点。

    注意清零

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #define MogeKo qwq
    using namespace std;
    const int maxn = 1e5+10;
    int t,n,q,val[maxn],cnt;
    int dpth[maxn],p[maxn][20];
    int to[maxn],head[maxn],nxt[maxn];
    
    void add(int x,int y) {
        to[++cnt] = y;
        nxt[cnt] = head[x];
        head[x] = cnt;
    }
    
    void dfs(int x,int fa) {
        dpth[x] = dpth[fa]+1;
        p[x][0] = fa;
        for(int i = 1; (1<<i)<=dpth[x]; i++)
            p[x][i] = p[p[x][i-1]][i-1];
        for(int i = head[x]; i; i = nxt[i]) {
            if(to[i] == fa)continue;
            dfs(to[i],x);
        }
    }
    
    int solve(int x,int y) {
        for(int i = 19; i >= 0; i--) {
            if(val[p[x][i]] >= y) x = p[x][i];
            if(x==0 || val[x]==y)break;
        }
        return x;
    }
    
    int main() {
        scanf("%d",&t);
        for(int j = 1;j <= t;j++) {
            printf("Case %d:
    ",j);
            memset(to,0,sizeof(to));
            memset(head,0,sizeof(head));
            memset(nxt,0,sizeof(nxt));
            memset(p,0,sizeof(p));
            cnt = 0;
            val[0] = 1;
            dpth[0] = 0;
            scanf("%d%d",&n,&q);
            int x,y;
            for(int i = 1; i <= n-1; i++) {
                scanf("%d%d",&x,&y);
                add(x,i);
                val[i] = y;
            }
            dfs(0,0);
            for(int i = 1; i <= q; i++) {
                scanf("%d%d",&x,&y);
                printf("%d
    ",solve(x,y));
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    JS定时执行,循环执行
    Ecshop(二次开发)
    百度歌曲接口
    给大家讲讲在哪些地方发外链最好
    360浏览器默认以兼容模式或急速模式方式打开页面
    子iframe 怎么调用 父级的JS函数
    ASP 发送邮件
    PHP发送邮件
    php表单数据验证类
    js获取url传递参数
  • 原文地址:https://www.cnblogs.com/mogeko/p/10609251.html
Copyright © 2011-2022 走看看