该算法的详细解释请戳:

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. poj3693(后缀数组)

    http://poj.org/problem?id=3693 题意:给出一串字符,需要求这串字符中的最长重复子串,要是有多个,输出字典序最小的......... 我自己的一些想法:这个思路我一开始倒是 ...

  2. 利用Python操作Word文档【图片】

    利用Python操作Word文档

  3. input 和<fmt:formatDate>的结合使用

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <inpu ...

  4. 数据库事务隔离级别<转>

    数据库事务的隔离级别有4个,由低到高依次为Read uncommitted.Read committed.Repeatable read.Serializable,这四个级别可以逐个解决脏读.不可重复 ...

  5. C#调用Oracle存储过程

    C#调用Oracle存储过程的代码如下所示: using System; using System.Collections.Generic; using System.Collections.Obje ...

  6. css实现圆角三角形例子(无图片)

    css实现圆角三角形例子(无图片) 以前我们做圆角时都会要用到图片来实现,今天我给大家整理的这款css圆角效果是不需要图片的纯css实现的哦,下面我们一起来看看吧. 效果如下图所示 三角形所对方向&q ...

  7. 115个Java面试题和答案(下)

    转自:http://www.importnew.com/11028.html   第一篇讨论了面向对象编程和它的特点,关于Java和它的功能的常见问题,Java的集合类,垃圾收集器,本章主要讨论异常处 ...

  8. 【BZOJ】1693: [Usaco2007 Demo]Asteroids(匈牙利)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1693 裸匈牙利.. #include <cstdio> #include <cst ...

  9. thinkphp中ajaxReturn的用法

    1.例子: if ($codeid = $model->addCustomer($this->admin["id"])) { $data["code" ...

  10. swift - SQLite数据库的使用(引用)

    SQLite轻量级数据库在移动应用中使用非常普遍,但是目前的库是C编写的,为了方便使用,对SQLite相关的操作用Swift进行了封装.这个封装代码使用了一个开源项目SQLiteDB,地址是:http ...