poj1470 Closest Common Ancestors LCA(最近公共祖先)
2015-01-27 17:08
120 查看
题意:
给出一棵树和多组询问,然后依次输出以点i为最近公共祖先的询问有多少组。思路:
很裸得LCA离线算法。代码(4664K,1547MS):
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <vector> using namespace std; int n, q; vector<int> edges[1000]; int ans[1000]; int lca[1000][1000]; bool notRoot[1000]; bool vis[1000]; int father[1000]; int find(int p) { if (p == father[p]) return p; return find(father[p]); } void dfs(int i) { vis[i] = 1; for (int j = 0; j < edges[i].size(); j++) { int nex = edges[i][j]; if (!vis[nex]) { dfs(nex); father[nex] = i; } } for (int j = 1; j <= n; j++) { if (vis[j] && !lca[i][j]) lca[i][j] = lca[j][i] = find(j); } } int main() { while (~scanf("%d", &n)) { memset(lca, 0, sizeof(lca)); memset(ans, 0, sizeof(ans)); memset(notRoot, 0, sizeof(notRoot)); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { father[i] = i; edges[i].clear(); } int v, k, son; char ch[100]; for (int i = 1; i <= n; i++) { scanf("%d", &v); scanf("%[^0-9]", ch); scanf("%d", &k); scanf("%[^0-9]", ch); for (int i = 0; i < k; i++) { scanf("%d", &son); edges[v].push_back(son); notRoot[son] = true; } } for (int i = 1; i <= n; i++) if (!notRoot[i]) dfs(i); scanf("%d", &q); int a, b; for (int i = 0; i < q; i++) { scanf("%[^0-9]", ch); scanf("%d", &a); scanf("%[^0-9]", ch); scanf("%d", &b); ans[lca[a][b]]++; } scanf("%[^0-9]", ch); for (int i = 1; i <= n; i++) { if (ans[i]) printf("%d:%d\n", i, ans[i]); } } return 0; }
相关文章推荐
- 【LCA最近公共祖先】 poj1470 Closest Common Ancestors
- POJ1470 Closest Common Ancestors(最近公共祖先lca,离线Tarjan)
- POJ 1470 Closest Common Ancestors【最近公共祖先LCA】
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
- POJ 1470 Closest Common Ancestors(最近公共祖先)
- poj 1470:Closest Common Ancestors(最近公共祖先,Tarjan模版)
- POJ 1470 Closest Common Ancestors 最近公共祖先
- TOJ 3033 ZOJ 1141 POJ 1470 Closest Common Ancestors / 最近公共祖先
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
- POJ1330 Nearest Common Ancestors【最近公共祖先】【Tarjan-LCA算法】
- [POJ 1330]Nearest Common Ancestors(LCA最近公共祖先)
- POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)
- Closest Common Ancestors+poj+最近公共祖先
- POJ1330 Nearest Common Ancestors(最近公共祖先LCA 并查集+DFS)
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
- POJ - 1330 Nearest Common Ancestors(LCA最近公共祖先 朴素算法&倍增法)
- poj 1330 Nearest Common Ancestors(最近公共祖先(LCA))
- poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)
- 【LCA】 POJ 1470 Closest Common Ancestors
- poj 1470 Closest Common Ancestors(LCA)