poj 1470 lca-targan离线算法
来源:互联网 发布:小熊相机软件 编辑:程序博客网 时间:2024/05/17 15:39
题意:
给一棵树n个节点,然后一些k个询问u,v。
最后输出的是每个询问中被询问的u,v的最近公共祖先和被访问的次数。
解析:
用targan离线处理,每次遇到访问则cnt++。
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <set>#include <climits>#include <cassert>#define Min(a,b) ((a)<(b)?(a):(b))#define Max(a,b) ((a)>(b)?(a):(b))#define LL long long#define lson lo, mi, rt << 1#define rson mi + 1, hi, rt << 1 | 1using namespace std;const int maxn = 1000 + 10;const int maxq = 500000 + 10;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = acos(-1.0);const double ee = exp(1.0);int cnt[maxn];int n;vector<int> node[maxn], query[maxq];int inDegree[maxn];int ancestor[maxn];bool vis[maxn];int fa[maxn];int Rank[maxn];void Init(int n){ memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= n; i++) { fa[i] = i; Rank[i] = 0; node[i].clear(); query[i].clear(); } memset(vis, false, sizeof(vis)); memset(inDegree, 0, sizeof(inDegree)); memset(ancestor, 0, sizeof(ancestor));}int Find(int x){ if (fa[x] != x) fa[x] = Find(fa[x]); return fa[x];}void Union(int u, int v){ int fau = Find(u); int fav = Find(v); if (fau == fav) return; if (Rank[fau] < Rank[fav]) { fa[fau] = fav; } else { fa[fav] = fau; if (Rank[fau] == Rank[fav]) Rank[fau]++; }}void lca(int rt){ int sz = node[rt].size(); //自成集合 ancestor[rt] = rt; for (int i = 0; i < sz; i++) { lca(node[rt][i]); //递归子树 Union(rt, node[rt][i]); //合并子树与根 int id = Find(node[rt][i]); ancestor[id] = rt; //子树祖先也指向根 } vis[rt] = true; sz = query[rt].size(); for (int i = 0; i < sz; i++) { if (vis[query[rt][i]]) { int id = Find(query[rt][i]); cnt[ancestor[id]]++;// printf("%d\n", ancestor[id]); } } return;}int main(){#ifdef LOCAL freopen("in.txt", "r", stdin);#endif // LOCAL int n, q; while (~scanf("%d", &n)) { Init(n); for (int i = 1; i <= n; i++) { int u, k; scanf("%d:(%d)", &u, &k); while (k--) { int v; scanf("%d", &v); inDegree[v]++; node[u].push_back(v);//// node[v].push_back(u); } } scanf("%d", &q); for (int i = 0; i < q; i++) { int u, v; char c; cin >> c; scanf("%d %d)", &u, &v); query[u].push_back(v); query[v].push_back(u); } int rt; for (int i = 1; i <= n; i++) { if (!inDegree[i]) { rt = i; break; } } lca(rt); for (int i = 1; i <= n; i++) { if (cnt[i]) { printf("%d:%d\n", i, cnt[i]); } } } return 0;}
0 0
- poj 1470 lca-targan离线算法
- poj 1330 lca-targan离线算法
- HDU2874-LCA-离线targan
- poj 1470 LCA 离线算法
- POJ 1470 LCA tarjan 离线算法
- POJ 1470 Closest Common Ancestors LCA离线Tarjin算法
- LCA问题的Tarjan离线算法 + POJ 1470
- POJ 1470 Closet Conmon Ancestors(LCA离线算法)
- poj 1330lca模板题离线算法
- poj 2186 Popular Cows(targan算法)
- POJ - 1470 Closest Common Ancestors(LCA离线)
- [POJ 1470]Closest Common Ancestors[离线LCA]
- poj 1470 Closest Common Ancestors LCA离线
- POJ 1470 Closest Common Ancestors 离线LCA
- (算法)Tarjan离线算法解决LCA问题 (附POJ 1470 Closest Common Ancestors 代码)
- (算法)Tarjan离线算法解决LCA问题 (附POJ 1470 Closest Common Ancestors 代码)
- POJ 3728 离线 LCA
- targan算法
- 关于CMUSphinx的一些整理
- const和mutable
- 对商业方,架构师要避免愤世嫉俗
- Zend Guard6.0使用教程——PHP代码加密
- 大整数乘法问题
- poj 1470 lca-targan离线算法
- hdu5365 Run
- 如何将JAVA工程转为MAVEN工程
- 使用butterknife中遇到的几个问题及解决方法
- hiho一下 第五十八周 Beautiful String dp
- javascript以及衍生框架自学笔记
- 教你自定义竖直跑马灯效果(广告专用)
- Window与Linux跨平台JAVA编程的注意事项
- Mysql之存储过程回顾