Tarjan算法就不说了 想学看这 https://www.byvoid.com/blog/scc-tarjan/ https://www.byvoid.com/blog/biconnect/ 下面是几份基本的模版 首先是无向图割点桥的代码 下面的代码是用于求割点数目的 其中add_block[u] = x 表示删除u点之后增加的联通块个数.注意是增加的联通块个数 ; ; const int INF = 0x3f3f3f3f; struct Edge { int u,v,next; int w
2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无向图,用DFS来进行暴力搜索解决问题.因为没有什么数据顺序或是范围的限制,所以搜索剪枝好像也不太容易. 代码: // 4.9 Find all paths in a binary tree, the path doesn't have to start or end at the root or a
2014-03-19 05:04 题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树.子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样. 解法:首先可以根据节点个数省去一大部分不必要的搜索,然后再递归判断.代码还比较简单,请看下面. 代码: // 4.8 Check if a tree is a subtree of another. #include <cstdio> #include <unordered_map> using nam
2014-03-19 04:48 题目:最近公共父节点问题. 解法1:Naive算法,先对其高度,然后一层一层往上直到找到结果. 代码: // 4.7 Least Common Ancestor // This solution is Naive Algorithm, may timeout on very large and skewed trees. #include <cstdio> #include <cstring> using namespace std; ; // t
2014-03-19 04:16 题目:找出一棵二叉搜索树中的中序遍历后继节点,每个节点都有指针指向其父节点. 解法1:分两种情况:向下走时,先右后左:向上走时,先左后右.如果目标节点有右子树,就向右下走,否则往左上走.话说,如果没有父指针的话,还是一口气进行各中序遍历,求出所有结果比较有效率. 代码: // 4.6 Find the inorder successor of a node in the binary tree. // online algorithm with parent p
2014-03-19 04:11 题目:设计算法检查一棵二叉树是否为二叉搜索树. 解法:既然是二叉搜索树,也就是说左子树所有节点都小于根,右子树所有节点都大于根.如果你真的全都检查的话,那就做了很多重复工作.只需要将左边最靠右,和右边最靠左的节点和根进行比较,然后依照这个规则递归求解即可. 代码: // 4.5 Check if a binary tree is binary search tree. #include <cstdio> using namespace std; struct
2014-03-19 03:32 题目:给定一个有向图,判断其中两点是否联通. 解法:DFS搜索解决,如果是无向图的话,就可以用并查集高效解决问题了. 代码: // 4.2 Write a program to check if there exists a path between two nodes in a directed graph. #include <algorithm> #include <cstdio> #include <unordered_map>
2014-03-19 03:30 题目:判断一个二叉树是否为平衡二叉树,即左右子树高度相差不超过1. 解法:递归算高度并判断即可. 代码: // 4.1 Implement an algorithm to check if a bianry tree is height-balanced. #include <algorithm> #include <cstdio> #include <unordered_map> using namespace std; struct
1.LaTeX文件的框架如下: \documentclass{article} \begin{document} This is the body of the article \end{document} 第一句:\documentclass[选项]{类},确定整篇文章的处理格式,期刊或者会议论文一般可选类为article,再付上控制全局格式的选项,比如字体.字号.页面格式.纸张大小等等.也有期刊直接提供类模板,比如 Lecture Notes in Computer Science,只要把相