11.7 NOIP总复习总结

好像要1A模板题。完败

下面主要是一波SB错误总结

最小生成树(忘了sort(QwQ))

// It is made by XZZ
// Fei Fan Ya Xi Lie~~~
#include<cstdio>
#include<algorithm>
using namespace std;
#define il inline
#define rg register
#define vd void
typedef long long ll;
il int gi(){
    rg int x=0,f=1;rg char ch=getchar();
    while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
struct frog{int a,b,c;}p[200001];
il bool operator <(const frog&a,const frog&b){return a.c<b.c;}
int fa[5001];
il int hd(const int&a){return a==fa[a]?a:fa[a]=hd(fa[a]);}
int main(){
// 	freopen("3366.in","r",stdin);
// 	freopen("3366.out","w",stdout);
    int n=gi(),m=gi()+1;
    for(rg int i=1;i<m;++i)p[i].a=gi(),p[i].b=gi(),p[i].c=gi();
    sort(p+1,p+m+1);
    for(rg int i=1;i<=n;++i)fa[i]=i;
    int orz=1;long long ans=0;
    for(rg int i=1;i<n;++i){
        while((orz^m)&&hd(p[orz].a)==hd(p[orz].b))++orz;
        if(orz==m){puts("orz");return 0;}
        fa[hd(p[orz].b)]=hd(p[orz].a);
        ans+=p[orz].c;
    }
    printf("%lld\n",ans);
    return 0;
}

SB并查集(按秩合并应该不会卡)

// It is made by XZZ
// Fei Fan Ya Xi Lie~~~
#include<cstdio>
#include<algorithm>
using namespace std;
#define il inline
#define rg register
#define vd void
typedef long long ll;
il int gi(){
    rg int x=0,f=1;rg char ch=getchar();
    while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int fa[10001];
il int hd(const int&a){return a==fa[a]?a:fa[a]=hd(fa[a]);}
int main(){
// 	freopen("3367.in","r",stdin);
// 	freopen("3367.out","w",stdout);
    int n=gi(),m=gi(),opt,x,y;
    for(rg int i=1;i<=n;++i)fa[i]=i;
    while(m--){
        opt=gi(),x=gi(),y=gi();
        if(opt==1)fa[hd(x)]=hd(y);
        else printf("%c\n","NY"[hd(x)==hd(y)]);
    }
    return 0;
}

字符串哈希

用set维护即可。防冲突用了pair,防T用了一个自然溢

// It is made by XZZ
// Fei Fan Ya Xi Lie~~~
#include<cstdio>
#include<algorithm>
#include<set>
#include<cstring>
using namespace std;
#define il inline
#define rg register
#define vd void
typedef long long ll;
il int gi(){
    rg int x=0,f=1;rg char ch=getchar();
    while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
char str[233333];
set<pair<long long,unsigned> >st;
int main(){
// 	freopen("3370.in","r",stdin);
// 	freopen("3370.out","w",stdout);
    int n=gi(),ans=0,len;
    long long A,bs;unsigned B,BS;
    while(n--){
        scanf("%s",str+1),len=strlen(str+1);
        A=0,B=0;bs=BS=1;
        for(rg int i=1;i<=len;++i){
            A=(A+bs*str[i])%998244353;
            B+=BS*str[i];
            bs=bs*19260817%998244353;
            BS*=2333;
        }
        #ifdef xzz
        printf("%s %lld %u\n",str+1,A,B);
        #endif
        if(st.find(make_pair(A,B))==st.end())++ans,st.insert(make_pair(A,B));
    }
    printf("%d\n",ans);
    return 0;
}

单源最短路

似乎是常数较小的spfa。dij懒得写了

// It is made by XZZ
// Fei Fan Ya Xi Lie~~~
#include<cstdio>
#include<algorithm>
#include<set>
#include<cstring>
using namespace std;
#define il inline
#define rg register
#define vd void
typedef long long ll;
il int gi(){
    rg int x=0,f=1;rg char ch=getchar();
    while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
const int maxn=10001,maxm=500001;
int fir[maxn],dis[maxm],nxt[maxm],w[maxm],id;
il vd add(const int&a,const int&b,const int&c){nxt[++id]=fir[a],fir[a]=id,dis[id]=b,w[id]=c;}
il vd SPFA(long long*dist,int S){
    static int que[maxn],hd,tl,x;
    static bool inque[maxn]={0};
    hd=tl=0;que[tl++]=S;inque[S]=1;
    dist[S]=0;
    while(hd^tl){
        x=que[hd];
        for(rg int i=fir[x];i;i=nxt[i])
            if(dist[dis[i]]>dist[x]+w[i]){
                dist[dis[i]]=dist[x]+w[i];
                if(!inque[dis[i]]){
                    inque[dis[i]]=1;
                    que[tl++]=dis[i];
                    if(tl==maxn)tl=0;
                }
            }
        ++hd;if(hd==maxn)hd=0;
        inque[x]=0;
    }
}
int main(){
// 	freopen("3371.in","r",stdin);
// 	freopen("3371.out","w",stdout);
    int n=gi(),m=gi(),s=gi(),x,y,z;
    while(m--)x=gi(),y=gi(),z=gi(),add(x,y,z);
    static long long dist[maxn];
    for(rg int i=1;i<=n;++i)dist[i]=1e15;
    SPFA(dist,s);
    for(rg int i=1;i<=n;++i){
        if(dist[i]==1e15)dist[i]=2147483647;
        printf("%lld ",dist[i]);
    }
    return 0;
}

缩点+DP

tarjan大家都会了吧。vector存新图

// It is made by XZZ
// Fei Fan Ya Xi Lie~~~
#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
#define il inline
#define rg register
#define vd void
typedef long long ll;
il int gi(){
    rg int x=0,f=1;rg char ch=getchar();
    while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
const int maxn=10001,maxm=100001;
int fir[maxn],dis[maxm],nxt[maxm],id;
il vd add(const int&a,const int&b){nxt[++id]=fir[a],fir[a]=id,dis[id]=b;}
int w[maxn],W[maxn],fr[maxn],top;
int dfn[maxn],low[maxn];
set<int>T[maxn];
int stk[maxn];bool instk[maxn];
il vd tarjan(const int&x){
    dfn[x]=low[x]=++dfn[0];
    stk[++top]=x;instk[x]=1;
    for(int i=fir[x];i;i=nxt[i])
        if(!dfn[dis[i]])tarjan(dis[i]),low[x]=min(low[x],low[dis[i]]);
        else if(instk[dis[i]])low[x]=min(low[x],dfn[dis[i]]);
    if(low[x]==dfn[x]){
        do{
            instk[stk[top]]=0;
            fr[stk[top]]=fr[0];
            W[fr[0]]+=w[stk[top]];
            --top;
        }while(stk[top+1]^x);
        ++fr[0];
    }
}
ll f[maxn];
il ll dp(int x){
    if(f[x])return f[x];
    f[x]=0;
    for(set<int>::iterator it=T[x].begin();it!=T[x].end();++it)
        f[x]=max(f[x],dp(*it));
    f[x]+=W[x];
    return f[x];
}
int main(){
// 	freopen("3387.in","r",stdin);
// 	freopen("3387.out","w",stdout);
    int n=gi()+1,m=gi(),u,v;
    for(rg int i=1;i<n;++i)w[i]=gi();
    while(m--)u=gi(),v=gi(),add(u,v);
    fr[0]=1;
    for(rg int i=1;i<n;++i)if(!dfn[i])tarjan(i);
    for(rg int i=1;i<n;++i)
        for(rg int j=fir[i];j;j=nxt[j])
            if(fr[i]^fr[dis[j]])T[fr[dis[j]]].insert(fr[i]);
    ll ans=0;
    for(rg int i=1;i<fr[0];++i)ans=max(ans,dp(i));
    printf("%lld\n",ans);
    return 0;
}

ST表

SB错误犯几次,我还说这是我打的最熟的东西。QwQ
SB错误:预处理log时没想清

    for(rg int i=2;i<n;++i)
        if(1<<log[i-1]+1==i)log[i]=log[i-1]+1;
        else log[i]=log[i-1];

代码:

// It is made by XZZ
// Fei Fan Ya Xi Lie~~~
#include<cstdio>
#include<algorithm>
using namespace std;
#define il inline
#define rg register
#define vd void
typedef long long ll;
il int gi(){
    rg int x=0,f=1;rg char ch=getchar();
    while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int st[17][100001];
int log[100001];
int main(){
// 	freopen("3865.in","r",stdin);
// 	freopen("3865.out","w",stdout);
    int n=gi()+1,m=gi(),Log;
    for(rg int i=2;i<n;++i)
        if(1<<log[i-1]+1==i)log[i]=log[i-1]+1;
        else log[i]=log[i-1];
    for(rg int i=1;i<n;++i)st[0][i]=gi();
    Log=log[n-1]+1;
    for(rg int i=1;i<Log;++i)
        for(rg int j=n-(1<<i)+1;j;--j)
            st[i][j]=max(st[i-1][j],st[i-1][j+(1<<(i-1))]);
    int l,r,lg;
    while(m--){
        l=gi(),r=gi(),lg=log[r-l+1];
        printf("%d\n",max(st[lg][l],st[lg][r-(1<<lg)+1]));
    }
    return 0;
}

LCA

这个不1A我真是SB了。

// It is made by XZZ
// Fei Fan Ya Xi Lie~~~
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define il inline
#define rg register
#define vd void
typedef long long ll;
il int gi(){
    rg int x=0,f=1;rg char ch=getchar();
    while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int st[19][500001],_log,n,m,rt;
const int maxn=500001,maxm=1000001;
int fir[maxn],dis[maxm],nxt[maxm],dep[maxn],id;
il vd add(const int&a,const int&b){nxt[++id]=fir[a],fir[a]=id,dis[id]=b;}
il vd dfs(const int&x){
    for(rg int i=fir[x];i;i=nxt[i])
        if(dis[i]^st[0][x])st[0][dis[i]]=x,dep[dis[i]]=dep[x]+1,dfs(dis[i]);
}
il int LCA(int a,int b){
    if(dep[a]<dep[b])swap(a,b);
    for(rg int i=_log;~i;--i)if(dep[st[i][a]]>dep[b])a=st[i][a];
    if(dep[a]^dep[b])a=st[0][a];
    for(rg int i=_log;~i;--i)if(st[i][a]^st[i][b])a=st[i][a],b=st[i][b];
    if(a^b)a=st[0][a];
    return a;
}
int main(){
// 	freopen("lca.in","r",stdin);
// 	freopen("lca.out","w",stdout);
    n=gi(),m=gi(),rt=gi();
    int u,v;
    for(rg int i=1;i<n;++i)u=gi(),v=gi(),add(u,v),add(v,u);
    _log=log2(n);
    dfs(rt);
    for(rg int i=1;i<=_log;++i)
        for(rg int j=1;j<=n;++j)
            st[i][j]=st[i-1][st[i-1][j]];
    while(m--)printf("%d\n",LCA(gi(),gi()));
    return 0;
}

线性筛

才发现不会,来学一下。
大概是每个合数用最大的那个因子筛掉。
要加if(i%prime[j]==0)break剪枝。
总复习再看一轮吧(肯定理解不了辣 背背板子吧QwQ)

// It is made by XZZ
// Fei Fan Ya Xi Lie~~~
#include<cstdio>
#include<algorithm>
using namespace std;
#define il inline
#define rg register
#define vd void
typedef long long ll;
il int gi(){
    rg int x=0,f=1;rg char ch=getchar();
    while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
bool yes[10000001];
int prime[2333333];
const char out[][233]={"Yes","No"};
int main(){
// 	freopen("3383.in","r",stdin);
// 	freopen("3383.out","w",stdout);
    int n=gi()+1,m=gi();
    yes[0]=yes[1]=1;
    prime[0]=1;
    for(rg int i=2;i<n;++i){
        if(yes[i]==0)prime[prime[0]++]=i;
        for(rg int j=1;j<prime[0]&&i*prime[j]<n;++j){
            yes[i*prime[j]]=1;
            if(i/prime[j]*prime[j]==i)break;
        }
    }
    while(m--)puts(out[yes[gi()]]);
    return 0;
}

割点

洛谷数据有bug,用的城市的备用交换机

// It is made by XZZ
// Fei Fan Ya Xi Lie~~~
#include<cstdio>
#include<algorithm>
using namespace std;
#define il inline
#define rg register
#define vd void
typedef long long ll;
il int gi(){
	rg int x=0,f=1;rg char ch=getchar();
	while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	return x*f;
}
const int maxn=100010,maxm=maxn<<1;
int fir[maxn],nxt[maxm],dis[maxm],id;
il vd add(const int&a,const int&b){nxt[++id]=fir[a],fir[a]=id,dis[id]=b;}
bool yes[maxn];
int dfn[maxn],low[maxn];
int cnt=0;
il vd tarjan(const int&x){
	dfn[x]=low[x]=++dfn[0];
	for(int i=fir[x];i;i=nxt[i])
		if(!dfn[dis[i]]){
			tarjan(dis[i]),low[x]=min(low[x],low[dis[i]]);
			if(low[dis[i]]>=dfn[x]){
				if(x==1)++cnt;
				else yes[x]=1;
			}
		}
		else low[x]=min(low[x],dfn[dis[i]]);
	if(x==1)yes[x]=cnt>1;
}
int main(){
// 	freopen("3388.in","r",stdin);
// 	freopen("3388.out","w",stdout);
	int n=gi(),u,v;
	while(scanf("%d%d",&u,&v)==2)add(u,v),add(v,u);
	tarjan(1);
	int tot=0;
	for(rg int i=1;i<=n;++i)if(yes[i])++tot;
	printf("%d\n",tot);
	for(rg int i=1;i<=n;++i)if(yes[i])printf("%d\n",i);
	return 0;
}
posted @ 2017-11-07 23:01  菜狗xzz  阅读(248)  评论(0编辑  收藏  举报