倍增法模板题

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define maxn 1000
#define DEG 20
struct Edge{
int to,next;
}edge[maxn*maxn*];
int head[maxn],tot;
void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
int fa[maxn][DEG];
int deg[maxn];
void bfs(int root){
queue<int> que;
deg[root]=;
fa[root][]=root;
que.push(root);
while(!que.empty()){
int tmp=que.front();
que.pop();
for(int i=;i<DEG;i++)
fa[tmp][i]=fa[fa[tmp][i-]][i-];
for(int i=head[tmp];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==fa[tmp][]) continue;
deg[v]=deg[tmp]+;
fa[v][]=tmp;
que.push(v);
}
}
}
int lca(int u,int v){
if(deg[u]>deg[v]) swap(u,v);
int hu=deg[u],hv=deg[v],tu=u,tv=v;
for(int det=hv-hu,i=;det;det>>=,i++)
if(det&) tv=fa[tv][i];//将uv提到同一深度
if(tu==tv) return tu;
for(int i=DEG-;i>=;i--){
if(fa[tu][i]==fa[tv][i]) continue;
tu=fa[tu][i];
tv=fa[tv][i];
}
return fa[tu][];
}
int ans[maxn],flag[maxn];
void init(){
tot=;
memset(ans,,sizeof ans);
memset(head,-,sizeof head);
memset(flag,,sizeof flag);
}
int main(){
int n,u,v,m,q;
while(scanf("%d",&n)==){
init();
for(int i=;i<=n;i++){
scanf("%d:(%d)",&u,&m);
while(m--){
scanf("%d",&v);
addedge(u,v);
addedge(v,u);
flag[v]=true;
}
}
int root;
for(int i=;i<=n;i++) if(!flag[i]){root=i;break;}
bfs(root); scanf("%d",&q);
char c;
while(q--){
cin>>c;
scanf("%d %d)",&u,&v);
ans[lca(u,v)]++;
}
for(int i=;i<=n;i++)
if(ans[i]) printf("%d:%d\n",i,ans[i]);
}
return ;
}

poj1470 LCA倍增法的更多相关文章

  1. LCA(最近公共祖先)——LCA倍增法

    一.前人种树 博客:最近公共祖先 LCA 倍增法 博客:浅谈倍增法求LCA 二.沙场练兵 题目:POJ 1330 Nearest Common Ancestors 代码: const int MAXN ...

  2. POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)

    1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...

  3. hdu2586 lca倍增法

    倍增法加了边的权值,bfs的时候顺便把每个点深度求出来即可 #include<iostream> #include<cstring> #include<cstdio> ...

  4. 最近公共祖先 LCA 倍增法

    [简介] 解决LCA问题的倍增法是一种基于倍增思想的在线算法. [原理] 原理和同样是使用倍增思想的RMQ-ST 算法类似,比较简单,想清楚后很容易实现. 对于每个节点u , ancestors[u] ...

  5. luogu3379 【模板】最近公共祖先(LCA) 倍增法

    题目大意:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 整体步骤:1.使两个点深度相同:2.使两个点相同. 这两个步骤都可用倍增法进行优化.定义每个节点的Elder[i]为该节点的2^k( ...

  6. LCA—倍增法求解

    LCA(Least Common Ancestors) 即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. 常见解法一般有三种 这里讲解一种在线算法-倍增 首先我们定义fa[u][j ...

  7. LCA - 倍增法去求第几个节点

    You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...

  8. POJ 1330(LCA/倍增法模板)

    链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: #inc ...

  9. 【模板】Lca倍增法

    Codevs 1036 商务旅行 #include<cstdio> #include<cmath> #include<algorithm> using namesp ...

随机推荐

  1. Goslate: Free Google Translate API

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  2. Vagrant工具的安装

    Vagrant工具的安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 这篇博客源于我的北京一位好友:崔佳.在此,为了感激崔佳的帮助,特意写下这篇博客.希望对一些开发的小伙伴有些帮 ...

  3. python---web框架本质(1)

    总的来说php相对较为简单,但是内部封装太多,不利于对编程的更本质探索. 但是对于生产开发确实是一门不错的语言.python对于socket以及web框架的理解更加透彻 # coding:utf8 # ...

  4. Said

    呃~~生活中的每天都会经历很多事儿,而影响结果的无非就是人对事物的处理方式和对事物的处理态度~~ 在上学期间,有时考试不理想,我都会进行反思,对不该错的题巩固在三,对不会做的题查缺补漏……因为不能不思 ...

  5. 02-MySQL的安装和配置

    1. 软件和环境 注:安装MySQL数据库的操作系统必须保证有.NET环境和VC运行库的支持.    下载地址:百度云网盘链接 2. MySQL服务器安装详细步骤 (1). 选择安装类型 这里我们选择 ...

  6. 9 Web开发——springmvc自动配置原理

    官方文档目录: https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-sp ...

  7. Java——关于num++和++num

    public class num_add_add { public static void numAdd(){ int num = 10; int a = num++; System.out.prin ...

  8. js scroll函数

    $(function () { $(".sticky").hide(); var top = $(window).scrollTop(); if (top >= 100) { ...

  9. POJ3694 Network【连通分量+LCA】

    题意: 一个无向图可以有重边,下面q个操作,每次在两个点间连接一条有向边,每次连接后整个无向图还剩下多少桥(注意是要考虑之前连了的边,每次回答是在上一次的基础之上). 思路: 首先运行一次Tarjan ...

  10. 第18月第2天 ios博客

    1. github https://githuber.cn/search?language=Objective-C https://www.jianshu.com/u/815d10a4bdce htt ...