杂项处理

· · 个人记录

保留位数

    printf("%.3f",a);
    cout<<fixed<<setprecision(3)<<a;

对拍

::::info[扩展名改成 .bat]

makeExample.exe > testCode.in
testCode < testCode.in > testCode.out
correctCode < testCode.in > correctCode.out
fc testCode.out correctCode.out
if errorlevel==1 pause
%0

::::

spfa P3371 【模板】单源最短路径(弱化版)

dijkstra P4779 【模板】单源最短路径(标准版)

快读

::::info[同步流]

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

::::

::::info[代码]

int read(){
    int x=0;int f=1;char c=getchar();
    while(c<'0' or c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0' and c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
    return x*f;
}

::::

快写

::::info[代码]

inline void mwrite(int a){
    if(a>9)mwrite(a/10);
    putchar((a%10)|48);
}

::::

重载运算符

::::info[代码]

struct node{
    int w,id;
    bool operator<(const node& other) const {
        // 从小到大
        return w<other.w;
    }
};

::::

去重

::::info[代码]

    sort(a.begin(),a.end());
    a.erase(unique(a.begin(),a.end()),a.end());

::::

快速幂 P1226 【模板】快速幂

离散化

::::info[代码]

    for(int i=1;i<=n;i++)   b[i]=a[i];
    sort(b+1,b+1+n);
    for(int i=1;i<=n;i++)
        if(b[i]!=b[i-1]) 
            c[b[i]]=++ii;/*c[i]表示的是值为i的数离散化后的数*/
    for(int i=1;i<=n;i++)   b[i]=c[a[i]];/*b[i]是离散化后的数组*/

STL 版的:

sort(v.begin(),v.end());        
v.erase(unique(v.begin(),v.end()),v.end());      

::::

tarjan(连通性) P2863 [USACO06JAN] The Cow Prom S

博客园

::::info[代码]

void dfs(int u){
    dfn[u]=low[u]=++cn;
    st[++top]=u;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].to;
        if(!dfn[v]){
            dfs(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v]){
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u]){
        int cur;
        while(u!=cur){
            cur=st[top--];
            vis[cur]=0;
        }
    }
}

:::: ::::success[low[u]=min(low[u],dfn[v])] 为什么是和 dfn[v] 比,理解一下“now 一定能到达 e[i].v,但不一定能到达 low[e[i].v]”(个人认为这句话是有问题的)。

Tarjan 爷爷自己的解释:

压根没懂,实际上问题在于low表示 **通过至多一条非树边**,两条都不行,那用 low 会有什么问题呢?实际上,在所有的Tarjan模版中,如果将dfn改成low都不会有问题(也有可能是没卡出来)除了 **割点** 的板子,举个例子: ![](https://cdn.luogu.com.cn/upload/image_hosting/sd730e3z.png) 假设先走 5 这条支路,很显然的是,正确算下来 low[3]=2(用 dfn[2] 去更新 low[4]),而如果用 low[2] 去更新 low[4] 的话,那 low[3]=low[2]=1,这样一来,在判断点2的时候,2将不会被判成一个割点(low[3]<dfn[2]),所以就不对。 :::: ### 欧拉筛 [P3383 【模板】线性筛素数](https://www.luogu.com.cn/problem/P3383) ### 2-SAT [P4782 【模板】2-SAT](https://www.luogu.com.cn/problem/P4782) ::::info[记住就行] 我们求解 2-sat 的过程为: 1. 建图,将限制 若 p,则 q 的点连接起来。 2. 缩点。 3. 如果一个状态的正负两个点再同一个 scc 中则无解。 4. 否则一定有解;对每个变量取所在分量 scc 标号较小的对应状态 ::::