POJ 1470 Cloest Common Ancestor(用Tarjan查询LCA)
来源:互联网 发布:网络主播名字大全女 编辑:程序博客网 时间:2024/06/09 16:21
题目链接;
POJ 1470 Cloest Common Ancestor
题意:
给一个
数据范围:
分析:
。
算法从根节点
root 开始搜索,每次递归搜索所有的子树,然后处理跟当前根节点相关的所有查询。
用集合表示一类节点,这些节点跟集合外的点的LCA 都一样,并把这个LCA 设为这个集合的祖先。当搜索到节点x 时,创建一个由x 本身组成的集合,这个集合的祖先为x 自己。然后递归搜索x 的所有儿子节点。当一个子节点搜索完毕时,把子节点的集合与x 节点的集合合并,并把合并后的集合的祖先设为x 。因为这棵子树内的查询已经处理完,x 的其他子树节点跟这棵子树节点的LCA 都是一样的,都为当前根节点x 。所有子树处理完毕之后,处理当前根节点x 相关的查询。遍历x 的所有查询,如果查询的另一个节点v 已经访问过了,那么x 和v 的LCA即为v 所在集合的祖先。
其中关于集合的操作都是使用并查集高效完成。
算法的复杂度为,O(n) 搜索所有节点,搜索每个节点时会遍历这个节点相关的所有查询。如果总的查询个数为Q ,则总的复杂度为O(n+Q) 。
#include <stdio.h>#include <algorithm>#include <string.h>#include <math.h>#include <vector>using namespace std;typedef long long ll;const int MAX_N = 1010;const int MAX_M = 500010;int n, m;int in[MAX_N], fa[MAX_N], cnt[MAX_N], vis[MAX_N], ancestor[MAX_N];// fa[]:并查集操作,cnt[i]:i作为LCA的次数,vis[]:标记是否访问,ancestor[]:存集合的LCAvector<int> vec[MAX_N], query[MAX_N]; // vec[i]:存i指向节点(儿子)信息,query[i]:存需要查i和谁的LCAint find(int x){ return fa[x] == x ? x : fa[x] = find(fa[x]);}void Union(int x, int y){ int fx = find(x), fy = find(y); if (fx != fy) { fa[fy] = fx; }}void Tarjan(int x){ for (int i = 0; i < vec[x].size(); ++i) { Tarjan(vec[x][i]); Union(x, vec[x][i]); // 注意传参和并查集合并顺序 } vis[x] = 1; for (int i = 0; i < query[x].size(); ++i) { if (vis[query[x][i]]) { // x 和 query[x][i]的LCA是find(query[x][i]) cnt[find(query[x][i])]++; } }}void init(){ for (int i = 0; i <= n; ++i) { fa[i] = ancetor[i] = i; vec[i].clear(); query[i].clear(); } memset(vis, 0, sizeof(vis)); memset(in, 0, sizeof(in)); memset(cnt, 0, sizeof(cnt));}int main(){ while (~scanf("%d", &n)) { init(); for (int i = 1; i <= n; ++i) { int u, num, v; scanf("%d:(%d)", &u, &num); for (int j = 0; j < num; ++j) { scanf("%d", &v); vec[u].push_back(v); in[v]++; } } scanf("%d", &m); while (m --) { while(getchar() != '(') ; int u, v; scanf("%d%d", &u, &v); query[u].push_back(v); query[v].push_back(u); } while (getchar() != ')') ; // 找到根节点 int root; for (int i = 1; i <= n; ++i) { if (in[i] == 0) { root = i; break; } } Tarjan(root); for (int i = 1; i <= n; ++i) { if (cnt[i]) printf("%d:%d\n", i, cnt[i]); } } return 0;}
0 0
- POJ 1470 Cloest Common Ancestor(用Tarjan查询LCA)
- AIZU Lowest Common Ancestor Tarjan求LCA
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
- poj 1470 Closest Common Ancestors(tarjan离线求lca)
- poj 1470 Closest Common Ancestors 【Tarjan 离线 LCA】
- poj 1470 Closest Common Ancestors (离线LCA Tarjan)
- POJ 1470 Closest Common Ancestors(离线tarjan-LCA)
- POJ 1470Closest Common Ancestors tarjan算法求LCA
- POJ 1470 Closest Common Ancestors LCA Tarjan算法
- POJ 1330 Nearest Common Ancestors 【用Tarjan求LCA】
- poj 1470--tarjan--LCA
- poj Nearest Common Ancestors(LCA+Tarjan)
- 【LCA|Tarjan】POJ-1330 Nearest Common Ancestors
- POJ 1330 Nearest Common Ancestors(LCA Tarjan)
- poj--1330 Nearest Common Ancestors(LCA/Tarjan)
- Lowest Common Ancestor(LCA)
- LCA(Least Common Ancestor)
- SPOJ LCA Lowest Common Ancestor
- Android WebView加载html5 3D全景
- (转)std::map的用法总结
- 华为OJ——图片整理
- Android HttpClient实例详解--用代码说话
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (tire 树)
- POJ 1470 Cloest Common Ancestor(用Tarjan查询LCA)
- 不同类型在不同位数机器的平台下长度
- 修改 tomcat 的内存
- Socket心跳包机制
- bzoj1047(单调队列,矩阵中)
- poj 3281 Dining(网络流)
- 百度面试(数据库隔离级别、overload、override)
- 多线程的编程题目(一)
- listvie y轴 监听滑动距离