POJ 1470 Closest Common Ancestors LCA题解
本题也是找LCA的题目,只是要求多次查询。一般的暴力查询就必定超时了,故此必须使用更高级的方法,这里使用Tarjan算法。
本题处理Tarjan算法,似乎输入处理也挺麻烦的。
注意: 由于查询的数据会极大,故此使用一个数组记录全部查询数据就会超时的。
我就载在这里了。查了好久才想到这点。
由于我使用了一个vector容器记录了查询数据。故此每次都循环这组这么大的数据,就超时了。
----解决的方法:使用一个vector<int> quest来记录查询数组。这样每次都仅仅须要循环某节点的邻接查询点就能够了。数据量是非常小的。
有些说法没道理的:比方:结尾是否有空格?没有!
我使用了按权值查询并查集的优化,实验证明:没有优化效果。
使用map容器记录结果,好像没有加速,只是这种代码更加成熟。
其它就是Tarjan算法了,网上也不少讲解的了,结合代码学习,这个算法也不难。
#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std; struct Node
{
bool notRoot;
bool vis;
vector<int> child;
}; const int MAX_N = 901;
int N, u, v, n;
Node Tree[MAX_N];
//vector<int> quest;//这样记录就会超时,应该是由于须要查询的数据极其大,所以引发超时
vector<int> quest[MAX_N];
map<int, int> ans;
int par[MAX_N];
int rank[MAX_N];
int ancestor[MAX_N]; void init(int n)
{
for (int i = 1; i <= n; i++)
{
Tree[i].child.clear();
Tree[i].notRoot = false;
Tree[i].vis = false;
quest[i].clear();
}
} int find(int x)
{
if (!par[x]) return x;
return par[x] = find(par[x]);
} void unionTwo(int x, int y)
{
x = find(x);
y = find(y);
if (x == y) return;
if (rank[x] < rank[y]) par[x] = y;
else
{
par[y] = x;
rank[x]++;
}
} void LCATarjan(int r)
{
//ancestor[r] = r;
for (int i = 0; i < (int)Tree[r].child.size(); i++)
{
int v = Tree[r].child[i];
//if (Tree[v].vis) continue;
LCATarjan(v);
unionTwo(r, v);
ancestor[find(r)] = r;
}
Tree[r].vis = true;
for (int i = 0; i < (int)quest[r].size(); i++)
{
int v = quest[r][i];
if (Tree[v].vis) ans[ancestor[find(v)]]++;
}
} int main()
{
while (scanf("%d", &N) != EOF)
{
init(N);
memset(par, 0, sizeof(int) * (N+1));
memset(ancestor, 0, sizeof(int) * (N+1));
memset(rank, 0, sizeof(int) * (N+1)); for (int i = 0; i < N; i++)
{
scanf("%d", &u);
while (getchar() != '(') ;
scanf("%d", &n);
while (getchar() != ')') ;
for (int j = 0; j < n; j++)
{
scanf("%d", &v);
Tree[u].child.push_back(v);
Tree[v].notRoot = true;
}
} scanf("%d", &n);
for (int i = 0; i < n; i++)
{
char a = getchar();
while (a != '(') a = getchar();
u = 0;
a = getchar();
while (a != ' ')
{
u = (u <<3) + (u<<1) + (a - '0');
a = getchar();
}
v = 0;
a = getchar();
while (a != ')')
{
v = (v<<3) + (v<<1) + (a - '0');
a = getchar();
}
quest[u].push_back(v);
quest[v].push_back(u);
} int root = 0;
for (int i = 1; i <= N; i++)
{
if (!Tree[i].notRoot)
{
root = i;
break;
}
}
ans.clear();
LCATarjan(root); map<int, int>::iterator it;
for (it = ans.begin(); it != ans.end(); it++)
{
printf("%d:%d\n", it->first, it->second);
}
}
return 0;
}
POJ 1470 Closest Common Ancestors LCA题解的更多相关文章
- poj 1470 Closest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1470 Write a program that takes as input a rooted tree and a list of ...
- POJ 1470 Closest Common Ancestors(LCA&RMQ)
题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...
- POJ 1470 Closest Common Ancestors(LCA 最近公共祖先)
其实这是一个裸求LCA的题目,我使用的是离线的Tarjan算法,但是这个题的AC对于我来说却很坎坷……首先是RE,我立马想到数组开小了,然后扩大了数组,MLE了……接着把数组调整适当大小,又交了一发, ...
- 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】
任意门: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 ...
随机推荐
- cocos2d-x 开发中使用的一些工具
这些工具平常也用到,不过没有像这样整理出来,这是我在网上看到的.就记录一下. 位图字体工具Bitmap Font Tools BMFont (Windows)FonteditorGlyph Desig ...
- Check whether a + b = c or not after removing all zeroes from a,b and c
Check whether a + b = c or not after removing all zeroes from a,b and c Given two integers a and b, ...
- list容器详解
首先说说STL ( STL的目的是标准化组件,这样就不用重新开发,可以使用现成的组件.STL现在是C++的一部分,因此不用额外安装什么.它被内建在你的编译器之内.因为STL的list是一个简单的容器, ...
- https请求过程
我们都知道HTTPS能够加密信息,以免敏感信息被第三方获取.所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议. HTTPS简介 HTTPS其实是有两部分组成:HTTP + SSL ...
- 使用JDBC连接数据库的一些BUG
题记:前几天用JDBC连接MYSQL数据库的时候,出现了一些BUG,有代码层次的,也有设置层次的, 下面的解决方法时我目前所遇到的,后期如果还有遇到的会进行补充. 一.出现:远程mysql_java. ...
- php基础知识一
1.PHP是什么: 开源,免费的,跨平台的 2.PHP能做什么: 3.PHP的特点: 4.PHP的标记风格: <?php ?> <? ?> <script languag ...
- Hibernate 基于外键的双向一对一关联映射
之前简单介绍了基于外键的单项一对一的关联映射关系,本文简单介绍基于外键的双向一对一的关联映射. 1.设计表结构 表结构对于双向一对一来说没有多少改变,只是双向都可以获取到对方. 2.创建Person对 ...
- HDU 6085 Rikka with Candies(bitset)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6085 [题目大意] 给出一个数组a一个数组b,以及询问数组c, 问对于每个c有多少对a%b=c,答 ...
- 【筛法求素数】【推导】【组合数】UVALive - 7642 - Prime Distance
题意:n个格子,m个球,让你把球放入某些格子里,使得所有有球的格子之间的距离(abs(i-j))均为素数 ,让你输出方案数. 只占一个格子或者两个格子显然可行. 占有三个格子的情况下,则必须保证其中两 ...
- Android背后的设计思想——功能共享机制
Android的系统设计,与别的智能手机操作系统有很大区别,甚至在以往的任何操作系统里,很难找到像Android这样进行全面地系统级创新的操作系统.从创新层面上来说,Android编程上的思想和支持这 ...