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. ...
随机推荐
- Confluence 6 导入一个 Confluence 站点
有下面 2 种类方法可以导入一个站点 - 通过上传一个文件或者从你 Confluence 服务器上读取一个目录.上传文件仅仅是针对一个小站点的情况.为了取得最好的导入结果,我们推荐你从服务器上的目录上 ...
- android studio 包名冲突解决
Error: Execution failed for task ': app: packageAllDebugClassesForMultiDex'. > Java.util.zip.ZipE ...
- php url函数
1.base64_encode 与 base64_decode base64_encode(string) 表示使用 MIME base64 对数据进行编码 base64_decode(string) ...
- Java 输入一组数字,用穷举的方法列出
import java.util.Scanner; public class TestScanner { public static void main(String[] args) { Scanne ...
- eclipse创建动态maven项目
需求表均同springmvc案例 此处只是使用maven 注意,以下所有需要建立在你的eclipse等已经集成配置好了maven了,说白了就是新建项目的时候已经可以找到maven了 没有的话需要安装m ...
- Loadrunner11.0 录制手机App脚本的方法一
使用Loadrunner录制手机终端App脚本 1. 说明 目前手机APP上的功能日益丰富,对手机应用功能的性能测试需求也越来越多.公司比较抠门没有花钱买Loadrunner,可怜我们工作中一直用的破 ...
- python文件操作r+,w+,a+,rb+,
w:以写方式打开, a:以追加模式打开 (从 EOF 开始, 必要时创建新文件) r+:以读写模式打开 w+:以读写模式打开 (参见 w ) a+:以读写模式打开 (参见 a ) rb:以二进制读模式 ...
- HDU 1277全文检索(字典树)
全文检索 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- github.com加速节点
github.com加速节点 192.30.253.118 github.com192.30.253.119 github.com93.46.8.89 github.com
- C# 属性(Property)和字段(Field)的区别
导读: 近期学习过程中发现了一些问题,我的学习只是学习,敲代码就是敲代码,没有加入思考,也不问为什么就直接去敲人家写好的例子去敲,把知识都学死了,逐渐散失了思考能力,所以学习的兴趣大打折扣,正如那句话 ...