poj 1330 【最近公共祖先问题+fa[]数组+ 节点层次搜索标记】
2015-08-11 21:31
525 查看
题目地址:http://poj.org/problem?id=1330
Sample Input
Sample Output
节点编号为:1-->n
代码:
Sample Input
2 16 1 14 8 5 10 16 5 9 4 6 8 4 4 10 1 13 6 15 10 11 6 7 10 2 16 3 8 1 16 12 16 7 5 2 3 3 4 3 1 1 5 3 5
Sample Output
4 3 n个节点、n-1条边,生成的一定是一棵树。找到最后的那组x y的最近的公共祖先。并查集思想实现的LCA
节点编号为:1-->n
代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <math.h> #include <iostream> #include <queue> #include <stack> #include <vector> #include <algorithm> #define N 10000+10 using namespace std; vector<int>a ; int fa ;//记录每个节点的父亲节点 int r ;//记录每个节点所在的层次 void dfs(int u, int dep) { //通过先根遍历计算每个节点的层次 r[u]=dep; vector<int>::iterator it; for(it=a[u].begin(); it!=a[u].end(); it++) { dfs(*it, dep+1); } } int main() { int tg; scanf("%d", &tg); int i, j, k; int n, u, v; while(tg--) { scanf("%d", &n); for(i=1; i<=n; i++){ a[i].clear(); fa[i]=i;//自己是自己的父亲节点 } for(i=0; i<n-1; i++){ scanf("%d %d", &u, &v); a[u].push_back(v); fa[v]=u; //u是v的父亲 } for(i=1; i<=n; i++) if(fa[i]==i) break;//找到根节点 dfs(i, 0); //计算每个节点的层次 int x, y;//寻找x y的最近公共祖先 scanf("%d %d", &x, &y); while(x!=y) { if(r[x]>r[y]) x=fa[x];//说明x的辈分比y小 将x的父亲搬过来 else y=fa[y]; } printf("%d\n", x); } return 0; }
相关文章推荐
- 软件测试基础总结
- Tempus et mobilius Time and motion栈与队列
- 2015.08.10总结
- java通过加载配置文件设计数据库
- RMAN备份时报“ORA-19504: failed to create file”和“ORA-27038: created file already exists”
- angular.copy方法
- Tomcat管理工具
- python2.7学习笔记(1) ——简介、安装、输入输出
- hdu 5375 Gray code(DP)
- POJ2429 GCD & LCM Inverse
- 借贷宝有多少人看得懂?借贷宝系统崩溃分析
- K - Leapin' Lizards - HDU 2732(最大流)
- 最大公共字串LCS问题(阿里巴巴)
- java实现邮箱发送功能
- 用RAID0 安装Ubuntu Server
- java复习-类和对象(二)
- 欢迎使用CSDN-markdown编辑器
- NOIP2013 货车运输(最大生成树+LCA)
- 链表插入排序
- HDU1272 小希的迷宫