该算法的详细解释请戳:

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. MySQL 找回密码

    Windows: 1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysqld --skip-grant-tables回车.如果没有出现提示信息,那就对了. ...

  2. java的map取值

    第一种方法根据键值的名字取值 import java.util.HashMap; import java.util.Map; /**   * @param args   */  public stat ...

  3. C语言 · 数对

    算法训练 数对   时间限制:1.0s   内存限制:512.0MB      问题描述 编写一个程序,该程序从用户读入一个整数,然后列出所有的数对,每个数对的乘积即为该数. 输入格式:输入只有一行, ...

  4. [Linux]gcc/libc/glibc

    转自:http://blog.csdn.net/weiwangchao_/article/details/16989713 1.gcc(gnu collect compiler)是一组编译工具的总称. ...

  5. PHP——小尾巴之权限管理

    流程: 在权限管理页面,默认显示用户的角色,更改复选框的按钮内容,可以改变角色,点击确定提交至数据库 在登陆之后,只显示该用户的角色所对应的权限 数据库: guanli.php <!DOCTYP ...

  6. 大数据处理-Bloom Filter

    大数据处理--Bloom Filter 布隆过滤器(Bloom Filter)是由巴顿.布隆于一九七零年提出的.它实际上是一个很长的二进制向量和一系列随机映射函数. 如果想判断一个元素是不是在一个集合 ...

  7. libcurl库的编译

    终于弄懂了libcurl库的编译,记下来免得忘记. 下载地址:   libcurl库:http://curl.haxx.se/latest.cgi?curl=zip   openssl安装包:http ...

  8. C++ 函数的扩展④--函数重载与函数指针

    //函数扩展--函数重载与函数指针 #include<iostream> using namespace std; //函数参数类型不同 void Fuc(char * b){ print ...

  9. 纪念,BZOJ AC 100题!

    虽然说有将近50+是usaco,然后还有很多水题T_T 看来我还是刷水题.... 看来我还是那么弱. T_T 但是好歹也要留个纪念..

  10. hdu 4709:Herding(叉积求三角形面积+枚举)

    Herding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...