TTTTTTTTTTTTTTTTTT POJ 1330
题意:给一个有根树,一个查询节点(u,v)的最近公共祖先;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=10000;
int dep[maxn+10],par[maxn+10];
int n,a,b,u,v;
vector<int> G[maxn+10]; void dfs(int cur,int d)
{
dep[cur]=d;
for(int i=0;i<G[cur].size();i++)
dfs(G[cur][i],d+1);
} int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) G[i].clear();
MM(par,0);
for(int i=1;i<=n-1;i++)
{
scanf("%d %d",&a,&b);
G[a].push_back(b);//降低复杂度,将n^2降至m
par[b]=a;
} int root;
for(int i=1;i<=n;i++)
if(!par[i]) {root=i;break;} dfs(root,1);//标记深度
scanf("%d %d",&u,&v); if(dep[u]<dep[v]) swap(u,v);
while(dep[u]>dep[v]) u=par[u]; while(u!=v)
{
u=par[u];
v=par[v];
}
printf("%d\n",u);
}
return 0;
}
分析:最基础的LCA
TTTTTTTTTTTTTTTTTT POJ 1330的更多相关文章
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)
1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...
- POJ 1330 LCA裸题~
POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineerin ...
随机推荐
- 深度解析Maven
此文来源于: https://www.cnblogs.com/hafiz/p/8119964.html 带你深度解析Maven 一.What`s Maven? Maven是基于项目对象模型(POM ...
- CDH的ntp时间同步
云服务器: ntpq -p ntpdate -u 10.52.255.1 #手动同步 自建NTP服务器: https://www.cnblogs.com/yinzhengjie/p/9480665. ...
- Tarjan算法求有向图强连通分量并缩点
// Tarjan算法求有向图强连通分量并缩点 #include<iostream> #include<cstdio> #include<cstring> #inc ...
- [Wpf]在C#中添加 collectionViewSource
Products = new ObservableCollection<Product>(products); ProductOptions = new ObservableCollect ...
- nginx 针对特定地区的ip进行规则匹配
使用geoip模块,加载ip库 geoip_country GeoIP.dat; geoip_city GeoLiteCity.dat; 转自http://ju.outofmemory.cn/entr ...
- JS作用域及域解析规则
1.JS作用域:变量和函数作用的范围. 2.JS解析器可以分为域解析和逐行解读代码两个过程. 域解析:1.当进行域解析的时候,一旦找到var,就会提取后面的变量名,并给它赋值给undefined. 2 ...
- express做登录判断
1)JWT试试 https://github.com/penguinab/express-jwt 2)express session https://github.com/whevether/reac ...
- Pycharm新建第一个Django项目
1:安装django 打开Pycharm,在creatproject那里选择新建django项目的时候,会自动帮你安装最新版的Django版本 2:进入Pycharmd的命令窗口,在下方Termina ...
- python-装饰器1
python-装饰器1 定义本质就是函数,(装饰其他函数)就是为其他函数添加附加功能原则:1.不能修改被装饰的函数的源代码2.不能修改被装饰的函数的调用方式 def logger(): print(' ...
- python-文件操作3(读写文件的详细操作)
f=open('my-heart','r') print(f.encoding)#返回字符编码 print(f.fileno())#返回操作系统的端口编号 print(f.seekable())#是否 ...