POJ 1330 Tarjan LCA、ST表(其实可以数组模拟)
题意:给你一棵树,求两个点的最近公共祖先。
思路:因为只有一组询问,直接数组模拟好了。
(写得比较乱)
原题请戳这里
#include <cstdio>
#include <bitset>
#include <cstring>
#include <algorithm>
using namespace std;
int first[10005],v[10005],next[10005];
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
bitset<10005> b;
memset(first,-1,sizeof(first));
int tot=1,n,x,s,e;
scanf("%d",&n);
for(int i=1;i<n;i++)
{
scanf("%d%d",&v[tot],&x);
next[i]=first[x];
first[x]=tot++;
}
scanf("%d%d",&s,&e);
b.flip(s);
while(first[s]!=-1)
{
s=v[first[s]];
b.flip(s);
}
if(b.test(e))
{
printf("%d\n",e);
goto end;
}
while(first[e]!=-1)
{
e=v[first[e]];
if(b.test(e))
{
printf("%d\n",e);
break;
}
}
end:;
}
}
搞个Tarjan玩玩
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int first[10005],cas,next[10005],v[10005],s,e,ans,f[10005];
bool vis[10005];
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
void Tarjan(int x)
{
f[x]=x;
for(int i=first[x];~i;i=next[i])
{
Tarjan(v[i]);
f[find(v[i])]=x;
}
vis[x]=1;
if(x==s&&vis[e])
{
ans=find(e);
}
else if(x==e&&vis[s])
{
ans=find(s);
}
}
int main()
{
scanf("%d",&cas);
while(cas--)
{
memset(first,-1,sizeof(first));
memset(vis,0,sizeof(vis));
int tot=1,n,x,root;
scanf("%d",&n);
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&v[tot]);
next[i]=first[x];
vis[v[tot]]=1;
first[x]=tot++;
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
root=i;
break;
}
}
memset(vis,0,sizeof(vis));
scanf("%d%d",&s,&e);
Tarjan(root);
printf("%d\n",ans);
}
}
(对于这道题,还是模拟大法好啊)
如果给多组询问,就只能用Tarjan或者ST了。。
ST的:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int cases,root,n,jyx,jyy,tot,fa[100005][19];
int in[100005],dep[100005],first[100005],next[100005],v[100005];
void add(int x,int y){
v[tot]=y;
next[tot]=first[x];
first[x]=tot++;
}
void dfs(int x)
{
for(int i=1;i<=18;i++)
{
fa[x][i]=fa[fa[x][i-1]][i-1];
}
for(int i=first[x];~i;i=next[i])
{
dep[v[i]]=dep[x]+1;
dfs(v[i]);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
for(int i=18;i>=0;i--)
{
if(dep[x]>=dep[y]+(1<<i))x=fa[x][i];
if(x==y)return x;
}
for(int i=18;i>=0;i--)
{
if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
}
return fa[x][0];
}
int main()
{
scanf("%d",&cases);
while(cases--){
memset(first,-1,sizeof(first));
memset(in,0,sizeof(in));
tot=0;
scanf("%d",&n);
for(int i=1;i<n;i++){
scanf("%d%d",&jyx,&jyy);
fa[jyy][0]=jyx;
add(jyx,jyy);
in[jyy]++;
}
scanf("%d%d",&jyx,&jyy);
for(int i=1;i<=n;i++)if(!in[i])root=i;
dfs(root);
printf("%d\n",lca(jyx,jyy));
}
}
POJ 1330 Tarjan LCA、ST表(其实可以数组模拟)的更多相关文章
- 51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径
51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径 题面 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即 ...
- poj 1330(RMQ&LCA入门题)
传送门:Problem 1330 https://www.cnblogs.com/violet-acmer/p/9686774.html 参考资料: http://dongxicheng.org/st ...
- POJ 3264 Balanced Lineup | st表
题意: 求区间max-min st表模板 #include<cstdio> #include<algorithm> #include<cstring> #inclu ...
- st表树状数组入门题单
预备知识 st表(Sparse Table) 主要用来解决区间最值问题(RMQ)以及维护区间的各种性质(比如维护一段区间的最大公约数). 树状数组 单点更新 数组前缀和的查询 拓展:原数组是差分数组时 ...
- BZOJ 4556: [Tjoi2016&Heoi2016]字符串(后缀数组 + 二分答案 + 主席树 + ST表 or 后缀数组 + 暴力)
题意 一个长为 \(n\) 的字符串 \(s\),和 \(m\) 个询问.每次询问有 \(4\) 个参数分别为 \(a,b,c,d\). 要你告诉它 \(s[a...b]\) 中的所有子串 和 \(s ...
- poj 1330(初探LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23795 Accept ...
- POJ 1330 (LCA)
http://poj.org/problem?id=1330 题意:给出一个图,求两个点的最近公共祖先. sl :水题,贴个模板试试代码.本来是再敲HDU4757的中间发现要用LCA, 操蛋只好用这 ...
- POJ 1330(LCA/倍增法模板)
链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: #inc ...
- Nearest Common Ancestors POJ - 1330 (LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 34657 Accept ...
随机推荐
- JTable设置表格背景颜色——隔行不同
package view; import java.awt.Color; import java.awt.Component; import javax.swing.JLabel; import ja ...
- ES6 箭头函数this指向问题
var name = "window"; var person1 = { name: "person1", show1: function() { consol ...
- MarkDown 语法及使用
MarkDown #什么是Markdown - 定义 - markdown 是一款轻量级标记语言,功能没有HTML标记语言那么强大 ,Markdown专注书写! #试用人群: 程序员/等计算机爱好者 ...
- 史上最详细的CentOS 7 安装 HBase教程
1. 前半部分参考 https://www.cnblogs.com/ivictor/p/5906433.html 2.问题 namenode无法启动,参考 https://stackoverflow. ...
- vuejs验证码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> ...
- Redis学习总结(1)——Redis内存数据库详细教程
1.redis是什么 2.redis的作者何许人也 3.谁在使用redis 4.学会安装redis 5.学会启动redis 6.使用redis客户端 7.redis数据结构 – 简介 8.redis数 ...
- Tensorflow人工智能入门(一)
前言: 作为一个程序员,已经离开开发岗好多年,最近突然迷茫了,不知道自己何去何从.互联网技术发展的速度已快得难以想象,许久不码代码的手也越来越僵直,需求沟通中的套话和空话却越发的熟练,这和当年入行时的 ...
- CodeForces - 284C - Cows and Sequence
先上题目: C. Cows and Sequence time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- 【ACM】nyoj_132_最长回文子串_201308151713
最长回文子串 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 输入一个字符串,求出其中最长的回文子串.子串的含义是:在原串连续出现的字符串片段.回文的含义是:正着看和 ...
- ELECTRON新增模块的方法
因为electron和node.js用的V8版本不一致,所以直接使用npm安装的模块可能在electron中不可用,特别是使用c.c++开发的模块.官方的说明:https://github.com/e ...