题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的。树上所有边的边权是1。 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分情况讨论。 一开始困扰我的问题是如果lca不是正中间的点,如何在比较低的复杂度的层面上求解中点。 倍
分类:
其他好文 时间:
2016-03-22 09:03:46
阅读次数:
357
给你N个点的无向图 (1 <= N <= 15,000),记为:1…N。 图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_j ( 1 < = d_j < = 1,000,000,000). 现在有 K个询问 (1 < = K < = 15,000)。 每个询问的格式是:
分类:
Web程序 时间:
2016-03-20 19:35:47
阅读次数:
270
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikip
分类:
其他好文 时间:
2016-03-13 14:10:33
阅读次数:
149
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia
分类:
其他好文 时间:
2016-03-13 08:57:50
阅读次数:
136
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowes
分类:
其他好文 时间:
2016-03-13 08:52:28
阅读次数:
134
题目链接:http://poj.org/problem?id=3694 题意是给你一个无向图n个点,m条边,将m条边连接起来之后形成一个图,有Q个询问,问将u和v连接起来后图中还有多少个桥。 首先用tarjan标记点的low和dfn值,那么u和v相连的边是桥的条件是dfn[u] < low[v](说
分类:
Web程序 时间:
2016-03-11 22:17:41
阅读次数:
362
题目:
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betwee...
分类:
其他好文 时间:
2016-03-07 15:06:42
阅读次数:
148
题目链接 先求拓扑序 现在要求一棵树,如果某一个结点消失,那么整棵子树都会消失 一个点能影响的只能是在拓扑序中比它靠前的,于是边往树中加点,就边求一个点在原图中连接的点的lca 为了保证这些点能受到当前要加入点的影响,就按拓扑序加点,最后dfs一遍找出子树大小 1 #include<algorith
分类:
其他好文 时间:
2016-02-29 16:35:49
阅读次数:
223
两个最近的点u和v的最近的公共的祖先称为最近公共祖先(LCA)。普通的LCA算法,每算一次LCA的时间复杂度为线性o(n); 这里讲LCA + 二分的方法。首先对于任意的节点v,利用其父节点的信息,可以通过par2[v]=par[par[v]]得到向上走两步的节点。依此信息可以通过par4[v]=p
分类:
其他好文 时间:
2016-02-26 23:34:06
阅读次数:
194