235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the ...
分类:
其他好文 时间:
2016-04-21 16:34:57
阅读次数:
197
ur的c果然sxbk啊 ur2:有根树分治裸题,复杂度玄学$O(n\sqrt{n})$。 首先,转化为统计k的倍数的个数。 经过重心C的路径分两种情况:(设n为当前树的大小) 1、在C的子树中,直接O(n)计算; 2、一个在C的子树中,另一个不在,则枚举路径的lca=p,计算出p的不在C到当前分治结 ...
分类:
其他好文 时间:
2016-04-20 23:19:25
阅读次数:
167
还想用hash记录……果然是天真。lca转移比较简单,每次增加1。每次找是当前-1的就行了。 ...
分类:
其他好文 时间:
2016-04-19 06:13:04
阅读次数:
159
Distance Queries Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He t ...
分类:
其他好文 时间:
2016-04-19 06:12:10
阅读次数:
173
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description After World War X, a ...
分类:
移动开发 时间:
2016-04-19 06:12:04
阅读次数:
220
这里先推荐两道练习的裸题 首先是求点 【codevs4605】 LCA 就是求两个点的公共祖先,每次询问xor上上一个询问的答案。 先是两遍DFS: dfs1:把dep、siz、son求出来 dfs2:求出top和w siz[v]表示以v为根的子树的节点数 dep[v]表示v的深度(根深度为1) t ...
分类:
其他好文 时间:
2016-04-18 22:30:29
阅读次数:
307
求出这棵树的dfs序,对于一条链$u-v$,假设$st[u]\leq st[v]$,那么一条链不经过点$x$当且仅当它满足下面任意一个条件: 1.$st[v]<st[x]$ 2.$st[u]>en[x]$ 3.$st[x]<st[lca(u,v)]\leq en[x]$ 4.$st[u]<st[x] ...
分类:
其他好文 时间:
2016-04-18 06:26:40
阅读次数:
262
Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In t ...
分类:
其他好文 时间:
2016-04-14 01:07:51
阅读次数:
161
2588: Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权。其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文。 ...
分类:
其他好文 时间:
2016-04-13 23:38:56
阅读次数:
237
4月4日,应学弟要求去了次学校讲了一堂课,其实讲的挺内容挺杂的,但是目的是引出LCA算法。 现在整理一下当天讲课的主要内容: 开始并没有直接引出LCA问题,而是讲了RMQ(Range Minimum/Maximum Query)问题。 RMQ指的是对于给定的一个数组,每一次询问一个区间[L,R]中数 ...
分类:
编程语言 时间:
2016-04-13 02:04:58
阅读次数:
166