POJ 1330】的更多相关文章

POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu Submit Status Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In t…
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an…
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节点编号. LCA裸题,用倍增思想. 代码总览 #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #define nmax 80520 #define demen…
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 File Name :F:\2013ACM练习\专题学习\LCA\POJ1330_3.cpp ************************************************ */ #include <stdio.h> #include <string.h> #inclu…
Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14902   Accepted: 7963 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each…
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24209   Accepted: 12604 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the fi…
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. No…
1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST */ #include<iostream> #include<stdio.h> #include<string.h> using namespace std; ; *MAXN];//rmq数组,就是欧拉序列对应的深度序列 struct ST{ *MAXN]; *MAXN][…
POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an anc…
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节点的第2j个父亲是多少   这个代码不是我的,转自 邝斌博客 /* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 File Name :F:\2013AC…
详细讲解见:https://blog.csdn.net/liangzhaoyang1/article/details/52549822 zz:https://www.cnblogs.com/kuangbin/p/3302493.html /* *********************************************** Author :kuangbin Created Time :2013-9-5 0:09:55 File Name :F:\2013ACM练习\专题学习\LCA…
http://poj.org/problem?id=1330 题意:给你一棵树的上的两个点,要你求这两个点的最近的父亲节点. 第一行的是m案例数 第二行给你个N,代表有N-1种父子关系,其中a b,a是b的父亲. 第N行就是要你求这两个点的最近的父亲节点. 思路:很简单,不用discuss里面的那些,就直接一个递归寻找出第一个点的所以父亲节点,并做好标记,然后用递归寻找第二个点的父亲节点,如果碰到了标记的话,那么这个点就是他们的最近的那个父亲节点. #include <stdio.h> #in…
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. N…
题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从树根出发进行后序深度优先遍历,设置vis数组实时记录是否已被访问. 每遍历完一棵子树r,把它并入以r的父节点p为代表元的集合.这时判断p是不是所要求的u, v节点之一,如果r==u,且v已访问过,则lca(u, v)必为v所属集合的代表元.p==v的情况类似. 我的第一道LCA问题的Tarjan算法…
题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从树根出发进行后序深度优先遍历,设置vis数组实时记录是否已被访问. 每遍历完一棵子树r,把它并入以r的父节点p为代表元的集合.这时判断p是不是所要求的u, v节点之一,如果r==u,且v已访问过,则lca(u, v)必为v所属集合的代表元.p==v的情况类似. 我的第一道LCA问题的Tarjan算法…
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 34942   Accepted: 17695 Description A rooted tree is a well-known data structure in computer science and engineering. An example…
题目地址:http://poj.org/problem?id=1330 Sample Input 2 16 1 14 8 5 10 16 5 9 4 6 8 4 4 10 1 13 6 15 10 11 6 7 10 2 16 3 8 1 16 12 16 7 5 2 3 3 4 3 1 1 5 3 5 Sample Output 4 3 n个节点.n-1条边,生成的一定是一棵树.找到最后的那组x y的最近的公共祖先.并查集思想实现的LCA 节点编号为:1-->n 代码: #include <…
http://poj.org/problem?id=1330 题意:给出一个图,求两个点的最近公共祖先. sl :水题,贴个模板试试代码.本来是再敲HDU4757的中间发现要用LCA,  操蛋只好用这个题目试试自己写的对不对. 下面是个倍增的写法,挺实用的. 好了,继续... ;  ][MAX],dep[MAX];       G[     G[to].push_back( }      dep[u]=d; parent[][u]=pre;     ;i<G[u].size();i++) {  …
http://poj.org/problem?id=1330 题目意思就是T组树求两点LCA. 这个可以离线DFS(Tarjan)-----具体参考 O(Tn) 0ms 还有其他在线O(Tnlogn)也可参考LCA // This file is made by YJinpeng,created by XuYike's black technology automatically. // Copyright (C) 2016 ChangJun High School, Inc. // I don…
链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<set> #include<string> #include<vector&…
传送门:Problem 1330 https://www.cnblogs.com/violet-acmer/p/9686774.html 参考资料: http://dongxicheng.org/structure/lca-rmq/ 挑战程序设计竞赛(第二版) 变量解释: 对有根树进行DFS,将遍历到的节点按照顺序记下,我们将得到一个长度为2N-1的序列,称之为欧拉序列. total : 记录dfs遍历过程中回溯的节点编号,其实就是从0->2N-1. vs[ ] : 记录DFS访问的顺序,也就是…
传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Accepted: 13734 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:    In the figur…
LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.value; int right = v.value; //二叉查找树内,如果左结点大于右结点,不对,交换 if (left > right) { int temp = left; left = right; right = temp; } while (true) { //如果t小于u.v,往t的右…
Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27316   Accepted: 14052 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, eac…
Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14698   Accepted: 7839 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each…
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<queue> using namespace std; ; *MAXN];//rmq数组,就是欧拉序列对应的深度序列 struct ST { *MAXN]; *MAXN][];//最小值对应的下标 void init(int n) { mm…
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define N 10010 using namespace std; int dep[N],farther[N],vi[N],sec[N],Md,index[N],e,set[N]; struct Edge{ int to,next; }edge[N]; void…
题意:给出一棵树,再给出两个节点a.b,求离它们最近的公共祖先.方法一: 先用vector存储某节点的子节点,fa数组存储某节点的父节点,最后找出fa[root]=0的根节点root.      之后求每个节点的“高度”,更节点的高度为1,每往下一层高度+1.      读取a和b后,先求出它们位于同一个高度的祖先:      1.若此祖先相同,则即为最近的公共祖先.      2.若不相同,则求各自的父节点,知道两者的父节点相同,即为最近的公共祖先. #include <iostream>…
多校第七场考了一道lca,那么就挑一道水题学习一下吧= = 最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u,v的公共祖先. 标记的时候又犯老毛病了:while,do while都不对,最后还是while(1)了T^T #include<cstdio> #include<cstring> ; int p[MAXN],vis[MAXN]; int main() { int T,n,u,v; sc…
题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩子,vector<int> v[M]写在主函数里面,放在外面超时. 代码: #include<iostream> #include<vector> #include<cstdio> #include<cstdlib> #include<cstr…