C++ 模版整理

强连通 Tarjan

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #define ll long long
 5 struct node{
 6     ll next,v;
 7 }edge[1001];
 8 const int maxn=10010;
 9 ll n,m,a,b,last[1001],tot,cnt,low[maxn],dfn[maxn],index,stack[maxn];
10 bool v[maxn];
11 inline ll min(ll a,ll b)
12 {
13     return a<b?a:b;
14 }
15 inline void read(ll &k)
16 {
17     ll f=1;k=0;char c=getchar();
18     while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
19     while (c>='0'&&c<='9')k=k*10+c-'0',c=getchar();
20     k*=f;
21 }
22 void tarjan(ll now)
23 {
24     dfn[now]=low[now]=++tot;
25     stack[++index]=now;
26     v[now]=1;
27     for (int i=last[now];i;i=edge[i].next)
28     {
29         if (!dfn[edge[i].v])
30         {
31             tarjan(edge[i].v);
32             low[now]=min(low[now],low[edge[i].v]);
33         }
34         else if (v[edge[i].v]) low[now]=min(low[now],dfn[edge[i].v]);
35     }
36     if (low[now]==dfn[now])
37     {
38         do{
39             printf("%lld ",stack[index]);
40             v[stack[index--]]=0;
41         }while (now!=stack[index+1]);
42         printf("\n");
43     }
44 }
45 inline void add(ll a,ll b)
46 {
47     edge[++cnt].next=last[a];
48     edge[cnt].v=b;
49     last[a]=cnt;
50 }
51 int main()
52 {
53     read(n);read(m);
54     for (int i=1;i<=m;i++)
55     {
56         read(a);read(b);
57         add(a,b);
58     }
59     for (int i=1;i<=n;i++)
60     if (!dfn[i])tarjan(i);
61 }
View Code

LCA Tarjan

 1 #include <iostream>
 2 #include <cstdio>
 3 const int maxn=500000*2+100;
 4 int n,m,i,to[maxn],root,x,y,last[maxn],next[maxn],next1[maxn],last1[maxn],to1[maxn],ans[maxn],fa[maxn],v[maxn];
 5 using namespace std;
 6 inline void read(int &k)
 7 {
 8     int f=1;char c=getchar();k=0;
 9     while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
10     while (c>='0'&&c<='9')k=k*10+c-'0',c=getchar();
11     k*=f;
12 }
13 inline int gf(int now)
14 {
15     return fa[now]==now?now:fa[now]=gf(fa[now]);
16 }
17 inline void dfs(int now)
18 {
19     v[now]=1;
20     for(int cur=last[now];cur;cur=next[cur])
21     {
22         if (!v[to[cur]])
23         {
24             dfs(to[cur]);
25             fa[to[cur]]=now;
26         }
27     }
28     for (int cur=last1[now];cur;cur=next1[cur])
29     if (v[to1[cur]])ans[(cur+1)>>1]=gf(to1[cur]);
30 }
31 int main()
32 {
33     read(n);read(m);read(root);
34     for (i=1;i<n;i++)
35     {
36         read(x);read(y);
37         to[i*2-1]=y;
38         next[i*2-1]=last[x];
39         last[x]=i*2-1;
40         to[i*2]=x;
41         next[i*2]=last[y];
42         last[y]=i*2;
43     }
44     for (i=1;i<=m;i++)
45     {
46         read(x);read(y);
47         to1[i*2-1]=y;
48         next1[i*2-1]=last1[x];
49         last1[x]=i*2-1;
50         to1[i*2]=x;
51         next1[i*2]=last1[y];
52         last1[y]=i*2;
53     }
54     for (i=1;i<=n;i++)fa[i]=i;
55     dfs(root);
56     for (i=1;i<=m;i++)
57     printf("%d\n",ans[i]);
58 }
View Code

线段树

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #define ll long long
 5 using namespace std;
 6 ll n,m,l,r,tmp,add;
 7 const int maxn=200000;
 8 ll le[maxn*4],ri[maxn*4],delta[maxn*4],sum[maxn*4];
 9 void read(ll &k)
