HDU 6031 Innumerable Ancestors

树状数组,倍增,枚举,$dfs$序。

对于每一次的询问,可以枚举$B$集合中的所有点,对于每一个点,在树上二分$LCA$,找到最低的更新答案。

判断是否是$LCA$可以搞个$dfs$序,将$A$集合中所有点标$1$,然后查询子树对应的区间上的区间和。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 100010;
int L[maxn],R[maxn];
int c[2*maxn];
int h[maxn];
int to[2*maxn];
int nx[2*maxn];
int sz;
int tt;
int dp[100010][25];
int dep[100010];
int ans;
int n,m;

int k1,k2;
int A[100010],B[100010];

void add(int a,int b)
{
    to[sz] = b;
    nx[sz] = h[a];
    h[a] = sz++;
}

void dfs(int x,int y)
{
    L[x] = tt; tt++;
    dp[x][0] = y;

    if(y==-1) dep[x]=1;
    else dep[x] = dep[y]+1;

    for(int i=h[x];i!=-1;i = nx[i])
    {
        if(L[to[i]]) continue;
        dfs(to[i],x);
    }
    R[x] = tt; tt++;
}

int lowbit(int x)
{
    return x&(-x);
}

void update(int pos,int val)
{
    for(int i=pos;i<=2*n;i=i+lowbit(i))
    {
        c[i] += val;
    }
}

int get(int pos)
{
    int res=0;

    for(int i=pos;i>0;i=i-lowbit(i))
    {
        res=res+c[i];
    }

    return res;
}

void work(int x)
{
    while(1)
    {
        int Q = dp[x][0];
        if(get(R[Q])-get(L[Q]-1))
        {
            ans = max(ans,dep[Q]);
            break;
        }

        for(int i=17;i>=0;i--)
        {
            int to = dp[x][i];
            if(to==-1) continue;
            if(get(R[to])-get(L[to]-1)) continue;
            x=to;
            break;
        }
    }

    x = dp[x][0];
    ans = max(ans,dep[x]);
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        sz=0;

        for(int i=0;i<=n;i++)
        {
            c[i]=0;
            h[i]=-1;
            L[i]=0;
            R[i]=0;
        }

        for(int i=1;i<=n-1;i++)
        {
            int a,b; scanf("%d%d",&a,&b);
            add(a,b); add(b,a);
        }

        tt=1; dfs(1,-1);

        for(int j=1;j<=17;j++)
        {
            for(int i=1;i<=n;i++)
            {
                if(dp[i][j-1]==-1) dp[i][j]=-1;
                else dp[i][j] = dp[dp[i][j-1]][j-1];
            }
        }

        for(int i=1;i<=m;i++)
        {
            scanf("%d",&k1); for(int j=1;j<=k1;j++) scanf("%d",&A[j]), update(L[A[j]],1);
            scanf("%d",&k2);

            ans=0;

            for(int j=1;j<=k2;j++)
            {
                scanf("%d",&B[j]);
                if(get(R[B[j]])-get(L[B[j]]-1))
                {
                    ans = max(ans,dep[B[j]]);
                    continue;
                }

                work(B[j]);
            }

            printf("%d\n",ans);

            for(int j=1;j<=k1;j++) update(L[A[j]],-1);
        }
    }
    return 0;
}
时间: 2024-08-10 02:10:46

HDU 6031 Innumerable Ancestors的相关文章

hdu 6031 Innumerable Ancestors(LCA+剪枝)

题目链接:hdu 6031 Innumerable Ancestors 题意: 给你一棵n个节点的树,现在有m个询问,每次给你两个点集a,b. 让你从a,b点集中选两个点x,y,使得这两个点的LCA的深度最大. 题解: 标解应该是二分+LCA,不过我试了一下暴力,稍微剪了点枝,就直接过去了. 具体看代码 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 using namespace std; 4 c

hdu 6031 Innumerable Ancestors (虚树 lca)

hdu6031 一棵树(根节点是 1),给出两个集合,集合是 由树上的节点组成的.从两个集合中分别选一个元素,求出他们的 lca,问:lca 的深度最大是多少. 每个询问,两个集合,建虚树,然后dfs一遍,记录子树有没有A点.有没有B点,然后看既有A点又有B点的就更新深度到答案. #include <bits/stdc++.h> using namespace std; const int maxN=100000+10; const int maxM=100000+10; const int

HDU6031 Innumerable Ancestors 倍增 - 题意详细概括 - 算法详解

题目 查看原题 - HDU6031 Innumerable Ancestors 题目描述 有一棵有n个节点的有根树,根节点为1,其深度为1,现在有m个询问,每次询问给出两个集合A和B,问LCA(x,y)(x∈A,y∈B)的深度最大为多少. 输入描述 有多组数据(数据组数<=5) 对于每一组数据,首先2个数n,m,表示有根树的节点个数和询问个数.然后n-1行,每行2个数a,b表示节点a和节点b之间存在直接的连边:接下去2m行,每两行,分别描述当前询问的集合A和集合B:对于一个集合,用一行来描述,该

hdu6031 Innumerable Ancestors

hdu6031 Innumerable Ancestors 倍增 题意 给定一张无向图,对于每组询问,给出两个集合 A 与 B 求 lca(x,y) 最深的时候的深度 x属于A y属于B 题解 首先 我们发现答案具有单调性,于是我们就可以二分这个答案 mid 然后 把 a 集合 的 这个深度的祖先 求出来 然后看 与b 集合的这个深度 祖先 是否有交集就行了 用set 判断 是否有交集 用倍增法 快速求出一个点的祖先 求一次 只要 log n 1 #include <bits/stdc++.h>

HDU 1330 Nearest Common Ancestors(求两个点的近期公共祖先)

题目链接:id=1330">传送门 在线算法: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 40010; struct nod{ int to,next,w; }edge[maxn*2]; int head[maxn],ip,tot; bool vis[ma

HDU 1330 Nearest Common Ancestors(求两个点的最近公共祖先)

题目链接:传送门 在线算法: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 40010; struct nod{ int to,next,w; }edge[maxn*2]; int head[maxn],ip,tot; bool vis[maxn]; int R[maxn*2

LCA(最近公共祖先)--tarjan离线算法 hdu 2586

HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11320    Accepted Submission(s): 4119 Problem Description There are n houses in the village and some bidirectional roads c

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

HDU 4781 Assignment For Princess(YY乱搞)

http://acm.hdu.edu.cn/showproblem.php?pid=4781 Assignment For Princess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 639    Accepted Submission(s): 196 Special Judge Problem Description Long