poj1330
来源:互联网 发布:nginx 绑定多个域名 编辑:程序博客网 时间:2024/06/02 01:10
//当时在写这道题的时候还不知道离线算法,但也过了,当然运行时间长
//只是简单的向根节点进行回溯,找到最近共同祖先
#include <iostream>
#include <vector>#include <stdio.h>
using namespace std;
const int N = 10000;
vector<int> a[N];
int f[N],r[N];
void DFS(int u,int dep)
{
r[u] = dep;
for(vector<int>::iterator it = a[u].begin();it != a[u].end();it++)
DFS(*it,dep + 1);
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int casenum,num,x,y,node,root;
cin>>casenum;
for(num = 0;num < casenum;num++)
{
for(int i = 0;i < N;i++)
a[i].clear();
memset(f,-1,sizeof(f));
cin>>node;
for(int i = 0;i < node -1;i++)
{
cin>>x>>y;
a[x - 1].push_back(y - 1);
f[y - 1] = x - 1;
}
for(root = 0;f[root] >= 0;root++)
;
DFS(root,0);
cin>>x>>y;
x--;
y--;
while(x != y)
{
if(r[x] > r[y])
x = f[x];
else
y = f[y];
}
printf("%d\n",x + 1);
}//casenum for
return 0;
}
0 0
- poj1330
- poj1330
- poj1330
- poj1330
- poj1330
- poj1330
- POJ1330 TarjanLCA
- POJ1330 倍增
- POJ1330 Nearest Common Ancestors
- LCA问题 poj1330 / poj1470
- poj1330 LCA离线算法
- poj1330 解题报告
- POJ1330(Nearest Common Ancestors)
- poj1330 lca转rmq
- POJ1330--Nearest Common Ancestors
- POJ1330(最近公共祖先)
- POJ1330(LCA-离线tarjan)
- poj1330 倍增LCA
- Feel Good - UVa 1619 dp
- 使用OJ评判C/C++程序可能出现的问题总结
- Linux系统编程(12)——shell基础
- scanf函数浅析
- 仿MIUI音量变化环形进度条实现
- poj1330
- 【Visual C++】游戏开发笔记三十六 浅墨DirectX提高班之四 顶点缓存的逆袭
- 算法学习-数据结构之链表操作,创建,插入,删除,查找。
- 游戏中使用的子弹
- 论Node在构建超媒体API中的作用
- mkdir: 无法创建目录"": 没有那个文件或目录
- 【Visual C++】游戏开发笔记三十七 浅墨DirectX提高班之五 顶点缓存的红颜知己:索引缓存的故事
- Android----怎么判断手机的网络连接状态
- chmod解释