10 {
11     k=0;ll f=1;char c=getchar();
12     while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
13     while (c>='0'&&c<='9')k=k*10+c-'0',c=getchar();
14     k*=f;
15 }
16 void build(ll l,ll r,ll cur)
17 {
18     le[cur]=l;ri[cur]=r;
19     if (l==r){read(sum[cur]);return;}
20     build(l,(l+r)>>1,cur*2);
21     build(((l+r)>>1)+1,r,cur*2+1);
22     sum[cur]=sum[cur*2]+sum[cur*2+1];
23 }
24 ll query(ll l,ll r,ll cur)
25 {
26     if (l<=le[cur]&&ri[cur]<=r)return sum[cur]+delta[cur]*(ri[cur]-le[cur]+1);
27     ll ret=0;
28     delta[cur*2]+=delta[cur];
29     delta[cur*2+1]+=delta[cur];
30     sum[cur]+=delta[cur]*(ri[cur]-le[cur]+1);
31     delta[cur]=0;
32     if (l<=(le[cur]+ri[cur])>>1)ret=query(l,r,cur*2);
33     if (r>(le[cur]+ri[cur])>>1)ret+=query(l,r,cur*2+1);
34     return ret;
35 }
36 void update(ll l,ll r,ll add,ll cur)
37 {
38     if (l<=le[cur]&&r>=ri[cur]){delta[cur]+=add;return;}
39     if (l<=(le[cur]+ri[cur])>>1)update(l,r,add,cur*2);
40     if (r>(le[cur]+ri[cur])>>1)update(l,r,add,cur*2+1);
41     sum[cur]=sum[cur*2]+delta[cur*2]*(ri[cur*2]-le[cur*2]+1);
42     sum[cur]+=sum[cur*2+1]+delta[cur*2+1]*(ri[cur*2+1]-le[cur*2+1]+1);
43 }
44 int main()
45 {
46     read(n);
47     build(1,n,1);
48     read(m);
49     for (int i=0;i<m;i++)
50     {
51         read(tmp);
52         if (tmp==1)
53         {
54             read(l);read(r);read(add);
55             update(l,r,add,1);
56         }
57         if (tmp==2)
58         {
59             read(l);read(r);
60             printf("%lld\n",query(l,r,1));
61         }
62     }
63 }
View Code

快速幂

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int a,b;
 5 long long qp(int a,int b)
 6 {
 7     int t=1;int y=a;
 8     while (b)
 9     {
10         if (b&1) t=t*y;//whether odd or not
11         y=y*y;
12         b=b>>1;
13         //cout << a << " " << b << " " << t << " " << y << endl;
14     }
15     return(t);
16 }
17 int main()
18 {
19     scanf("%d %d",&a,&b);
20     printf("%lld",qp(a,b));
21 }
View Code

Kruskal

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #define ll long long
 6 using namespace std;
 7 struct poi{ll x,y,cost;}p[100001];
 8 ll n,m,ans,fa[100001];
 9 bool cmp(poi a,poi b){return a.cost<b.cost;}
10 int gf(ll v){return fa[v]==v?v:fa[v]=gf(fa[v]);}
11 int main()
12 {
13     scanf("%lld %lld",&n,&m);
14     for (int i=0;i<=n;i++)fa[i]=i;
15     for (int i=0;i<m;i++)scanf("%lld %lld %lld",&p[i].x,&p[i].y,&p[i].cost);
16     sort(p,p+m,cmp);
17     for (int i=0;i<m;i++)
18     {
19         int x=gf(p[i].x),y=gf(p[i].y);
20         if (x!=y) ans+=p[i].cost,fa[x]=y;
21     }
22     printf("%lld\n",ans);
23 }
View Code

 KMP

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #define ll long long 
 6 using namespace std;
 7 ll n,m,f[23333],j;
 8 char p[2333],t[2333];
 9 inline void read(ll &k)
10 {
11     ll f=1;char c=getchar();k=0;
12     while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
13     while (c>='0'&&c<='9')k=k*10+c-'0',c=getchar();
14     k*=f;
15 }
16 inline void getfail()
17 {
18     f[0]=0;f[1]=0;
19     for (int i=1;i<m;i++)
20     {
21         j=f[i];
22         while ((j>0)&&(t[i]!=t[j]))j=f[j];
23         if (t[i]==t[j])f[i+1]=j+1;else f[i+1]=0;
24     }
25 }
26 int main()
27 {
28     scanf("%s %s",p,t);
29     n=strlen(p);
30     m=strlen(t);
31     getfail();
32     j=0;
33     for (int i=0;i<n;i++)
34     {
35         while ((j>0)&&(p[i]!=t[j]))j=f[j];
36         if (p[i]==t[j])
37         {
38             if (j==m-1)printf("%lld\n",i-m+2);
39             j++;
40         }
41     }
42 }
View Code

 

posted @ 2017-08-11 16:44  Michael_Zhuang  阅读(178)  评论(0编辑  收藏  举报