poj1330lca入门题
直接套模板,dfs的时候注意起点
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; vector<int>v[N];
int depth[N],father[][N];
void dfs(int u,int f,int d)
{
depth[u]=d;
for(int i=;i<v[u].size();i++)
if(v[u][i]!=f)
dfs(v[u][i],u,d+);
}
void init(int n)
{
int x=;
while(father[][x]!=-)x=father[][x];
dfs(x,-,);
for(int i=;i<;i++)
for(int j=;j<=n;j++)
father[i][j]=father[i-][father[i-][j]];
}
int lca(int x,int y)
{
if(depth[x]>depth[y])swap(x,y);
for(int i=;i<;i++)
if((depth[y]-depth[x])>>i&)
y=father[i][y];
if(x==y)return x;
for(int i=;i>=;i--)
{
if(father[i][x]!=father[i][y])
{
x=father[i][x];
y=father[i][y];
}
}
return father[][x];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int t,n;
cin>>t;
while(t--){
cin>>n;
for(int i=;i<=n;i++)v[i].clear();
memset(father,-,sizeof father);
memset(depth,,sizeof depth);
for(int i=;i<n;i++)
{
int a,b;
cin>>a>>b;
father[][b]=a;
v[a].push_back(b);
}
init(n);
int a,b;
cin>>a>>b;
cout<<lca(a,b)<<endl;
}
return ;
}
/******************** ********************/
poj1330lca入门题的更多相关文章
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- hdu 1754:I Hate It(线段树,入门题,RMQ问题)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- poj 3254 状压dp入门题
1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...
- zstu.4194: 字符串匹配(kmp入门题&& 心得)
4194: 字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 206 Solved: 78 Description 给你两个字符串A,B,请 ...
- hrbustoj 1073:病毒(并查集,入门题)
病毒Time Limit: 1000 MS Memory Limit: 65536 KTotal Submit: 719(185 users) Total Accepted: 247(163 user ...
- hdu 1465:不容易系列之一(递推入门题)
不容易系列之一 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)
基础数据结构——顺序表(2) Time Limit: 1000 MS Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...
随机推荐
- Python计算地图上两点经纬度间的距离
处理地图数据时,经常需要用到两个地理位置间的距离.比如A点经纬度(110.0123, 23.32435),B点经纬度(129.1344,25.5465),求AB两点之间的距离.我们可以用haversi ...
- PHP使用Mongodb
一.安装Mongodb的PHP扩展 wget http://pecl.php.net/get/mongo-1.2.7.tgz //下载扩展包tar zxvf mongo-1.2.7.tgzcd mon ...
- tomcat 介绍
Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开发 ...
- node.js---sails项目开发(3)
1.为新建的sails数据库新建一个用户,首先连接数据库 mongo localhost:27017 (1)显示所有数据库 (2)切换数据库 show dbs use sails 新建一个用户 账 ...
- android studio本地gradle
1.从网站上下载http://services.gradle.org/distributions/ 2.打开工程里的gradle-wrapper.properties, distributionUrl ...
- 简明python教程五----数据结构
python中有三种内建的数据结构:列表.元组和字典 list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.在python中,每个项目之间用逗号分隔. 列表中的项目应该包括在方 ...
- uva 11426 GCD - Extreme (II) (欧拉函数打表)
题意:给一个N,和公式 求G(N). 分析:设F(N)= gcd(1,N)+gcd(2,N)+...gcd(N-1,N).则 G(N ) = G(N-1) + F(N). 设满足gcd(x,N) 值为 ...
- master: Error: JAVA_HOME is not set and could not be found.问题解决
- Firebug入门指南(转)
本文转自:http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html 作者: 阮一峰 日期: 2008年6月 8日 据说,对于网页开发人员 ...
- hadoop21---使用代理修改List,代理流程
package cn.itcast_05_proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Met ...