<题目链接>

题目大意:
给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数。

解题分析:
LCA模板题,下面用的是离线Tarjan来解决。并且为了代码的简洁,本代码用的是vector存图。

 #include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<vector>

 using namespace std;
 ;

 vector<int>edge[N];
 int query[N][N],father[N],count[N],indeg[N];
 bool vis[N];
 int n,m;
 void init(){
     ;i<=n;i++)edge[i].clear();
     memset(query,,sizeof(query));
     memset(vis,false,sizeof(vis));
     memset(count,,sizeof(count));
     memset(indeg,,sizeof(indeg));
 }
 int find(int x){    //找到根节点
     if(x!=father[x])
         father[x]=find(father[x]);
     return father[x];
 }
 void Tarjan(int u){
     father[u]=u;
     ;i<edge[u].size();i++){   //得到该树上所有节点的父子关系
         int v=edge[u][i];
         Tarjan(v);
         father[v]=u;
     }
     vis[u]=true;
     ;i<=n;i++)
         if(vis[i] && query[u][i])
             count[find(i)]+=query[u][i];   //最近公共祖先出现次数+1
 }
 int main(){
     while(~scanf("%d",&n)){
         init();
         int u,v;
         ;i<n;i++){
             scanf("%d:(%d)",&u,&m);
             while(m--){
                 scanf(" %d",&v);
                 edge[u].push_back(v);   //建立有向边
                 indeg[v]++;    //统计入度,用于寻找根节点
             }
         }
         scanf(" %d",&m);
         ;i<m;i++){
             scanf(" (%d %d)",&u,&v);
             query[u][v]++;     //将代查询的节点也全部记录下来
             query[v][u]++;
         }
         ;i<=n;i++)
             ){
                 Tarjan(i);
                 break;
             }
         ;i<=n;i++)
             if(count[i])
                 printf("%d:%d\n",i,count[i]);
     }
     ;
 }

2018-10-21

POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】的更多相关文章

  1. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

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

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

  3. POJ 1470 Closest Common Ancestors【近期公共祖先LCA】

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...

  4. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  5. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  6. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  7. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

  8. poj——1470 Closest Common Ancestors

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20804   Accept ...

  9. POJ 1470 Closest Common Ancestors【LCA Tarjan】

    题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...

随机推荐

  1. 找到 Confluence 6 的日志和配置文件

    找到 Confluence 的日志文件 这部分内容对 Confluence 的默认日志表现进行描述并且假设你没有对 Confluence 的默认日志配置进行修改.为了统一在不同平台中的日志输出,Con ...

  2. 前端html

    前端html html   是一种描述网页的语言,是超文本标记语言 :hyper Text Markup Lauguage 是一种标记语言[标记语言是一套标记标签 markup tag]使用标记标签来 ...

  3. 【ES】学习10-聚合3

    聚合是在查询匹配的文档中做统计的 不指定查询语句时,从所有文档中匹配. 下面两个语句等价: GET /cars/transactions/_search { , "aggs" : ...

  4. bitset用法详解

    参见此博客: https://www.cnblogs.com/magisk/p/8809922.html

  5. Java 获取当前系统的时间

    获取当前系统的时间,每隔一秒,打印一次. import java.util.Date; public class TestDate { public static void main(String[] ...

  6. fio 测试磁盘性能

    在磁盘测试中最关心的几个指标分别为: iops(每秒执行的IO次数).bw(带宽,每秒的吞吐量).lat(每次IO操作的延迟). 当每次IO操作的block较小时,如512bytes/4k/8k等,测 ...

  7. 论文阅读笔记十七:RefineNet: Multi-Path Refinement Networks for High-Resolution Semantic Segmentation(CVPR2017)

    论文源址:https://arxiv.org/abs/1611.06612 tensorflow代码:https://github.com/eragonruan/refinenet-image-seg ...

  8. c++实现 给定直角停车位两个点,求取剩余两点坐标。

    //2018-09-08-fourmi /*************************include head files************************************ ...

  9. pycaffe简明文档

    pycaffe简明文档 by ChrisZZ, imzhuo@foxmail.com 2018年01月18日19:00:56 说明 caffe的python接口没有官方说明文档,例如查看一个函数的用法 ...

  10. Android常用框架和控件使用

    Router框架 https://github.com/iqiyi/Andromeda/blob/master/CHINESE_README.md https://github.com/alibaba ...