容易忘的

kruskal

bool cmp(node a,node b){return a.z<b.z;}
int get(int x){
	if(fa[x]==x)return x;
	return fa[x]= get(fa[x]);
}
void kksk(){
	sort(Edge+1,Edge+m+1,cmp);
	for(int i = 1;i <= n;i++) fa[i]=i;
	for(int i = 1;i <= m;i++){
		int x=get(Edge[i].x);
		int y=get(Edge[i].y);
		if(x!=y){
			fa[x]=y;
			ans+=Edge[i].z;
			cnt++;
             if(cnt==n-1)break;
		}
	}
}

重载运算符

struct node{
    /*变量*/
    bool operator /*运算符*/ (node i)const{
        return /*变量 运算符 i.变量*/ ;
    }
};

快读

inline int read(){
    int num=0,fl=1;char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-') fl=-1;
        c=getchar();
    }
    while(c >='0'&&c <='9'){
        num=(num<<3)+(num<<1)+(c^48);
        c=getchar();
    }
    return num*fl;
}

快写

inline void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9){
        write(x/10);
    }
    putchar(x%10+'0');
}

dijiesitela

void dij(int s){
	memset(di,0x7f,sizeof di);
	memset(v,0,sizeof v);
	di[s]=0;
	q.push(make_pair(0,s));
	while(!q.empty()){
		int x=q.top().second;q.pop();
		if(v[x]) continue;
		v[x]=1;
		for(int i = head[x];i;i=next[i]){
			int y=ver[i]; int z=edge[i];
			if(di[y]>di[x]+z){
				di[y]=di[x]+z;
				q.push(make_pair(-di[y],y));
			}
		} 
	}
}

SPFA

inline void spfa(int st)
{
    memset(di,0x3f,sizeof di);
    memset(v,0,sizeof v);
    di[st]=0,v[st]=1;
    q.push(st);
    while(!q.empty()){
        int x=q.front();q.pop();
        v[x]=0;
        for(int i = E.Head[x];i;i=E.Next[i]){
            int y=E.Ver[i],z=E.Edge[i];
            if(di[y]>di[x]+z){
                di[y]=di[x]+z;
                if(!v[y]) q.push(y),v[y]=1;
            }
        }
    }
}

离散化

for(int i = 1;i<=n;i++)d[i]=hash[i]=read();
std::sort(hash+1,hash+n+1);
len=std::unique(hash+1,hash+n+1)-(hash+1);
for(int i = 1;i <= n;i++)        
    d[i]=std::lower_bound(hash+1,hash+len+1,d[i])-hash;

快速幂

inline int qpow(int a,int p){
    int b=p,base=a,ans=1;
    while(b){
        if(b&1) ans=(ans*base)%p;
        base=(base*base)%p;
        b>>=1;
    }
    return ans;
}

埃氏筛

inline void primes(int n)
{
    memset(v,0,sizeof v);
    for(int i = 2;i <= n;i++){
        if(v[i]) continue;
        p.push_back(i);
        for(int j=i;j<=n/i;j++) v[j*i]=1;
    }
}
posted @ 2023-11-01 15:42  tkt  阅读(32)  评论(2编辑  收藏  举报