• 族谱书


    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define int long long
    using namespace std;
    
    const int A = 1e7+10;
    const int B = 1e7+10;
    const int mod = 1e9 + 7;
    const int inf = 0x3f3f3f3f;
    
    inline int read() {
      char c = getchar();
      int x = 0, f = 1;
      for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
      for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
      return x * f;
    }
    
    struct node {int v,nxt;} e[B<<1];
    
    int depmax,head[B],cnt,n,m,dep[B],mx[B],m1[B],m2[B],f[B],ans[B];
    
    void modify(int u,int v)
    {
        e[++cnt].v=v;
        e[cnt].nxt=head[u];
        head[u]=cnt;
    }
    
    int dfs1(int x,int d)
    {
        mx[x]=dep[x]=d;
        depmax=max(depmax,mx[x]);
        
        for (int i=head[x];i;i=e[i].nxt)
        {
            int v=e[i].v;
            mx[x]=max(mx[x],dfs1(v,d+1));
            
            if (mx[v]>mx[m1[x]])
            {
                m2[x]=m1[x];
                m1[x]=v;
            }
            else if (mx[m2[x]]<mx[v]) 
            {
                m2[x]=v;
            }
        }
        
        return mx[x];
    }
    
    int dfs2(int u,int target)
    {
        if (mx[m2[u]]>=target) return u;
        if (!mx[m1[u]] || dep[u]==target) return u;
        
        return dfs2(m1[u],target);
    }
    
    main()
    {
        int x;
        n=read(),m=read();
        for (int i=1;i<=n;i++)
        {
            f[i]=read();
            modify(f[i],i);      
        }
        
        dfs1(1,1);
        ans[0]=1;    
        for (int i=1;i<=depmax;i++) ans[i]=dfs2(ans[i-1],i);
        
        for (int i=1;i<=m;i++)
        { 
            x=read(); 
            printf("%lld
    ",ans[x]);
        }
    }
    ```c
  • 相关阅读:
    linux下的磁盘挂载
    shell中的循环语句while
    hadoop安装和配置
    shell 命令 创建/删除 软连接 ln -s
    azkaban disable 停用部分工作流
    git dev 分支merge到master
    shell 命令 zip unzip
    git代码同步服务器代码需要注意的问题
    shell 命令 if elif else fi 用法
    python 引入本地 module
  • 原文地址:https://www.cnblogs.com/lToZvTe/p/14550191.html
Copyright © 2020-2023  润新知