hdu 4409 LCA】的更多相关文章

思路:就是个比较裸的LCA了,不过要注意的是,如果a和b的公共祖先是a,那么答案就是farther[a]. #include<cstring> #include<cmath> #include<vector> #include<algorithm> #include<map> #include<string> #include<iostream> #include<cstdio> #define Maxn 30…
题意: 给出一些名字,名字间有父子关系,有三种操作: 1.按祖先到后代,兄弟间按字典序由小到大排序,然后输出 2.求某个节点的兄弟节点有多少个,包括自己(注意,根节点的兄弟节点是1) 3.求节点a和b的公共祖先 (注意:如果公共祖先是a或b,必须要输出其父亲,与传统的LCA可以是自己不同) 解法: 先把整棵树整理出来,son[u]表示u的儿子个数,用来求兄弟个数, fa[u]表示父亲,Gson存储儿子的标号,关于排序的问题,先读入所有名字,然后排个序哈希一下,使字典序小的节点标号一定小,那么直接…
Family Name List Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 882    Accepted Submission(s): 271 Problem Description Kong belongs to a huge family. Recently he got a family name list which l…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3078 题目大意:定点修改.查询树中任意一条树链上,第K大值. 解题思路: 先用离线Tarjan把每个Query树链的LCA求出来. LCA中对连接树Dfs的时候,令p[v]=u,记录v的前驱. LCA结束后,对于每个Query: 从u开始回溯到LCA,记录值.从v开始回溯到LCA,记录值. 再加上LCA这个点的值,形成一条完整树链.特判树链长度是否小于K. 对树链中的值,从大到小排序,取第K大即可…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 /   \ 2      3 设dist[i]为i到根0的链和,求法(Dfs过程中dist[v]=dist[u]+e[i].w) 对于树中任意两点形成的链,可以通过LCA最近公共祖先剖分. 比如2->3,就可以经过LCA点1:  2->1->3 链和=dist[u]+dist[v]-2*dist[LCA[u,v]] (…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 在线版本: 在线方法的思路很简单,就是倍增.一遍dfs得到每个节点的父亲,以及每个点的深度.然后用dp得出每个节点向上跳2^k步到达的节点. 那么对于一个查询u,v,不妨设depth[u]>=depth[v],先让u向上跳depth[u]-depth[v]步,跳的方法就是直接用数字的二进制表示跳. 然后现在u和v都在同一深度上了,再二分找向上共同的祖先,就可以二分出lca了.复杂度nlogn预…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 思路:近乎纯裸的LCA,只是题目给出的是森林,就要判断是否都在同一颗树上,这里我们只需判断两个子节点的根节点是否相同就可以了. http://paste.ubuntu.com/5957639/…
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:n个村庄构成一棵无根树,q次询问,求任意两个村庄之间的最短距离 思路:求出两个村庄的LCA,dis[ i ] 表示结点 i 到树根的距离,那么任意两点u,v的最短距离就是dis[ u ]  - dis [LCA] + dis [ v ] - dis[ LCA ].代码是用tarjan做的,算是模板,记录一下. AC代码: #include<iostream> #include<cst…
How far away ? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Submission(s): Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like…
题目大意: 多点形成一棵树,树上边有权值,给出一堆询问,求出每个询问中两个点的距离 这里求两个点的距离可以直接理解为求出两个点到根节点的权值之和,再减去2倍的最近公共祖先到根节点的距离 这是自己第一道lca题目 学习了两种方法 第一种在ST算法,利用RMQ思想预处理 /*在线ST算法*/ #pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <iostream&…
传送门 (这次的英文题面要比上一个容易看多了) (英语蒟蒻的卑微) 又是一个很裸的LCA题 (显然,这次不太容易打暴力咧) (但听说还是有大佬用dfs直接a掉了) 正好 趁这个机会复习一下LCA 这里用的是倍增lct的思想(一不小心是会写tle的吧) (也有可能是因为我太弱了才t掉的) 我才不会说我有一次卑微了 一开始看PPT 以为自己明白了 结果发现 有一点点问题 由于本题中这个树是题里给的 所以节点的编号不可以简简单单认为是从上到下从左到右顺序排列的 (但其实这部算什么大问题啊qwq) (还…
题意: n个点 m个询问 下面n个数字表示点权值 n-1行给定一棵树 m个询问 k u v k为0时把u点权值改为v 或者问 u-v的路径上 第k大的数 思路: LCA转RMQ求出 LCA(u,v) ; 登山坡式找到路径上所有点并记录其权值 排序输出k大的数 #include<iostream> #include<stdio.h> #include<algorithm> #include<string> #include<queue> #incl…
题意:在Windows下我们可以通过cmd运行DOS的部分功能,其中CD是一条很有意思的命令,通过CD操作,我们可以改变当前目录. 这里我们简化一下问题,假设只有一个根目录,CD操作也只有两种方式: 1. CD 当前目录名\...\目标目录名 (中间可以包含若干目录,保证目标目录通过绝对路径可达) 2. CD .. (返回当前目录的上级目录) 现在给出当前目录和一个目标目录,请问最少需要几次CD操作才能将当前目录变成目标目录? 链接:点我 先返回到根目录,然后直接前进到目标目录 目录刚好成一颗树…
Network Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 847    Accepted Submission(s): 347 Problem Description The ALPC company is now working on his own network system, which is connecting all…
题意及思路 说一下为什么按LCA深度从深到浅贪心是对的.我们可以直观感受一下,一条的路径会影响以这个lca为根的这颗树中的链,而深度越深,影响范围越小,所以先选影响范围小的路径. #include <bits/stdc++.h> using namespace std; const int maxn = 100010; int head[maxn], Next[maxn * 2], ver[maxn * 2], tot, f[maxn][20]; int deep[maxn], t; stru…
#include<stdio.h> #include<string.h>//用c/c++会爆栈,用g++ac #define inf 0x3fffffff #define N 41000 struct node { int u,v,w,next; }bian[N*2]; int head[N],yong; int pre[N],dis[N],deep[N]; void addedge(int u,int v,int w) { bian[yong].u=u; bian[yong].v…
给出N个点,M条边.Q次询问 Q次询问每两点之间的最短距离 典型LCA 问题   Marjan算法解 #include "stdio.h" #include "string.h" struct Edge { int to,next,len; }edge[20010]; struct Ques { int to,next,index; }ques[2000010]; int head[10010],q_head[10010],f[10010],dis[10010];…
Paths on the tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 297    Accepted Submission(s): 93 Problem Description bobo has a tree, whose vertices are conveniently labeled by 1,2,-,n. Th…
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 747    Accepted Submission(s): 144 Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes…
先给个LCA模板 HDU 1330(LCA模板) #include <cstdio> #include <cstring> #define N 40005 struct Edge{ int x,y,d,ne; }; Edge e[N*],e2[N*]; int be[N],be2[N],all,all2,n,m; bool vis[N]; int fa[N]; ]; int dis[N]; void add(int x, int y, int d, Edge e[], int be…
比赛链接: http://vjudge.net/contest/view.action?cid=47642#overview 题目来源: 2012 ACM/ICPC Asia Regional Jinhua Online     ID Origin Title   18 / 70 Problem A HDU 4400 Mines  (搜索, ★★★☆☆)   1 / 1 Problem B HDU 4401 Battery (数学, ★★☆☆☆ )    2 / 2 Problem C HDU…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4547 思路:这题的本质还是LCA问题,但是需要注意的地方有: 1.如果Q中u,v的lca为u,那么只需一步u->...->v. 2.如果Q中u,v的lca为v,那么需abs(dist[u]  - dist[v])步. 3.否则以上情况都不满足,那么需abs(dist[v] - dist[lca(u, v)])+1步. #include <iostream> #include <c…
Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 1643    Accepted Submission(s): 461 Problem Description   Zero and One are good friends who always have fun with each other. This time, t…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connected graph G with n nodes and m edges, with possibly repeated edges and/or loops. The stability of connectedness between node u and node v is defined by…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4822 Problem Description Three countries, Red, Yellow, and Blue are in war. The map of battlefield is a tree, which means that there are N nodes and (N – 1) edges that connect all the nodes. Each country…
http://acm.hdu.edu.cn/showproblem.php?pid=3078 题意:给出n个点n-1条边m个询问,每个点有个权值,询问中有k,u,v,当k = 0的情况是将u的权值修改成v,当k不为0的情况是问u和v的路径中权值第k大的点的权值是多少. 思路:比较暴力的方法,可能数据太水勉强混过去了.对于每一个询问的时候保留两个点之间的lca,还有计算出两个点之间的点的个数(询问的时候如果点的个数小于k就不用算了),然后tarjan算完之后对每个询问再暴力路径上的每个点放进vec…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形dp, 对于每条链u,v,w,我们只在lca(u,v)的顶点上处理它 让dp[i]表示以i为根的指数的最大值,sum[i]表示dp[vi]的和(vi为i的儿子们) 则i点有两种决策,一种是不选以i为lca的链,则dp[i]=sum[i]. 另一种是选一条以i为lca的链,那么有转移方程:dp[i]=…
How far away ? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How f…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; ; typedef pair<int , int>P; vector <P> G[MAXN]; ] , ok…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma comment(linker, "/STACK:102400000,102400000") //扩栈 #include <iostream> #include <cstdio> #include <cstring> using namespace std…