poj 1330(LCA)
来源:互联网 发布:python win32api 教程 编辑:程序博客网 时间:2024/05/16 05:00
基本题,留着模板
#include <iostream>#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <vector>#include <set>#include <queue>#include <stack>#include <climits>//形如INT_MAX一类的using namespace std;vector<int>lz[10005],que[10005];int vis[10005],nopa[10005];int n,m,pa[10005],s,e;int find(int x) { if(pa[x] != x) return pa[x] = find(pa[x]); else return x;}void LCA(int u){ int i,j; for(i=0; i<lz[u].size(); i++) //递归处理 { LCA(lz[u][i]); pa[lz[u][i]] = u; } vis[u] = 1; for(i=0; i<que[u].size(); i++) //询问处理 { if(vis[que[u][i]]) { cout << find(que[u][i]) << endl; return; } }}int main(){ int t,i,j,a,b; cin >> t; while(t--) { cin >> n; for(i=1; i<=n; i++) //初始化 { lz[i].clear(); que[i].clear(); vis[i] = 0; nopa[i] = 0; pa[i] = i; } for(i=1; i<n; i++) { scanf("%d%d",&a,&b); lz[a].push_back(b); //算是单向边,a是b的父结点 nopa[b] = 1; } scanf("%d%d",&s,&e); que[s].push_back(e); que[e].push_back(s); //询问两点连起关系 for(i=1; i<=n; i++) { if(!nopa[i]) //从总结点开始LCA { LCA(i); } } } return 0;}
- poj 1330(LCA)
- poj 1330(LCA)tarjam运用
- POJ 1330 LCA入门题(Tarjan)
- poj 1330 Nearest Common Ancestors(LCA)
- 【POJ 1330】 Nearest Common Ancestors(LCA)
- poj 1330 Nearest Common Ancestors(LCA)
- POJ 1330Nearest Common Ancestors(LCA)
- POJ-1330(LCA算法之RMQ)
- POJ 1330 Nearest Common Ancestors(LCA)
- POJ 1330 Nearest Common Ancestors(LCA)
- POJ 1330 LCA问题
- POJ 水题 1330 LCA
- poj 1330 LCA
- Poj 1330 LCA
- poj 1330 LCA
- (LCA倍增) poj 1330
- [LCA] POJ 1330
- poj 1330 (LCA模板题)
- 设计模式六大原则(3):依赖倒置原则
- 制作交叉工具链时对文件etc_profile的更改
- eclipse Vim 插件 ViPlugin 序列号生成程序
- 测试文档系列--测试报告
- ubuntu 12.10下搭建 hadoop 1.0.4 单机和伪分布模式
- poj 1330(LCA)
- 常用数据字典表
- 文档撰写入门
- Java-设计模式(Adapter适配器模式)
- Tomcat的JVM设置和连接数设置
- 语出惊人(心灵鸡汤)
- 【leetcode】Reverse Integer
- 笔试常见之C类型转换
- 如何让div中的文字只显示一行,多余的文字隐藏并加上省略号 .