Nearest Common Ancestors Poj1330
来源:互联网 发布:spss软件应用例子 编辑:程序博客网 时间:2024/04/30 10:44
//本题用i代表每个结点,数组f[i],d[i],s[i]分别映射到该节点的父节点信息,深度信息,儿子信息。
<span style="font-family: Arial, Helvetica, sans-serif;">#include<iostream></span>
#include<vector>#include<cstdio>#include<cstdlib>using namespace std;const int N = 10000 + 5;vector<int> s[N];int f[N];int d[N];void DFS(int i, int dep) //计算每个结点的深度{ d[i] = dep; for (vector<int>::iterator it = s[i].begin(); it != s[i].end(); ++it) DFS(*it, dep + 1);}int main(){ int t, n; scanf("%d", &t); while (t--) //循环t次 { scanf("%d", &n); //memset(f, -1, sizeof(f)); for (int &tem : f) tem = -1; for (int i = 0; i < n - 1; ++i) //输入n - 1条边关系 { int x, y; scanf("%d%d", &x, &y); f[y - 1] = x - 1; //处理y的父节点属性 s[x - 1].push_back(y - 1); //处理x的儿子结点属性 } //处理结点的层次属性 int r; //保存根节点 for (r = 0; f[r] >= 0; r++) //寻找根节点 DFS(r, 0); //处理结点的层次 int x, y; scanf("%d%d", &x, &y); //输入目标结点 x--; y--; while (y != x) { if (d[x] > d[y]) x = f[x]; else y = f[y]; } printf("%d\n", x + 1); } return 0;}
0 0
- POJ1330 Nearest Common Ancestors
- POJ1330(Nearest Common Ancestors)
- POJ1330--Nearest Common Ancestors
- poj1330 Nearest Common Ancestors
- Nearest Common Ancestors Poj1330
- POJ1330 Nearest Common Ancestors
- poj1330 Nearest Common Ancestors
- poj1330 Nearest Common Ancestors
- poj1330 Nearest Common Ancestors
- POJ1330(Nearest Common Ancestors)
- POJ1330 Nearest Common Ancestors[LCA]
- 【POJ1330】Nearest Common Ancestors(LCA)
- poj1330 Nearest Common Ancestors()
- poj1330——Nearest Common Ancestors
- [LCA模板]POJ1330 Nearest Common Ancestors
- poj1330 Nearest Common Ancestors pascal代码
- 倍增LCA poj1330 Nearest Common Ancestors
- poj1330 Nearest Common Ancestors (最近公共祖先)
- Android Fragment 真正的完全解析(下)
- QGIS2.0编译与二次开发遇到问题记录
- 解剖Nginx·自动脚本篇(6)编译器名称变量脚本 auto/cc/name
- 2014-10-25Android学习--------SimpleAdapter的实现
- 网站缓存
- Nearest Common Ancestors Poj1330
- 用户识别
- N皇后问题
- 让需求活动脚踏实地(零)——搞需求太难了
- CocosEditor-java
- eclipse各版本说明与区别
- C++中的private、protected、public区别
- 常用的sql语句小结
- c语言学习的阶段性小总结