POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】
<题目链接>
题目大意:
给你一棵树,然后进行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】的更多相关文章
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)
Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...
- POJ 1470 Closest Common Ancestors【近期公共祖先LCA】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj——1470 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20804 Accept ...
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...
随机推荐
- Servet
一.Servlet 是单例吗 不是. 1.你可以用多个 URL 映射同一个 Servlet.这样就会出现多个实例. 2.看看 Servlet 定义: 引用 For a servlet not host ...
- STM32应用实例十四:利用光敏二极管实现光度测量
最近我们在开发臭氧发生器时,需要监测生成的臭氧的浓度,于是想到使用光度计来测量.因为不同浓度的臭氧对管的吸收作用是不相同的,于是检测光照强度的变化就可以得到相应的浓度数据. 1.硬件设计 此次光照度检 ...
- Confluence 6 从一个备份中获得文件附件
页面中的文件附件可以从备份中获得而不需要将备份文件导入到 Confluence 中.这个在用户删掉了附件,但是你还是想恢复这个附件的时候就变得非常有用了. 自动备份和手动备份都允许你进行这个操作,但是 ...
- 为什么在移动端用rem圆角不圆
rem是根据网页效果图的尺寸来计算的,当然还要借助媒体查询来完成.例如你的设计稿如果是宽720px的话,那你的文字就要以原始大小除以11.25,就是对应下面媒体查询720px:例如16px的话就要16 ...
- 1010:Tempter of the Bone
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Problem Description The doggie found a bone in a ...
- python网络爬虫笔记(一)
一.查询数据字典型数据 1.先说说dictionary查找和插入的速度极快,不会随着key的增加减慢速度,但是占用的内存大 2.list查找和插入的时间随着元素的增加而增加,但还是占用的空间小,内存浪 ...
- cf219d 基础换根法
/*树形dp换根法*/ #include<bits/stdc++.h> using namespace std; #define maxn 200005 ]; int root,n,s,t ...
- 数据库MySql的安装
1.MySQL概述 MySQL最初是由“MySQL AB公司”开发的一套关系型数据库管理系统(RDBMS-Relation DataBase Management System).MySQL不仅是最流 ...
- Java 一个关于使用&&导致的BUG
二维数据track的定义: byte[][] track = new byte[10][10]; 本意:判断track[trackY][trackX]的值是否为零,以及trackX是否小于10. 带B ...
- 金蝶k3密码批量修改
该字段含有单引号,直接使用查询语句,需要转义其中的单引号.select * from t_User where FSID = ') F ", ,P T #8 *P!D &D 80!N ...