【CSP-S 2019模板复习 1】
虽然很大可能要退役了
但还是把复习的模板汇总一个吧
虽然显然有些根本不可能考
割点割边
注意对的特判
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline int readstring(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch))s[++top]=ch,ch=gc();
return top;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=100005;
vector<int> e[N],ans;
pii anc[N];
int n,m,low[N],dfn[N],cut[N],tot,tim;
void tarjan(int u,int fa){
low[u]=dfn[u]=++tim;
int cnt=0;
for(int &v:e[u]){
if(v==fa)continue;
if(!dfn[v]){
tarjan(v,u),cnt++;
chemn(low[u],low[v]);
if(low[v]>dfn[u])anc[++tot]=pii(min(u,v),max(u,v));
if(u!=1&&low[v]>=dfn[u])cut[u]=1;
}
else chemn(low[u],dfn[v]);
}
if(u==1&&cnt>=2)
cut[u]=1;
}
int main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
n=read(),m=read();
for(int i=1;i<=m;i++){
int u=read(),v=read();
e[u].pb(v),e[v].pb(u);
}
tarjan(1,0);
for(int i=1;i<=n;i++)if(cut[i])ans.pb(i);
if(!ans.size())cout<<"Null";
else for(int &x:ans)cout<<x<<" ";
puts("");
sort(anc+1,anc+tot+1);
for(int i=1;i<=tot;i++)cout<<anc[i].fi<<" "<<anc[i].se<<'\n';
}
边双
void tarjan(int u,int fa){
low[u]=dfn[u]=++tim;
stk[++top]=u;
for(int &v:e[u]){
if(v==fa)continue;
if(dfn[v])chemn(low[u],dfn[v]);
else {
tarjan(v,u),chemn(low[u],low[v]);
}
}
if(low[u]>=dfn[u]){
cnt++,mn[cnt]=1e9;
int tmp;
do{
tmp=stk[top--];
bel[tmp]=cnt,chemn(mn[cnt],tmp);
}while(tmp!=u);
}
}
点双
一个点有可能属于多个点双
比如一个形的图
void dfs(int u,int fa){
dfn[u]=low[u]=++tim;
stk[++top]=u;
for(int e=adj[u];e;e=nxt[e]){
int v=to[e];
if(v==fa)continue;
if(!dfn[v]){
dfs(v,u),chemn(low[u],low[v]);
}
else {chemn(low[u],dfn[v]);continue;}
if(low[v]>=dfn[u]){
int tmp;
belnum++;
do{
tmp=stk[top];
cir[belnum].pb(tmp);
num[belnum]++;
top--;
}while(tmp!=v);
cir[belnum].pb(u);
num[belnum]++;
}
}
}
强联通分量
注意要记
void tarjan(int u){
low[u]=dfn[u]=++tim;
stk[++top]=u,vis[u]=1;
for(int &v:e[u]){
if(!dfn[v]){
tarjan(v);
chemn(low[u],low[v]);
}
else if(vis[v])chemn(low[u],dfn[v]);
}
if(low[u]>=dfn[u]){
cnt++;
int tmp;
do{
tmp=stk[top--];
bel[tmp]=cnt;
vis[tmp]=0;num[cnt]++;
}while(tmp!=u);
}
}
注意空间要开到以上
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
int n,l;
cs int N=22000005;
char s[N],t[N];
int len[N];
int main(){
#ifdef Stargazer
freopen("lc.in","r",stdin);
#endif
scanf("%s",t+1);
n=strlen(t+1);
s[0]='!',s[l=1]='&';
for(int i=1;i<=n;i++)s[++l]=t[i],s[++l]='&';
s[l+1]='@';
for(int i=1,mxpos=0,r=0;i<=l;i++){
// cout<<s[i];
if(i<=r)len[i]=min(r-i+1,len[2*mxpos-i]);
while(s[i+len[i]]==s[i-len[i]])len[i]++;
if(i+len[i]-1>r)r=i+len[i]-1,mxpos=i;
}
int res=0;
for(int i=1;i<=l;i++)chemx(res,len[i]-1);//,cout<<len[i];puts("");
cout<<res<<'\n';;
}
匈牙利
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline int readstring(char *s){
int top=0;char ch=gc();
while(!isalpha(ch))ch=gc();
while(isalpha(ch))s[++top]=ch,ch=gc();
return top;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=1005;
int mat[N];
vector<int> e[N];
int n,m,E,vis[N];
bool dfs(int u){
if(vis[u])return false;
vis[u]=1;
for(int &v:e[u]){
if(!mat[v]||dfs(mat[v])){
mat[v]=u;return true;
}
}
return false;
}
int main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
n=read(),m=read(),E=read();
for(int i=1;i<=E;i++){
int u=read(),v=read();
if(u>n||v>m)continue;
e[u].pb(v);
}int ans=0;
for(int i=1;i<=n;i++)memset(vis,0,sizeof(vis)),ans+=dfs(i);
cout<<ans;
}
LCT
反正各种操作的时候能就多做一下防止有信息丢失
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline void readchar(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch))s[++top]=ch,ch=gc();
}
#define int long long
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=51061;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b;a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
cs int N=100005;
namespace Lct{
int ad[N],ml[N],val[N],son[N][2],fa[N],rev[N],s[N],siz[N];
#define lc(u) son[u][0]
#define rc(u) son[u][1]
inline void init(int u){s[u]=val[u]=ml[u]=1;}
inline void pushadd(int u,int k){
Add(ad[u],k),Add(val[u],k),Add(s[u],mul(siz[u],k));
}
inline void pushmul(int u,int k){
Mul(ml[u],k),Mul(ad[u],k),Mul(val[u],k),Mul(s[u],k);
}
inline void pushrev(int u){
swap(lc(u),rc(u)),rev[u]^=1;
}
inline void pushdown(int u){
if(rev[u]){
if(lc(u))pushrev(lc(u));
if(rc(u))pushrev(rc(u));
rev[u]=0;
}
if(ml[u]!=1){
if(lc(u))pushmul(lc(u),ml[u]);
if(rc(u))pushmul(rc(u),ml[u]);
ml[u]=1;
}
if(ad[u]){
if(lc(u))pushadd(lc(u),ad[u]);
if(rc(u))pushadd(rc(u),ad[u]);
ad[u]=0;
}
}
inline void pushup(int u){
siz[u]=siz[lc(u)]+siz[rc(u)]+1;
s[u]=val[u];
if(lc(u))Add(s[u],s[lc(u)]);
if(rc(u))Add(s[u],s[rc(u)]);
}
inline bool isrt(int u){
return !fa[u]||((rc(fa[u])!=u&&lc(fa[u])!=u));
}
inline bool isrc(int u){
return rc(fa[u])==u;
}
inline void rotate(int v){
int u=fa[v],z=fa[u];
int t=isrc(v);
if(!isrt(u))son[z][isrc(u)]=v;
fa[v]=z;
fa[son[v][t^1]]=u;
son[u][t]=son[v][t^1];
fa[u]=v,son[v][t^1]=u;
pushup(u),pushup(v);
}
int stk[N],top;
inline void splay(int u){
stk[top=1]=u;
for(int v=u;!isrt(v);v=fa[v])stk[++top]=fa[v];
for(int i=top;i;i--)pushdown(stk[i]);
while(!isrt(u)){
if(!isrt(fa[u]))
isrc(fa[u])==isrc(u)?rotate(fa[u]):rotate(u);
rotate(u);
}
}
inline void access(int u){
for(int v=0;u;v=u,u=fa[u]){
splay(u);
rc(u)=v;
pushup(u);
}
}
inline void makert(int u){
access(u),splay(u),pushrev(u);
}
inline void link(int u,int v){
makert(u),access(v),splay(v),fa[u]=v;
}
inline void cut(int u,int v){
makert(u),access(v),splay(v);
fa[lc(v)]=0,lc(v)=0,pushup(v);
}
inline void split(int u,int v){
// cerr<<"1\n";
makert(u);
// cerr<<"1\n";
access(v);
// cerr<<"1\n";
splay(v);
//cerr<<"1\n";
}
}
int n,q;
char op[5];
signed main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
n=read(),q=read();
for(int i=1;i<=n;i++)Lct::init(i);
for(int i=1;i<n;i++){
int u=read(),v=read();
Lct::link(u,v);
}
while(q--){
readchar(op);
int u=read(),v=read();
switch(op[1]){
case '+':{
int c=read();Lct::split(u,v),Lct::pushadd(v,c);
break;
}
case '-':{
Lct::cut(u,v),u=read(),v=read(),Lct::link(u,v);
break;
}
case '/':{
//cerr<<u<<" "<<v<<'\n';
Lct::split(u,v),cout<<Lct::s[v]<<'\n';
break;
}
default:{
// cerr<<u<<" "<<v<<'\n';
int c=read();Lct::split(u,v),Lct::pushmul(v,c);
break;
}
}
}
}
KMP
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline int readchar(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch))s[++top]=ch,ch=gc();
return top;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=1000005;
char s[N],t[N];
int nxt[N],n,m;
int main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
n=readchar(s);
m=readchar(t);
for(int i=0,j=2;j<=m;j++){
while(i&&t[i+1]!=t[j])i=nxt[i];
if(t[i+1]==t[j])i++;
nxt[j]=i;
}
for(int i=0,j=1;j<=n;j++){
while(i&&t[i+1]!=s[j])i=nxt[i];
if(t[i+1]==s[j])i++;
if(i==m)cout<<j-m+1<<'\n',i=nxt[i];
}
for(int i=1;i<=m;i++)cout<<nxt[i]<<" ";
}
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline int readchar(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch))s[++top]=ch,ch=gc();
return top;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=5005;
struct edge{
int v,cap,val,r;
edge(int a=0,int b=0,int c=0,int d=0):v(a),cap(b),val(c),r(d){}
};
int str,des,n,m;
namespace Flow{
vector<edge>e[N];
typedef vector<edge>::iterator It;
It tp[N];
int dis[N];
int vis[N];
int mncost,mxflow;
inline void addedge(int u,int v,int w,int c){
e[u].pb(edge(v,w,c,e[v].size()));
e[v].pb(edge(u,0,-c,e[u].size()-1));
}
int q[N*20],hd,tl;
inline bool spfa(){
memset(dis,127/3,sizeof(int)*(n+1));
q[hd=tl=1]=str,dis[str]=0;
while(hd<=tl){
int u=q[hd++];vis[u]=0;
for(edge &x:e[u]){
if(x.cap>0&&dis[x.v]>dis[u]+x.val){
dis[x.v]=dis[u]+x.val;
if(!vis[x.v])q[++tl]=x.v,vis[x.v]=1;
if(dis[q[tl]]<dis[q[hd]])swap(q[hd],q[tl]);
}
}
}
return dis[des]<dis[0];
}
inline int dfs(int u,int flow){
if(u==des)return flow;
int res=0;vis[u]=1;
for(It &it=tp[u];it!=e[u].end();it++){
if(it->cap>0&&dis[it->v]==dis[u]+it->val&&!vis[it->v]){
int now=dfs(it->v,min(it->cap,flow-res));
it->cap-=now,e[it->v][it->r].cap+=now,mncost+=now*it->val,res+=now;
if(res==flow)break;
}
}
vis[u]=0;return res;
}
inline void mcmf(){
while(spfa()){
for(int i=1;i<=n;i++)tp[i]=e[i].begin();
mxflow+=dfs(str,1e9);
}
}
inline void write(){
cout<<mxflow<<" "<<mncost<<'\n';
}
}
int main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
n=read(),m=read(),str=read(),des=read();
for(int i=1;i<=m;i++){
int u=read(),v=read(),w=read(),c=read();
Flow::addedge(u,v,w,c);
}
Flow::mcmf();
Flow::write();
}