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

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

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

#include <bits/stdc++.h>
using namespace std; const int maxn = ;
int L[maxn],R[maxn];
int c[*maxn];
int h[maxn];
int to[*maxn];
int nx[*maxn];
int sz;
int tt;
int dp[][];
int dep[];
int ans;
int n,m; int k1,k2;
int A[],B[]; 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][] = y; if(y==-) dep[x]=;
else dep[x] = dep[y]+; for(int i=h[x];i!=-;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<=*n;i=i+lowbit(i))
{
c[i] += val;
}
} int get(int pos)
{
int res=; for(int i=pos;i>;i=i-lowbit(i))
{
res=res+c[i];
} return res;
} void work(int x)
{
while()
{
int Q = dp[x][];
if(get(R[Q])-get(L[Q]-))
{
ans = max(ans,dep[Q]);
break;
} for(int i=;i>=;i--)
{
int to = dp[x][i];
if(to==-) continue;
if(get(R[to])-get(L[to]-)) continue;
x=to;
break;
}
} x = dp[x][];
ans = max(ans,dep[x]);
} int main()
{
while(~scanf("%d%d",&n,&m))
{
sz=; for(int i=;i<=n;i++)
{
c[i]=;
h[i]=-;
L[i]=;
R[i]=;
} for(int i=;i<=n-;i++)
{
int a,b; scanf("%d%d",&a,&b);
add(a,b); add(b,a);
} tt=; dfs(,-); for(int j=;j<=;j++)
{
for(int i=;i<=n;i++)
{
if(dp[i][j-]==-) dp[i][j]=-;
else dp[i][j] = dp[dp[i][j-]][j-];
}
} for(int i=;i<=m;i++)
{
scanf("%d",&k1); for(int j=;j<=k1;j++) scanf("%d",&A[j]), update(L[A[j]],);
scanf("%d",&k2); ans=; for(int j=;j<=k2;j++)
{
scanf("%d",&B[j]);
if(get(R[B[j]])-get(L[B[j]]-))
{
ans = max(ans,dep[B[j]]);
continue;
} work(B[j]);
} printf("%d\n",ans); for(int j=;j<=k1;j++) update(L[A[j]],-);
}
}
return ;
}

HDU 6031 Innumerable Ancestors的更多相关文章

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

    去博客园看该题解 题目 查看原题 - HDU6031 Innumerable Ancestors 题目描述 有一棵有n个节点的有根树,根节点为1,其深度为1,现在有m个询问,每次询问给出两个集合A和B ...

  2. 【HDU 6031]】 Innumerable Ancestors

    题意 有一棵有n个结点的树,这里有m个询问,每个询问给出两个非空的结点集合A和B,有些结点可能同时在这两个集合当中.你需要从A和B中分别选择一个节点x和y(可以是同一个结点)你的目标是使LCA(x,y ...

  3. HDU-6031 Innumerable Ancestors(二分+树上倍增)

    题意 给一棵树,$m$次询问,每次询问给两个点集问从两个点集中各取一个点的$LCA$的最大深度. 思路 二分答案.对于某个二分过程中得到的$Mid$,如果可行则两个点集在$Mid$所在的深度存在公共的 ...

  4. HDU6031:Innumerable Ancestors(二分+倍增数组)

    传送门 题意 n个点的图,有n-1条无向边,m个询问,每次询问 给出两个集合a和b,找到a的一个元素x,b的一个元素y,使得x和y的lca深度最大 分析 这道题如果直接暴力做,复杂度为O(mk1k2* ...

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

    题目链接:id=1330">传送门 在线算法: #include <iostream> #include <cstdio> #include <cstri ...

  6. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  7. hdu 4553 约会安排

    约会安排 http://acm.hdu.edu.cn/showproblem.php?pid=4553 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

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

    HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  9. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

随机推荐

  1. linux shell学习一

    本博客参考自: http://www.cnblogs.com/waitig/p/5523409.html <shell从入门到精通>  张春晓编著 Shell简介 Shell自身是一个用C ...

  2. CSS选择器-常用搜集

    标签选择器: div{ font-size=10px; color=red; background-color=yello; width=200px; height=200px; } <div& ...

  3. CronTrigger 表达式 (调度配置)

    CronTrigger 的用途更广,相比基于特定时间间隔进行调度安排的 SimpleTrigger,CronTrigger 主要适用于基于日历的调度安排.例如:每星期二的 16:38:10 执行,每月 ...

  4. HTML入门(三)后台系统显示页面_框架标签

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. Python学习笔记(四十一)— 内置模块(10)urllib

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432688314 ...

  6. Counting Pair

    Counting Pair Time Limit: 1000 ms Memory Limit: 65535 kB Solved: 112 Tried: 1209 Submit Status Best ...

  7. python 文字转语音包pyttsx安装出错解决方法

    pyttsx的python的文字转语音的包,但是pyttsx的官方网站上资源只更新2012年,所以在py3中使用pip install pyttsx或者下载安装包进行安装时,虽然可以安装成功,但是im ...

  8. jQuery中一些不常用的方法属性【转载】

    index(subject) 搜索与参数表示的对象匹配的元素,并返回相应元素的索引值.如果找到了匹配的元素,从0开始返回:如果没有找到匹配的元素,返回-1. data() data(elem):为页面 ...

  9. 【BZOJ】4596: [Shoi2016]黑暗前的幻想乡

    [题意]给定n个点的无向完全图,有n-1个公司各自分管一部分路,要求所有公司都有修路的生成树数.n<=17. [算法]容斥原理+生成树计数(矩阵树定理) [题解]每个生成树方案是一个公司有无修路 ...

  10. ubuntu16.04中启动anaconda图形化界面

    $ source ~/anaconda3/bin/activate root $ anaconda-navigator