<题目链接>

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5.  
  6. using namespace std;
  7. ;
  8.  
  9. vector<int>edge[N];
  10. int query[N][N],father[N],count[N],indeg[N];
  11. bool vis[N];
  12. int n,m;
  13. void init(){
  14. ;i<=n;i++)edge[i].clear();
  15. memset(query,,sizeof(query));
  16. memset(vis,false,sizeof(vis));
  17. memset(count,,sizeof(count));
  18. memset(indeg,,sizeof(indeg));
  19. }
  20. int find(int x){ //找到根节点
  21. if(x!=father[x])
  22. father[x]=find(father[x]);
  23. return father[x];
  24. }
  25. void Tarjan(int u){
  26. father[u]=u;
  27. ;i<edge[u].size();i++){ //得到该树上所有节点的父子关系
  28. int v=edge[u][i];
  29. Tarjan(v);
  30. father[v]=u;
  31. }
  32. vis[u]=true;
  33. ;i<=n;i++)
  34. if(vis[i] && query[u][i])
  35. count[find(i)]+=query[u][i]; //最近公共祖先出现次数+1
  36. }
  37. int main(){
  38. while(~scanf("%d",&n)){
  39. init();
  40. int u,v;
  41. ;i<n;i++){
  42. scanf("%d:(%d)",&u,&m);
  43. while(m--){
  44. scanf(" %d",&v);
  45. edge[u].push_back(v); //建立有向边
  46. indeg[v]++; //统计入度,用于寻找根节点
  47. }
  48. }
  49. scanf(" %d",&m);
  50. ;i<m;i++){
  51. scanf(" (%d %d)",&u,&v);
  52. query[u][v]++; //将代查询的节点也全部记录下来
  53. query[v][u]++;
  54. }
  55. ;i<=n;i++)
  56. ){
  57. Tarjan(i);
  58. break;
  59. }
  60. ;i<=n;i++)
  61. if(count[i])
  62. printf("%d:%d\n",i,count[i]);
  63. }
  64. ;
  65. }

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. Servet

    一.Servlet 是单例吗 不是. 1.你可以用多个 URL 映射同一个 Servlet.这样就会出现多个实例. 2.看看 Servlet 定义: 引用 For a servlet not host ...

  2. STM32应用实例十四:利用光敏二极管实现光度测量

    最近我们在开发臭氧发生器时,需要监测生成的臭氧的浓度,于是想到使用光度计来测量.因为不同浓度的臭氧对管的吸收作用是不相同的,于是检测光照强度的变化就可以得到相应的浓度数据. 1.硬件设计 此次光照度检 ...

  3. Confluence 6 从一个备份中获得文件附件

    页面中的文件附件可以从备份中获得而不需要将备份文件导入到 Confluence 中.这个在用户删掉了附件,但是你还是想恢复这个附件的时候就变得非常有用了. 自动备份和手动备份都允许你进行这个操作,但是 ...

  4. 为什么在移动端用rem圆角不圆

    rem是根据网页效果图的尺寸来计算的,当然还要借助媒体查询来完成.例如你的设计稿如果是宽720px的话,那你的文字就要以原始大小除以11.25,就是对应下面媒体查询720px:例如16px的话就要16 ...

  5. 1010:Tempter of the Bone

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Problem Description The doggie found a bone in a ...

  6. python网络爬虫笔记(一)

    一.查询数据字典型数据 1.先说说dictionary查找和插入的速度极快,不会随着key的增加减慢速度,但是占用的内存大 2.list查找和插入的时间随着元素的增加而增加,但还是占用的空间小,内存浪 ...

  7. cf219d 基础换根法

    /*树形dp换根法*/ #include<bits/stdc++.h> using namespace std; #define maxn 200005 ]; int root,n,s,t ...

  8. 数据库MySql的安装

    1.MySQL概述 MySQL最初是由“MySQL AB公司”开发的一套关系型数据库管理系统(RDBMS-Relation DataBase Management System).MySQL不仅是最流 ...

  9. Java 一个关于使用&&导致的BUG

    二维数据track的定义: byte[][] track = new byte[10][10]; 本意:判断track[trackY][trackX]的值是否为零,以及trackX是否小于10. 带B ...

  10. 金蝶k3密码批量修改

    该字段含有单引号,直接使用查询语句,需要转义其中的单引号.select * from t_User where FSID = ') F ", ,P T #8 *P!D &D 80!N ...