本题也是找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题解的更多相关文章

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

  2. POJ 1470 Closest Common Ancestors(LCA&RMQ)

    题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...

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

    其实这是一个裸求LCA的题目,我使用的是离线的Tarjan算法,但是这个题的AC对于我来说却很坎坷……首先是RE,我立马想到数组开小了,然后扩大了数组,MLE了……接着把数组调整适当大小,又交了一发, ...

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

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

  5. POJ 1470 Closest Common Ancestors 【LCA】

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

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

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

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

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

  8. POJ 1470 Closest Common Ancestors

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

  9. poj——1470 Closest Common Ancestors

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

随机推荐

  1. reverse和reverse_copy函数的应用

    reverse函数的作用是:反转一个容器内元素的顺序.函数参数:reverse(first,last);//first为容器的首迭代器,last为容器的末迭代器.它没有任何返回值. 这个函数比较简单, ...

  2. ASCII-->Ansi-->Unicode-->UTF8 关于编码 自己的总结

    各种不同的编码 无非就是效率 最大化. 我猜测编码的进化流程: ASCII(American Standard Code for Information Interchange)----满足了美国和西 ...

  3. 洛谷P1554 梦中的统计 题解

    题目传送门 这道题暴力又让我过了...数据真的很水(luogu) 暴力枚举n~m的每个数,再统计一次,交付评测...AC #include<bits/stdc++.h> using nam ...

  4. 洛谷 P1957 口算练习题 题解

    题目传送门 这道题是考字符串处理,另外输入要使用c++的cin的神奇功能. #include<bits/stdc++.h> using namespace std; int n;char ...

  5. go-互斥锁及原子函数

    用于解决并发函数的竞争状态问题... package main import ( "fmt" "runtime" "sync" " ...

  6. Ubuntu16.4 zookeeper-3.4.10 单机多实例部署

    上传 zookeeper-3.4.10.tar.gz 到服务器 root@temple-:/usr/local/temple/jar# ll total drwxr-xr-x root root 8月 ...

  7. Codeforces Round #490 (Div. 3) F - Cards and Joy

    F - Cards and Joy 思路:比较容易想到dp,直接dp感觉有点难,我们发现对于每一种数字要处理的情况都相同就是有 i 张牌 要给 j 个人分, 那么我们定义dp[ i ][ j ]表示 ...

  8. webpack3.0+总结

    ul>li{ color:blue; font-size:20px } .items>ul>li>ul>li{ color:crimson } --> webpac ...

  9. phantomjs2.1 初体验

    上次看了一下scrapy1.1的新手指南 决定写个小爬虫实验一下 目标网站是http://www.dm5.com/manhua-huofengliaoyuan准备爬取漫画火凤燎原的已有章节,将图片保存 ...

  10. Disruptor Java版和.NET版的区别

    The main differences comes from the fact that .NET supports structs (value types), Java doesn't. In ...