【打板进行中】
1.快速排序
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<ctime>
const int maxn=100005;
using namespace std;
int n,a[maxn];
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
void qsort(int *s,int l,int r) {
int bs=s[rand()%(r-l+1)+l];
int i=l,j=r;
while(i<=j) {
while(s[j]>bs) j--;
while(s[i]<bs) i++;
if(i<=j) {
swap(s[i],s[j]);
i++; j--;
}
}
if(j>l) qsort(s,l,j);
if(i<r) qsort(s,i,r);
}
void work() {
qsort(a,1,n);
for(int i=1;i<n;i++) printf("%d ",a[i]);
printf("%d\n",a[n]);
}
void init() {
read(n);
for(int i=1;i<=n;i++) read(a[i]);
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
srand(time(0));
init();
work();
return 0;
}
2.归并排序
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<ctime>
const int maxn=100005;
using namespace std;
int n,a[maxn];
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int tp[maxn];
void msort(int *s,int l,int r) {
if(l>=r) return;
int mid=((l+r)>>1);
msort(s,l,mid); msort(s,mid+1,r);
int tl=l,i=l,j=mid+1;
while(tl<=r) {
if(i<=mid&&(j>r||s[i]<s[j])) { tp[tl++]=s[i]; i++;}
else {tp[tl++]=s[j]; j++;}
}
for(int i=l;i<=r;i++) s[i]=tp[i];
}
void work() {
msort(a,1,n);
for(int i=1;i<n;i++) printf("%d ",a[i]);
printf("%d\n",a[n]);
}
void init() {
read(n);
for(int i=1;i<=n;i++) read(a[i]);
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
srand(time(0));
init();
work();
return 0;
}
3.kmp
//Twenty雨欲予鱼愉
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<ctime>
#include<queue>
const int maxn=1000007;
using namespace std;
char a[maxn],b[maxn];
int nxt[maxn],ans[maxn],sza,szb;
void make_nxt(char b[],int nxt[]) {
for(int i=1,k=0;i<szb;i++) {
while(k&&b[i]!=b[k]) k=nxt[k-1];
if(b[i]==b[k]) k++;
nxt[i]=k;
}
}
void kmp(char a[],char b[],int nxt[]) {
make_nxt(b,nxt);
for(int i=0,k=0;i<sza;i++) {
while(k&&a[i]!=b[k]) k=nxt[k-1];
if(a[i]==b[k]) k++;
if(k==szb) {ans[++ans[0]]=i-szb+2;}
}
}
void work() {
kmp(a,b,nxt);
for(int i=1;i<=ans[0];i++)
printf("%d\n",ans[i]);
for(int i=0;i<szb-1;i++) printf("%d ",nxt[i]);
printf("%d\n",nxt[szb-1]);
}
void init() {
scanf("%s",a);
getchar();
scanf("%s",b);
sza=strlen(a);
szb=strlen(b);
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
4.SLF优化的spfa
//Twenty雨欲予鱼愉
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<ctime>
#include<queue>
const int maxn=10005;
const int maxm=500007;
using namespace std;
int dis[maxn],n,m,s;
template<typename T>void read(T &x) {
char ch=getchar(); x=0; T f=1;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') ch=getchar(),f=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int ecnt,fir[maxn],nxt[maxm],to[maxm],val[maxm];
void add(int u,int v,int w) {
nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
}
int que[maxm+5],ql=1,qr=0,vis[maxn];
void spfa(int s) {
for(int i=1;i<=n;i++) dis[i]=2147483647;
memset(vis,0,sizeof(vis));
dis[s]=0;
vis[s]=1;
que[++qr]=s;
while(ql<=qr) {
int x=que[(ql+maxm)%maxm];
ql++;
vis[x]=0;
for(int i=fir[x];i;i=nxt[i]) {
if(dis[to[i]]>dis[x]+val[i]) {
dis[to[i]]=dis[x]+val[i];
if(!vis[to[i]]) {
vis[to[i]]=1;
if(ql<=qr&&(dis[que[(ql+maxm)%maxm]]>dis[to[i]])) {
ql--;
que[(ql+maxm)%maxm]=to[i];
}
else {
qr++;
que[(qr+maxm)%maxm]=to[i];
}
}
}
}
}
}
void work() {
spfa(s);
for(int i=1;i<=n;i++)
printf("%d ",dis[i]);
printf("\n");
}
void init() {
read(n);
read(m);
read(s);
for(int i=1;i<=m;i++) {
int x,y,z;
read(x); read(y); read(z);
add(x,y,z);
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
5.堆优化的dijkstra
//Twenty雨欲予鱼愉
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<ctime>
#include<queue>
const int maxn=10005;
const int maxm=500007;
using namespace std;
int dis[maxn],n,m,s;
template<typename T>void read(T &x) {
char ch=getchar(); x=0; T f=1;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') ch=getchar(),f=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int ecnt,fir[maxn],nxt[maxm],to[maxm],val[maxm];
void add(int u,int v,int w) {
nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
}
struct node {
int x,dis;
node(){}
node(int x,int dis):x(x),dis(dis){}
friend bool operator <(const node&A,const node&B) {
return A.dis>B.dis;
}
};
priority_queue<node>que;
int vis[maxn];
void spfa(int s) {
for(int i=1;i<=n;i++) dis[i]=2147483647;
memset(vis,0,sizeof(vis));
dis[s]=0;
que.push(node(s,0));
while(!que.empty()) {
node tp=que.top();
int x=tp.x;
que.pop();
if(vis[x]) continue;
vis[x]=1;
for(int i=fir[x];i;i=nxt[i])
if(!vis[to[i]]&&dis[to[i]]>dis[x]+val[i]) {
dis[to[i]]=dis[x]+val[i];
que.push(node(to[i],dis[to[i]]));
}
}
}
void work() {
spfa(s);
for(int i=1;i<=n;i++)
printf("%d ",dis[i]);
printf("\n");
}
void init() {
read(n);
read(m);
read(s);
for(int i=1;i<=m;i++) {
int x,y,z;
read(x); read(y); read(z);
add(x,y,z);
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
6.左偏树
//Twenty雨欲予鱼愉
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<ctime>
#include<queue>
const int maxn=100000+299;
using namespace std;
int a[maxn],n,m,bo[maxn],fa[maxn],dis[maxn],ch[maxn][2];
template<typename T>void read(T &x) {
char ch=getchar(); x=0; T f=1;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') ch=getchar(),f=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);}
#define lc ch[x][0]
#define rc ch[x][1]
int merge(int x,int y) {
if(!(x*y)) return x^y;
if(a[x]>a[y]) swap(x,y);
fa[y]=x;
rc=merge(rc,y);
if(dis[rc]>dis[lc]) swap(lc,rc);
if(!rc) dis[x]=0;
else dis[x]=dis[rc]+1;
return x;
}
void work() {
for(int i=1;i<=m;i++) {
int o,x,y;
read(o);
if(o==1) {
read(x);
read(y);
if(!bo[x]&&!bo[y]&&(find(x)!=find(y)))
x=merge(find(x),find(y));
}
else {
read(x);
if(bo[x]) {
printf("-1\n");
continue;
}
x=find(x);
printf("%d\n",a[x]);
fa[lc]=lc; fa[rc]=rc;
lc=merge(lc,rc);
if(!lc) fa[x]=rc;
else fa[x]=lc;
lc=rc=0;
bo[x]=1;
}
}
}
void init() {
read(n);
read(m);
for(int i=1;i<=n;i++) {
read(a[i]);
fa[i]=i;
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
7.线性筛素数
//Twenty
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<ctime>
#include<queue>
using namespace std;
const int maxn=10000007;
int n,m,p[maxn];
bool bo[maxn];
template<typename T>void read(T &x) {
char ch=getchar(); x=0; T f=1;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') ch=getchar(),f=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
void get_prime(int n) {
bo[1]=1;
for(int i=2;i<=n;i++) {
if(!bo[i]) p[++p[0]]=i;
for(int j=1;j<=p[0]&&p[j]*i<=n;j++) {
bo[p[j]*i]=1;
if(i%p[j]==0) break;
}
}
}
void init() {
read(n);
get_prime(n);
read(m);
for(int i=1;i<=m;i++) {
int x; read(x);
if(bo[x]) printf("No\n");
else printf("Yes\n");
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
return 0;
}
8.二分图最大匹配 匈牙利
//Twenty雨欲予鱼愉
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<ctime>
#include<queue>
const int maxn=2005;
using namespace std;
int n,m,e,pr[maxn],vis[maxn],ans;
template<typename T>void read(T &x) {
char ch=getchar(); x=0; T f=1;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') ch=getchar(),f=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int ecnt,fir[maxn],nxt[maxn*maxn],to[maxn*maxn];
void add(int u,int v) {
nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v;
}
int find(int x) {
for(int i=fir[x];i;i=nxt[i]) if(!vis[to[i]]){
int y=to[i];
vis[y]=1;
if(!pr[y]||find(pr[y])) {
pr[y]=x;
return 1;
}
}
return 0;
}
void work() {
for(int i=1;i<=n;i++) {
memset(vis,0,sizeof(vis));
ans+=find(i);
}
printf("%d\n",ans);
}
void init() {
read(n);
read(m);
read(e);
for(int i=1;i<=e;i++) {
int x,y;
read(x);
read(y);
if(x<=n&&y<=m)
add(x,y);
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
9.二分图匹配 km算法
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<ctime>
#define INF 0xfffffff
const int maxn=305;
typedef long long LL;
using namespace std;
int n,cx[maxn],cy[maxn],x[maxn],y[maxn],pr[maxn],slack[maxn];
int a[maxn][maxn];
int find(int u) {
x[u]=1;
for(int i=1;i<=n;i++) if(!y[i]){
if(cx[u]+cy[i]==a[u][i]) {
y[i]=1;
if(pr[i]==-1||find(pr[i])) {
pr[i]=u;
return 1;
}
}
else slack[i]=min(slack[i],-a[u][i]+cx[u]+cy[i]);
}
return 0;
}
void work() {
memset(pr,-1,sizeof(pr));
memset(cy,0,sizeof(cy));
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) slack[j]=INF;
for(;;) {
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
if(find(i)) break;
int d=INF;
for(int j=1;j<=n;j++)
if(!y[j]&&d>slack[j])
d=slack[j];
if(d==INF) return;
for(int j=1;j<=n;j++) {
if(x[j]) cx[j]-=d;
if(y[j]) cy[j]+=d;
else slack[j]-=d;
}
}
}
int ans=0;
for(int i=1;i<=n;i++)
ans+=a[pr[i]][i];
printf("%d\n",ans);
}
void init() {
while(~scanf("%d",&n)) {
for(int i=1;i<=n;i++) {
cx[i]=-INF;
for(int j=1;j<=n;j++) {
scanf("%d",&a[i][j]);
cx[i]=max(cx[i],a[i][j]);
}
}
work();
}
}
int main() {
init();
return 0;
}
10.线性基
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<ctime>
const int maxn=101;
typedef long long LL;
using namespace std;
int n;
LL a[maxn],b[maxn];
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
void work() {
for(int i=1;i<=n;i++)
for(int j=60;j>=0;j--)
if((1LL<<j)&a[i]) {
if(b[j]) a[i]^=b[j];
else { b[j]=a[i]; break;}
}
LL ans=0;
for(int i=60;i>=0;i--)
if((ans^b[i])>ans) ans^=b[i];
printf("%lld\n",ans);
}
void init() {
read(n);
for(int i=1;i<=n;i++) read(a[i]);
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
11.高斯消元
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
const int maxn=105;
typedef long long LL;
using namespace std;
int n;
double a[maxn][maxn];
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int gauss(double a[][105],int n) {
for(int i=1;i<=n;i++) {
int now=i;
for(int j=i+1;j<=n;j++)
if(a[j][i]>a[now][i]) now=j;
if(now!=i)
for(int j=i;j<=n+1;j++)
swap(a[i][j],a[now][j]);
if(a[now][i]==0) return 0;
for(int j=i+1;j<=n+1;j++)
a[i][j]/=a[i][i];
a[i][i]=1;
for(int j=i+1;j<=n;j++) {
for(int k=i+1;k<=n+1;k++)
a[j][k]-=a[i][k]*a[j][i];
a[j][i]=0;
}
}
for(int i=n-1;i>=1;i--)
for(int j=i+1;j<=n;j++)
a[i][n+1]-=a[i][j]*a[j][n+1];
return 1;
}
void work()
{
if(!gauss(a,n)) printf("No Solution\n");
else
for(int i=1;i<=n;i++)
printf("%.2lf\n",a[i][n+1]);
}
void init() {
read(n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n+1;j++)
read(a[i][j]);
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
12.tarjan割点
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
const int maxn=100005;
typedef long long LL;
using namespace std;
int n,m,ans[maxn];
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int ecnt,fir[maxn],nxt[maxn<<1],to[maxn<<1];
void add(int u,int v) {
nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v;
nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u;
}
int rc,dfs_clock,dfn[maxn],low[maxn],cut[maxn];
void tarjan(int x,int rt) {
dfn[x]=low[x]=++dfs_clock;
for(int i=fir[x];i;i=nxt[i]) {
if(!dfn[to[i]]) {
if(x==rt) rc++;
tarjan(to[i],rt);
low[x]=min(low[x],low[to[i]]);
if(x!=rt&&low[to[i]]>=dfn[x]) cut[x]=1;
}
else low[x]=min(low[x],dfn[to[i]]);
}
if(x==rt&&rc>=2) cut[x]=1;
}
void work() {
for(int i=1;i<=n;i++)
if(!dfn[i]) {rc=0;tarjan(i,i);}
for(int i=1;i<=n;i++)
if(cut[i])
ans[++ans[0]]=i;
printf("%d\n",ans[0]);
for(int i=1;i<ans[0];i++) printf("%d ",ans[i]);
if(ans[0]) printf("%d\n",ans[ans[0]]);
}
void init() {
read(n);
read(m);
for(int i=1;i<=m;i++) {
int x,y;
read(x); read(y);
add(x,y);
}
}
int main() {
init();
work();
return 0;
}
13.tarjan求桥
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
typedef long long LL;
using namespace std;
const int maxn=10000+299;
const int maxm=100000*2+299;
int T,n,m,x,y,fir[maxn],nxt[maxm],to[maxm],fa[maxn],cc,cut[maxm],dfn[maxn],id[maxm],low[maxn],dfs_clock,ecnt;
void add(int x,int y,int tot) {
nxt[++ecnt]=fir[x]; fir[x]=ecnt; to[ecnt]=y; id[ecnt]=tot;
nxt[++ecnt]=fir[y]; fir[y]=ecnt; to[ecnt]=x; id[ecnt]=tot;
}
void clear() {
memset(fir,0,sizeof(fir));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(cut,0,sizeof(cut));
memset(fa,0,sizeof(fa)); ecnt=0;
dfs_clock=0;
}
void tarjan(int x) {
dfn[x]=low[x]=++dfs_clock;
for(int i=fir[x];i;i=nxt[i]) {
if(!dfn[to[i]]) {
fa[to[i]]=i;
tarjan(to[i]);
low[x]=min(low[x],low[to[i]]);
}
else if(id[fa[x]]!=id[i]) low[x]=min(low[x],dfn[to[i]]);
}
if(fa[x]&&dfn[x]==low[x]) {
cc++;
cut[id[fa[x]]]=1;
}
}
int main()
{
scanf("%d",&T);
while(T) {
clear();
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) {
scanf("%d%d",&x,&y);
add(x,y,i);
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
printf("%d\n",cc);
for(int i=1;i<=m;i++)
if(cut[i]) { cc--; if(cc) printf("%d ",i); else printf("%d\n",i);}
--T;
if(T) printf("\n");
}
return 0;
}
14.网络最大流
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
#define inf 0xfffffff
const int N=10007;
const int M=100007;
typedef long long LL;
using namespace std;
int n,m,s,t,d[N],c[N],ecnt=1;
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
struct edge {
int from,to,cap,flow,nxt;
edge(){}
edge(int from,int to,int cap,int flow,int nxt):from(from),to(to),cap(cap),flow(flow),nxt(nxt){}
}e[M<<1];
int fir[N],cur[N];
void add(int u,int v,int w) {
e[++ecnt]=edge(u,v,w,0,fir[u]);
e[++ecnt]=edge(v,u,0,0,fir[v]);
fir[u]=ecnt-1; fir[v]=ecnt;
}
queue<int>que;
void bfs() {
for(int i=1;i<=n;i++) d[i]=n;
d[t]=0;
que.push(t);
while(!que.empty()) {
int x=que.front();
que.pop();
for(int i=fir[x];i;i=e[i].nxt) {
int y=e[i].to;
if(d[y]==n&&e[i].cap==e[i].flow) {
d[y]=d[x]+1;
que.push(y);
}
}
}
}
int p[N];
int cal() {
int fl=inf;
for(int x=t;x!=s;x=e[p[x]].from)
fl=min(fl,e[p[x]].cap-e[p[x]].flow);
for(int x=t;x!=s;x=e[p[x]].from) {
e[p[x]].flow+=fl;
e[p[x]^1].flow-=fl;
}
return fl;
}
int max_flow() {
bfs();
for(int i=1;i<=n;i++) cur[i]=fir[i],c[d[i]]++;
int res=0;
for(int x=s;d[x]<n;) {
if(x==t) {
res+=cal();
x=s;
}
int ok=0;
for(int &i=cur[x];i;i=e[i].nxt) {
int y=e[i].to;
if(d[y]+1==d[x]&&e[i].cap>e[i].flow) {
ok=1;
p[y]=i;
x=y;
break;
}
}
if(!ok) {
cur[x]=fir[x];
int M=n;
for(int i=cur[x];i;i=e[i].nxt) {
int y=e[i].to;
if(e[i].flow<e[i].cap&&d[y]+1<M) M=d[y]+1;
}
if(!(--c[d[x]])) break;
c[d[x]=M]++;
if(x!=s) x=e[p[x]].from;
}
}
return res;
}
void work() {
printf("%d\n",max_flow());
}
void init() {
read(n);
read(m);
read(s);
read(t);
for(int i=1;i<=m;i++) {
int u,v,w;
read(u);
read(v);
read(w);
add(u,v,w);
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
15.最小费用最大流
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
#define inf 0xfffffff
const int N=5007;
const int M=50007;
typedef long long LL;
using namespace std;
int n,m,s,t,ecnt=1;
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
struct edge {
int from,to,cap,flow,cost,nxt;
edge(){}
edge(int from,int to,int cap,int flow,int cost,int nxt):from(from),to(to),cap(cap),flow(flow),cost(cost),nxt(nxt){}
}e[M<<1];
int fir[N],cur[N];
void add(int u,int v,int w,int c) {
e[++ecnt]=edge(u,v,w,0,c,fir[u]);
e[++ecnt]=edge(v,u,0,0,-c,fir[v]);
fir[u]=ecnt-1; fir[v]=ecnt;
}
int p[N];
int cal() {
int fl=inf;
for(int x=t;x!=s;x=e[p[x]].from)
fl=min(fl,e[p[x]].cap-e[p[x]].flow);
for(int x=t;x!=s;x=e[p[x]].from) {
e[p[x]].flow+=fl;
e[p[x]^1].flow-=fl;
}
return fl;
}
queue<int>que;
int dis[N],vis[N];
int spfa() {
for(int i=1;i<=n;i++) dis[i]=inf;
dis[s]=0; vis[s]=1; que.push(s);
while(!que.empty()) {
int x=que.front();
que.pop(); vis[x]=0;
for(int i=fir[x];i;i=e[i].nxt) {
int y=e[i].to;
if(e[i].cap>e[i].flow&&dis[y]>dis[x]+e[i].cost) {
p[y]=i;
dis[y]=dis[x]+e[i].cost;
if(!vis[y]) {
vis[y]=1;
que.push(y);
}
}
}
}
return (dis[t]!=inf);
}
int ans2=0;
int max_flow() {
int res=0;
while(spfa()) {
int tp=cal();
res+=tp;
ans2+=tp*dis[t];
}
return res;
}
void work() {
printf("%d ",max_flow());
printf("%d\n",ans2);
}
void init() {
read(n);
read(m);
read(s);
read(t);
for(int i=1;i<=m;i++) {
int u,v,w,c;
read(u);
read(v);
read(w);
read(c);
add(u,v,w,c);
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
16.倍增求lca
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
const int maxn=500007;
typedef long long LL;
using namespace std;
int n,m,rt;
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int ecnt,fir[maxn],nxt[maxn<<1],to[maxn<<1];
void add(int u,int v) {
nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v;
nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u;
}
int f[maxn][20],R[maxn];
void dfs(int x,int fa) {
f[x][0]=fa; R[x]=R[fa]+1;
for(int i=1;i<=19;i++)
f[x][i]=f[f[x][i-1]][i-1];
for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
dfs(to[i],x);
}
}
int get_lca(int x,int y) {
if(R[x]<R[y]) swap(x,y);
for(int i=19;i>=0;i--)
if(f[x][i]&&R[f[x][i]]>=R[y])
x=f[x][i];
if(x==y) return x;
for(int i=19;i>=0;i--)
if(f[x][i]&&f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return f[x][0];
}
void work() {
dfs(rt,0);
for(int i=1;i<=m;i++) {
int x,y;
read(x); read(y);
printf("%d\n",get_lca(x,y));
}
}
void init() {
read(n);
read(m);
read(rt);
for(int i=1;i<n;i++) {
int u,v;
read(u); read(v);
add(u,v);
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
17.tarjan求lca
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
const int maxn=500007;
typedef long long LL;
using namespace std;
int n,m,rt;
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int ecnt,fir[maxn],nxt[maxn<<1],to[maxn<<1];
void add(int u,int v) {
nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v;
nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u;
}
int cnt,fi[maxn],nx[maxn<<1],tt[maxn<<1],id[maxn<<1],ans[maxn];
void add_ask(int u,int v,int i) {
nx[++cnt]=fi[u]; fi[u]=cnt; tt[cnt]=v; id[cnt]=i;
nx[++cnt]=fi[v]; fi[v]=cnt; tt[cnt]=u; id[cnt]=i;
}
int fa[maxn],vis[maxn];
int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]);}
void dfs(int x,int f) {
for(int i=fi[x];i;i=nx[i])
if(vis[tt[i]])
ans[id[i]]=find(tt[i]);
vis[x]=1;
for(int i=fir[x];i;i=nxt[i]) if(to[i]!=f)
dfs(to[i],x);
fa[x]=f;
}
void work() {
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++) {
int x,y;
read(x); read(y);
add_ask(x,y,i);
}
dfs(rt,0);
for(int i=1;i<=m;i++)
printf("%d\n",ans[i]);
}
void init() {
read(n);
read(m);
read(rt);
for(int i=1;i<n;i++) {
int u,v;
read(u); read(v);
add(u,v);
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
18.树剖求lca
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
const int maxn=500007;
typedef long long LL;
using namespace std;
int n,m,rt;
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int ecnt,fir[maxn],nxt[maxn<<1],to[maxn<<1];
void add(int u,int v) {
nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v;
nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u;
}
int R[maxn],sz[maxn],top[maxn],fa[maxn];
void dfs(int x,int f) {
R[x]=R[f]+1;
sz[x]=1; fa[x]=f;
for(int i=fir[x];i;i=nxt[i]) if(to[i]!=f) {
dfs(to[i],x);
sz[x]+=sz[to[i]];
}
}
void DFS(int x,int tt) {
top[x]=tt;
int mson=0;
for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa[x]&&sz[to[i]]>sz[mson]){
mson=to[i];
}
if(!mson) return;
DFS(mson,tt);
for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa[x]&&to[i]!=mson){
DFS(to[i],to[i]);
}
}
int get_lca(int x,int y) {
while(top[x]!=top[y]) {
if(R[top[x]]<R[top[y]]) swap(x,y);
x=fa[top[x]];
}
if(R[x]>R[y]) swap(x,y);
return x;
}
void work() {
dfs(rt,0);
DFS(rt,rt);
for(int i=1;i<=m;i++) {
int x,y;
read(x); read(y);
printf("%d\n",get_lca(x,y));
}
}
void init() {
read(n);
read(m);
read(rt);
for(int i=1;i<n;i++) {
int u,v;
read(u); read(v);
add(u,v);
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
work();
return 0;
}
19.manacher
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
const int maxn=11000007;
typedef long long LL;
using namespace std;
int tot,ans,rad[maxn<<1];
char a[maxn<<1];
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
void manacher() {
for(int i=0,j=0,k;i<tot;) {
while(a[i-j-1]==a[i+j+1]) j++;
rad[i]=j;
ans=max(ans,j);
for(k=1;k<=j&&rad[i-k]!=rad[i]-k;k++)
rad[i+k]=min(rad[i-k],rad[i]-k);
i+=k;
j=max(j-k,0);
}
}
void init() {
a[tot++]='&';
a[tot++]='#';
char ch=getchar();
while(ch<'a'||ch>'z') ch=getchar();
for(;ch>='a'&&ch<='z';ch=getchar()) {
a[tot++]=ch;
a[tot++]='#';
}
a[tot++]='*';
manacher();
printf("%d\n",ans);
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
return 0;
}
20.AC自动机
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
typedef long long LL;
using namespace std;
const int maxn=1e6+7;
int ch[maxn][26],w[maxn],fail[maxn],tot,rt,n,ans;
char a[maxn];
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
void insert() {
int now=rt;
for(int i=0;a[i]!='\0';i++) {
int c=a[i]-'a';
if(!ch[now][c]) ch[now][c]=++tot;
now=ch[now][c];
}
w[now]++;
}
queue<int>que;
void get_fail() {
que.push(rt);
while(!que.empty()) {
int x=que.front();
que.pop();
for(int i=0;i<26;i++) if(ch[x][i]){
int y=ch[x][i];
if(x==rt) fail[y]=rt;
else {
int tp=fail[x];
while(fail[tp]&&!ch[tp][i]) tp=fail[tp];
if(ch[tp][i]) fail[y]=ch[tp][i]; else fail[y]=rt;
}
que.push(y);
}
}
}
void qry() {
int now=rt;
for(int i=0;a[i]!='\0';i++) {
int c=a[i]-'a';
while(!ch[now][c]&&fail[now]) now=fail[now];
if(ch[now][c]) {
now=ch[now][c];
ans+=w[now];
w[now]=0;
int tp=fail[now];
while(tp&&w[tp]) {
ans+=w[tp];
w[tp]=0;
tp=fail[tp];
}
}
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
read(n);
for(int i=1;i<=n;i++) {
scanf("%s",a);
insert();
}
get_fail();
scanf("%s",a);
qry();
printf("%d\n",ans);
return 0;
}
21.lucas定理
//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
const int maxn=1e5+7;
typedef long long LL;
using namespace std;
int T,p;
LL n,m,power[maxn];
template<typename T> void read(T &x) {
char ch=getchar(); T f=1; x=0;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
LL ksm(LL a,LL b,int p) {
LL base=a,res=1;
while(b) {
if(b&1) (res*=base)%=p;
(base*=base)%=p;
b>>=1;
}
return res;
}
LL cal(LL n,LL m,int p) {
if(m>n) return 0;
return power[n]*ksm(power[m]*power[n-m]%p,p-2,p)%p;
}
LL lucas(LL n,LL m,int p) {
if(!m) return 1;
return cal(n%p,m%p,p)*lucas(n/p,m/p,p)%p;
}
void init() {
read(T);
while(T--) {
read(n);
read(m);
read(p);
power[0]=1;
for(int i=1;i<=p;i++) power[i]=power[i-1]*i%p;
printf("%lld\n",lucas(n+m,m,p));
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
init();
return 0;
}