![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
模板
文章平均质量分 80
尉水风
这个作者很懒,什么都没留下…
展开
-
树的建立和遍历
这段代码实现了二叉树的建立和前序、中序、后序、层序四种输出顺序#include#include #include#includeusing namespace std;struct tree{ int data; struct tree *l,*r;};void creatTree(int a,struct tree * &t);void preorder(struct原创 2017-07-26 10:33:52 · 296 阅读 · 0 评论 -
最小生成树
#define maxn ???int father[maxn];void init(){ for(int i=1;i<=n;i++) father[i]=i;}int find(int x){ return x==father[x]?x:(x=find(father[x]));}int kruskal(int n,int m) //n个端点,m条边 { in...原创 2018-05-24 18:55:45 · 168 阅读 · 0 评论 -
gcd和扩展gcd
//求解ax+my=1 int exGCD(int a,int b,int &x,int &y){ if(b==0) { x=1; y=0; return a; } int g=exGCD(b,a%b,x,y); int tmp=x; x=y; y=tmp-a/b*y; return g;} int gcd(int a,int b){ if...原创 2018-05-24 18:54:55 · 349 阅读 · 0 评论 -
树状数组 背包 模板
//树状数组#define lowbit(x) ((x)&(-x))#define maxn ???int an[maxn];int getsum(int x){ int sum=0; for(int i=x;i>0;i-=lowbit(i)) sum+=an[x]; return sum;}void update(int x,i...原创 2018-03-30 22:14:12 · 174 阅读 · 0 评论 -
KMP
int next[...];//大小为pattern串的长度 //常规写法 void getNext(char s[],int len){ int j=-1; next[0]=-1; for(int i=1;i<len,i++) { while(j!=-1&&s[i]!=s[j+1]) { j=next[j]; } if(s[i]==s[j+1]) {原创 2018-03-03 21:35:10 · 187 阅读 · 0 评论 -
树的直径
树的直径即树上最大长度,可以用两次dfs或者bfs求解证明如下:设树上有s,t,u,v四点,路径s-t是最长路径,从u点出发1.u点在s-t路径上,第一次就会找到距u最远的s或t点,第二次就会找到s-t路径;若第一次找到距u点最远的点是v,那么dis[u][v]>max(dis[s][u],dis[t][u]),则dis[s][t]<max(dis[s][v],dis[t][v]),与...原创 2018-03-08 21:24:04 · 322 阅读 · 0 评论 -
矩阵快速幂
#include<iostream>#include<cstring>using namespace std;typedef struct Matrix{ int matrix[35][35]; Matrix() { memset(matrix,0,sizeof(matrix)); }}Matrix;Matrix M_multi(Matrix a,M...原创 2018-03-06 18:56:49 · 161 阅读 · 0 评论 -
输出树形结构的二叉树
输入:ABC##DE#G##F###输入所表示的二叉树 A / B / \ C D / \ E F \原创 2017-12-17 21:57:42 · 7792 阅读 · 1 评论 -
线段树模板
//在我的理解里线段树并不是数据结构上所谓的树,而是通过数组模拟,通过//函数进行下标转换(查询) #include#define maxnusing namespace std; struct list{ int left; int right; int _max; int weight; //区间 }tree[maxn*4]; //乘4可以保证不会越界原创 2017-08-08 09:51:27 · 261 阅读 · 0 评论 -
树状数组(模板)
#define maxn#define lowbit(x) ((x)&(-x))int c[maxn]; //i号位之前(含i号位)lowbit(i)个整数之和 //点更新 //update函数将第x个整数加上vvoid update(int x,int v){ for(int i=x;i<=n;i+=lowbit(i)) { c[i]+=v; }} //区间查询原创 2017-08-08 19:31:28 · 223 阅读 · 0 评论 -
拓扑排序
vector<int> G[maxv]; //邻接表 int n,inDegree[maxv];//顶点数,入度bool toplogicalSort(){ int num=0; //记录顶点数 queue<int> q; for(int i=0;i<n;i++) { if(inDegree[i]==0) { q.push(i)...原创 2018-05-24 18:56:42 · 182 阅读 · 0 评论