该算法的详细解释请戳:

http://www.cnblogs.com/Findxiaoxun/p/3428516.html

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
;
int father[MAXN],ancestor[MAXN];
bool visit[MAXN];
int ans[MAXN];
vector<int> map[MAXN];//save the tree
vector<int> query[MAXN];//save the query
int n,t,root;
bool indegree[MAXN];//the indegree to find the root
int getfather(int v){//path compression
    if(father[v]==v)return v;
    return father[v]=getfather(father[v]);
}
void aunion(int u,int v){//??
    int fv=getfather(v),fu=getfather(u);
    father[fv]=fu;
}
void LCA(int id){
    int len=map[id].size();
    ;i<len;i++){
        int son=map[id][i];
        LCA(son);
        aunion(id,son);
    }
    visit[id]=;
    len=query[id].size();
    ;i<len;i++){
        int son=query[id][i];
        if(visit[son])
            ans[father[getfather(son)]]++;
    }

}
void init(){
    int x,y,z;
    ;i<=n;i++){
        map[i].clear();
        query[i].clear();
    }
    memset(visit,,sizeof(visit));
    memset(ans,,sizeof(ans));
    memset(indegree,,sizeof(indegree));
    ;i<=n;i++)father[i]=i;
    ;i<n;i++){
        scanf("%d:(%d)",&x,&y);
        ;j<y;j++){
            scanf("%d",&z);
            indegree[z]=;
            map[x].push_back(z);
        }
    }
    scanf("%d",&t);
    while(t--){//this method of the init is really clever
        while(getchar()!='(');
        scanf("%d%d",&x,&y);
        query[x].push_back(y);
        query[y].push_back(x);
    }
    while(getchar()!=')');
    ;i<=n;i++)if(!indegree[i])root=i;//find the root;warning:the 0
}
void output(){
    ;i<=n;i++){
        )
            printf("%d:%d\n",i,ans[i]);
    }

}
int main(){
    while(scanf("%d",&n)!=EOF){
        init();
        LCA(root);
        output();
    }
    ;
}

POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan的更多相关文章

  1. POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)

    Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...

  2. POJ1330Nearest Common Ancestors最近公共祖先LCA问题

    用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改 ...

  3. POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

    LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...

  4. POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)

    A - Nearest Common Ancestors Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld &am ...

  5. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18136   Accept ...

  6. POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)

    http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...

  7. POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题

    A rooted tree is a well-known data structure in computer science and engineering. An example is show ...

  8. 求最近公共祖先(LCA)的各种算法

    水一发题解. 我只是想存一下树剖LCA的代码...... 以洛谷上的这个模板为例:P3379 [模板]最近公共祖先(LCA) 1.朴素LCA 就像做模拟题一样,先dfs找到基本信息:每个节点的父亲.深 ...

  9. HihoCoder 1067 最近公共祖先(ST离线算法)

    最近公共祖先·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个 ...

随机推荐

  1. httpclient 4种关闭连接

    因此这样的配置就会导致每个链接至少要过180S才会被释放,这样在大量请求访问时就必然会造成链接被占满,请求等待的情况. 在通过DEBUH后发现HttpClient在method.releaseConn ...

  2. FreeRTOS 任务优先级分配方案

    任务优先级说明下面对 FreeRTOS 优先级相关的几个重要知识点进行下说明,这些知识点在以后的使用中务必要掌握牢固.  FreeRTOS 中任务的最高优先级是通过 FreeRTOSConfig.h ...

  3. 存储管理器实验——SDRAM

    序言:2440有nand和nor两种启动方式,在裸机部分,都是使用的nand启动. 现在,我们想在nand flash启动的时候,通过SRAM访问存储在外设SDRAM中的程序. nand启动,先把前4 ...

  4. oracle 命令 登录

    1.打开终端,输入命令:sqlplus /nolog 2.连接数据库:conn user_name/passwd@db_name

  5. UIImageView只显示一半

    本来正常的话,UIImageView会在UIScrollView内占满的,但是第一个UIImageView只占了高度的一半左右.如下图,红色的是UIScrollView的背景色,还有那么多没有填充,但 ...

  6. Maven基础命令

    Maven 参数 -D 传入属性参数 -P 使用pom中指定的配置 -e 显示maven运行出错的信息 -o 离线执行命令,即不去远程仓库更新包 -X 显示maven允许的debug信息 -U 强制去 ...

  7. Memcache内存分配策略

    一.Memcache内存分配机制 关于这个机制网上有很多解释的,我个人的总结如下. Page为内存分配的最小单位. Memcached的内存分配以page为单位,默认情况下一个page是1M,可以通过 ...

  8. datagrid鼠标悬浮提示

    /** * 扩展两个方法 */ $.extend($.fn.datagrid.methods, { /** * 开打提示功能 * @param {} jq * @param {} params 提示消 ...

  9. leetcode 153: Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  10. Spring_day04--HibernateTemplate介绍_整合其他方式_Spring分模块开发

    HibernateTemplate介绍 1 HibernateTemplate对hibernate框架进行封装, 直接调用HibernateTemplate里面的方法实现功能 2 HibernateT ...