Nearest Common Ancestors
http://poj.org/problem?id=1330
题解:LCA
树上倍增
/*
*@Author: STZG
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=10000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v,c;
int ans,cnt,flag,temp,tot,sum,num;
int pre[N];
bool vis[N];
int dep[N];
int dp[N][21];
string str,str1;
struct node{int v,w;
}x;
vector<int>G[N];
int find(int x){return pre[x]==x?x:pre[x]=find(pre[x]);}
void marge(int u,int v){int tu=find(u);int tv=find(v);if(tu!=tv){pre[tu]=tv;}
}
void dfs(int u){vis[u]=1;for(int i=0,j=G[u].size();i<j;i++){int v=G[u][i];if(!vis[v]){dep[v]=dep[u]+1;dp[v][0]=u;dfs(v);}}
}
void init(){for(int i=1;i<=n;i++){G[i].clear();pre[i]=i;}memset(vis,0,sizeof(vis));memset(dep,0,sizeof(dep));memset(dp,0,sizeof(dp));cnt=0,num=0;
}
int LCA(int x,int y){if(dep[x]<dep[y])swap(x,y);//cout<<x<<" "<<dp[1][0]<<endl;while(dep[x]>dep[y])x=dp[x][(int)log2(dep[x]-dep[y])];if(x==y)return x;for(int i=log2(dep[x]);i>=0;i--){if(dp[x][i]!=dp[y][i])x=dp[x][i],y=dp[y][i];}return dp[x][0];
}
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);scanf("%d",&t);while(t--){scanf("%d",&n);init();for(int i=1;i<n;i++){scanf("%d%d",&u,&v);G[u].push_back(v);G[v].push_back(u);marge(v,u);}//cout<<"1"<<endl;for(int i=1;i<=n;i++){if(pre[i]==i){dfs(i);}}//cout<<"2"<<endl;for(int i=0;i<20;i++){for(int j=1;j<=n;j++){dp[j][i+1]=dp[dp[j][i]][i];}}scanf("%d%d",&u,&v);cout<<LCA(u,v)<<endl;}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif//cout << "Hello world!" << endl;return 0;
}
Nearest Common Ancestors相关推荐
- POJ 1330 Nearest Common Ancestors 【LCA模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)...
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- Nearest Common Ancestors(LCA板子)
题目链接:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 1000 ...
- LCA——JD 3055 Nearest Common Ancestors
3055: Nearest Common Ancestors Time Limit: 1 Sec Memory Limit: 128 MB Description 给定N个节点的一棵树,有K次查询 ...
- JDOJ 3055: Nearest Common Ancestors
JDOJ 3055: Nearest Common Ancestors JDOJ传送门 Description 给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先. 样例中的16和7的公共 ...
- 【POJ - 1330】Nearest Common Ancestors(lca,模板题)
题干: A rooted tree is a well-known data structure in computer science and engineering. An example is ...
- POJ - 1330 Nearest Common Ancestors tanjan_LCA
传送门 题意就是题目 所谓在线,就是一个一个贼笨且时间太长. #include<stdio.h> #include<string.h> #include<iostream ...
- [POJ1330 Nearest Common Ancestors]
[关键字]:LCA [题目大意]:求出两点间的最近公共祖先. //=================================================================== ...
- POJ - 1330 Nearest Common Ancestors(树上倍增/树链剖分求LCA)
题目链接:点击查看 题目大意:给出一棵有根树,我们需要求出两个点的lca 题目分析:因为题目说了是有根树,所以我们在建图的时候直接建有向图就好了,并且记录一下每个点的入度,建完图后找一下入度为0的点, ...
最新文章
- 一条批处理语句ping局域网内的所有机器
- Scala if...else案例
- 深度学习总结:GAN,原理,算法描述,pytoch实现
- P2710-数列【Splay】
- java中生成1000~10000之间的随机数
- html中内容超出显示省略号的方法
- JAVA编码(5)——JAVA输入流输出流
- python语言入门-Python语言十分钟快速入门
- [转]Linq查询DataTable,DataRow
- Starship Troopers
- winform程序制作安装包
- 这样选择报表系统,才能更好的进行企业管理
- Eclipse SVN还原文件到历史版本详解
- 网络篇 路由器的密码破解10
- ASP.NET Core Razor 页面入门
- 鉴权html5服务器,搭建web之 服务器鉴权失败,请确认服务器已启用密码鉴权并且账号密码正确?...
- 多核处理器_胶水多核等于Low?处理器封装没有那么简单!
- 经典Bug永流传---每周一“虫”(九)
- 随机密码生成Python
- no source Theme.AppCompat.Light的解决方法
热门文章
- sketch 将动图转换为json_开源 | Picasso:sketch设计稿智能解析工具
- php日期差数,php如何计算日期差几天
- 两个python文件怎么联系在一起_【新手求助】怎样把两个程序连接在一起?老师作业,谢谢啦...
- Linux文件系统保存在哪里,文件系统保存在磁盘的()
- ibm v5000增加磁盘_存储扩容首选 IBM Storwize V5000促销
- java异常什么时候抛出异常,java - 什么时候应该抛出IllegalArgumentException?
- 六十一、Vue中父子组件传值和组件参数校验
- 为了OFFER,菜鸟的我必须搞懂动态规划系列三个背包问题之多重背包(二进制优化方法)
- 影评情感分类(基于IMDB数据集)
- 再谈对比学习:更好的对比样本选择,更好的对比效果