版本比较
各个算法标准代码
正文
增加 200 行删除 9 行
11234510 行未改动16171819202122232224252627282925 行未改动555657585659606162636415 行未改动808182838084858687888951 行未改动14114214314414014514614714814915054 行未改动20520620720820320921021121221321471 行未改动28628728828928329029129229329429542 行未改动33833934034133434234334434534634741 行未改动389390391392384393394395396397398109 行未改动全部展开508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692一些很有用的知识:::::info[一些很有用的知识:]
2维vector数组定义:
- 空二维 vector(先定义,后赋值)10 行未改动统一设置所有元素的初始值
vector<vector<int>>mat(3,vector<int>(4,10));
::::
各种算法标准代码:
并查集::::info[并查集]
```cpp#include <bits/stdc++.h>using namespace std;int f[5005];//f[i]表示i的目前最远祖先25 行未改动 cout<<(findfather(x)==findfather(y)?"Yes":"No"); }//输出查询结果}```美国血统(求后序遍历)::::::::info[美国血统(求后序遍历)]```cpp#include <bits/stdc++.h>using namespace std;void dfs(string bef,string mid){15 行未改动 dfs(bef,mid); return 0;}```单调栈::::::::info[单调栈]```cpp#include<bits/stdc++.h>#define ll long longusing namespace std;51 行未改动 out();//输出 return 0;}```单调队列::::::::info[单调队列]```cpp#include <bits/stdc++.h>#define ll long longusing namespace std;54 行未改动 out();//输出 return 0;}```归并排序::::::::info[归并排序]```cpp#include <bits/stdc++.h>#define ll long longusing namespace std;71 行未改动 out();//输出 return 0;}```链式前向星(无向图,无边权,求深度)::::::::info[链式前向星(无向图,无边权,求深度)]```cpp#include <bits/stdc++.h>using namespace std;int n,d,elen=0,ans=0;//n->节点数量 d->深度上限 elen->当前边总数 ans->结果42 行未改动 cout<<ans; return 0;}```堆(大根堆)::::::::info[堆(大根堆)]```cpp#include<bits/stdc++.h>using namespace std;vector<int>h;//堆(大根堆)41 行未改动 } return 0;}```对顶堆::::::::info[对顶堆]```cpp#include<bits/stdc++.h>using namespace std;vector<int>up;//大根堆109 行未改动全部展开 } balance();//维护对顶堆 }}::::::::info[左偏树(封装为类)]```cpp#include<bits/stdc++.h>using namespace std;struct tnode{ int value; int dist; tnode* left; tnode* right; tnode(int v):value(v),dist(0),left(nullptr),right(nullptr){}};struct heap{ private: tnode* root; bool cmp(tnode* x,tnode* y){//compare x with y return x->value<y->value; } tnode* mergenode(tnode* x,tnode* y){//merge two heaps who use x and y for root if(!x)return y; if(!y)return x; if(cmp(y,x))swap(x,y); x->right=mergenode(x->right,y); int ld=(x->left)?x->left->dist:-1; int rd=(x->right)?x->right->dist:-1; if(ld<rd)swap(x->left,x->right); x->dist=(x->right)?x->right->dist+1:0; return x; } void free(tnode* x){//free the heap who use x for root if(!x)return; free(x->left); free(x->right); delete x; } heap(const heap&)=delete;//you mustn't copy heap& operator=(const heap&)=delete;//you mustn't use '=' to give a heap to another heap public: heap():root(nullptr){}//build heap ~heap(){//delete heap free(root); } bool empty()const{//x->empty() return root==nullptr; } int top()const{//x->top() return (root==nullptr)?throw runtime_error("heap is empty"):root->value; } void merge(heap* other){//x->merge(heap* y) if(!other||other->empty())return; root=mergenode(root,other->root); other->root=nullptr; delete other; } void push(int k){//x->push(int k) tnode* newnode=new tnode(k); root=mergenode(root,newnode); } void pop(){//x->pop() if(empty())throw runtime_error("pop from empty heap"); tnode* old=root; root=mergenode(root->left,root->right); delete old; } static heap* mergeheap(heap* x,heap* y){//z=heap::mergeheap(heap* x,heap* y) if(!x)return y; if(!y)return x; tnode* xr=x->root; tnode* yr=y->root; heap* z=new heap(); z->root=mergenode(xr,yr); if(x)x->root=nullptr; if(y)y->root=nullptr; delete x; delete y; return z; }};int main(){ }```::::::::info[2-3-4树]```cpp#include<bits/stdc++.h>using namespace std;struct num{ int val; int count; num(int v):val(v),count(1){}};struct tnode{ num* left; num* mid; num* right; int size; tnode* left_left; tnode* left_right; tnode* right_left; tnode* right_right; tnode(int v):left(nullptr),mid(new num(v)),right(nullptr),size(1),left_left(nullptr),left_right(nullptr),right_left(nullptr),right_right(nullptr){}};struct _2_3_4_tree{ private: tnode* root; void fill(num& x,int k){//在x的位置填充k这个数 num* old=x; x=new num(k); delete old; } void move(num& x,num& y){//将x的位置上的数移动到y的位置 y->val=x->val; y->count=x->count; x->val=0; x->count=0; } pair<int,int> insert_leaf(tnode* x,int k){ x->size++; if(!x->left&&k<x->mid){ x->left=new num(k); return {0,0}; } if(!x->right&&k>x->mid){ x->right=new num(k); return {0,0}; } if(k==x->mid->val){ x->mid->count++; return {0,0}; } if(k==x->left->val){ x->left->count++; return {0,0}; } if(k==x->right->val){ x->right->count++; return {0,0}; } x->size--; if(x->left&&x->right){ int up=x->mid->val,upcount=x->mid->count; if(k<x->mid->val){ move(x->left,x->mid); } else{ move(x->right,x->mid); } insert_leaf(x,k); return {up,upcount}; } x->size++; if(k<x->mid->val){ move(x->mid,x->right); if(k<x->left){ move(x->left,x->mid); fill(x->left,k); } else{ fill(x->mid,k); } return {0,0}; } else{ move(x->mid,x->left); if(k>x->right){ move(x->right,x->mid); fill(x->right,k); } else{ fill(x->mid,k); } return {0,0}; } }};int main(){ }```::::