hdu 6031 Innumerable Ancestors
来源:互联网 发布:sai在mac虚拟机没压感 编辑:程序博客网 时间:2024/06/06 04:38
问题简化为,给定一个节点,求另外一点与它的lca的深度最大,可知dfs序列最接近的两点的lca的深度最大,所以,可以将给定集合中的节点按他的dfs序从小到大排序,二分找到最接近的那个,因为二分找到的只是比它大的最接近的一个,可能前一个比它小的更为接近,所以做两次判断。集合中有重复元素,并不会妨碍我们判断。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <vector>#include <map>#include <cmath>#include <set>#include <queue>using namespace std;const int INF=1e9+10;const double EPS = 1e-10; typedef long long ll;const int mod=1e9+7;const int N = 100002; const int M = 25;int n,m;int dir[N];int dp[2*N][M]; //这个数组记得开到2*N,因为遍历后序列长度为2*n-1 bool vis[N]; struct edge { int u,v,w,next; }e[2*N]; int tot,head[N]; inline void add(int u ,int v ,int w ,int &k) { e[k].u = u; e[k].v = v; e[k].w = w; e[k].next = head[u]; head[u] = k++; u = u^v; v = u^v; u = u^v; e[k].u = u; e[k].v = v; e[k].w = w; e[k].next = head[u]; head[u] = k++; } int ver[2*N],R[2*N],first[N]; //ver:节点编号 R:深度 first:点编号位置 void dfs(int u ,int dep) { vis[u] = true; ver[++tot] = u; first[u] = tot; R[tot] = dep; for(int k=head[u]; k!=-1; k=e[k].next) if( !vis[e[k].v] ) { int v = e[k].v,w = e[k].w; dfs(v,dep+1); ver[++tot] = u; R[tot] = dep; } }//返回的不是最小值,而是最小值时对应的下标void ST(int n) { for(int i=1;i<=n;i++) dp[i][0] = i; for(int j=1;(1<<j)<=n;j++) { for(int i=1;i+(1<<j)-1<=n;i++) { int a = dp[i][j-1] , b = dp[i+(1<<(j-1))][j-1]; dp[i][j] = R[a]<R[b]?a:b; } } }//中间部分是交叉的。 int RMQ(int l,int r) { int k=0; while((1<<(k+1))<=r-l+1) k++; int a = dp[l][k], b = dp[r-(1<<k)+1][k]; //保存的是编号 return R[a]<R[b]?a:b; } int LCA(int u ,int v) { int x = first[u] , y = first[v]; if(x > y) swap(x,y); int res = RMQ(x,y); return ver[res]; } void init1(){ memset(head,-1,sizeof(head)); memset(vis,false,sizeof(vis));}void init2(){tot=0;dfs(1,1);ST(2*N-1);}vector<pair<int,int> >S;int main(){//freopen("out.txt","w",stdout);while(scanf("%d %d",&n,&m)!=EOF){int u,v,k=0;init1();for(int i=0;i<n-1;i++){scanf("%d %d",&u,&v);add(u,v,1,k);}init2();while(m--){int ans=0;S.clear();int a1,b1,x;scanf("%d",&a1);for(int i=0;i<a1;i++){scanf("%d",&x);S.push_back(make_pair(first[x],x));}sort(S.begin(),S.end());scanf("%d",&b1);for(int i=0;i<b1;i++){scanf("%d",&x);int po=lower_bound(S.begin(),S.end(),make_pair(first[x],0))-S.begin();if(po!=S.size()){ans=max(ans,R[first[LCA(x,S[po].second)]]);}po--;if(po>=0){ans=max(ans,R[first[LCA(x,S[po].second)]]);}}printf("%d\n",ans );}} return 0;}
0 0
- hdu 6031 Innumerable Ancestors
- HDU 6031 Innumerable Ancestors[树链剖分]
- HDU 6031 Innumerable Ancestors【LCA】
- HDU 6031 Innumerable Ancestors (LCA)
- HDU 6031 Innumerable Ancestors(LCA,树链剖分)
- hdu 6031 Innumerable Ancestors(LCA+二分)
- hdu 6031 Innumerable Ancestors lca + 二分
- 2017CCPC女生赛 hdu 6031 Innumerable Ancestors
- hdu6031 Innumerable Ancestors
- HDU6031 Innumerable Ancestors 倍增
- 【HDU6031】Innumerable Ancestors(二分+LCA)
- .ancestors *效果
- HDU 1330 Nearest Common Ancestors(求两个点的最近公共祖先)
- HDU6031[Innumerable Ancestor]--二分+倍增思想
- Closest Common Ancestors
- POJ1330 Nearest Common Ancestors
- POJ1330(Nearest Common Ancestors)
- poj_1330Nearest Common Ancestors
- Highcharts应用--动态玫瑰图
- 为什么我们喜欢用 async function
- Android ListView工作原理完全解析,带你从源码的角度彻底理解
- 【opencv】python3 将图片生成视频文件
- Android线程池
- hdu 6031 Innumerable Ancestors
- Android数据库
- 对ASP.NET网站高性能和多并发的设计的讨论
- gradle使用遇到的问题,及tinker初用
- 文件指针与文件位置指针,文件位置指针相关的库函数
- MediaCodec_loop 崩溃
- NSSearchPathForDirectoriesInDomains详细用法
- [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
- 在oracle中给一个表创建序列的语句