板子都在这!爱看不看!


【1.强连通分量】( poj 2186 )

//poj 2186 强连通分量缩点 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=10005;
stack <int> s;
bool vis[mxn],mark[mxn];
int n,m,tim,num,cnt,ans;
int head[mxn],dfn[mxn],low[mxn],bel[mxn],x[mxn*10],y[mxn*10],size[mxn]; //bel:u是属于哪个集合中的
struct edge {int to,next;} f[mxn*10];
inline void add(int u,int v)
{
f[++cnt].to=v,f[cnt].next=head[u],head[u]=cnt;
}
inline void tarjan(int u)
{
dfn[u]=low[u]=++tim;
vis[u]=1;
s.push(u);
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(!dfn[v])
tarjan(v),low[u]=min(low[u],low[v]);
else if(vis[u])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int v;num++;
do
{
v=s.top(),s.pop();
vis[v]=0,bel[v]=num;
size[num]++;
}while(u!=v);
}
}
int main()
{
int i,j,tot=0;
scanf("%d%d",&n,&m);
fo(i,1,m)
{
scanf("%d%d",&x[i],&y[i]);
add(x[i],y[i]);
}
fo(i,1,n) if(!dfn[i]) tarjan(i);
fo(i,1,m) if(bel[x[i]]!=bel[y[i]]) mark[bel[x[i]]]=1; //表示点集bel[x[i]]有出度
fo(i,1,num)
if(!mark[i])
ans=size[i],tot++;
if(tot==1) printf("%d\n",ans);
else printf("0\n");
return 0;
}

【2.双连通分量】(poj 3352 Road Construction)

//tarjan 求双联通分量(分量内任意两点至少能通过两条点不相交的路径互相到达) 
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=100005;
int n,m,cnt,tim,tot,tp,num,ans;
int dfn[mxn],low[mxn],head[mxn],s[mxn],x[mxn],y[mxn],du[mxn],bel[mxn];
struct edge {int to,next;} f[mxn];
inline void add(int u,int v)
{
f[++cnt].to=v,f[cnt].next=head[u],head[u]=cnt;
}
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
inline void dfs(int u,int fa)
{
dfn[u]=low[u]=++tim;
s[++tp]=u;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(!dfn[v]) dfs(v,u),low[u]=min(low[u],low[v]);
else if(v!=fa) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
num++;int v;
do
{
v=s[tp--];
bel[v]=num;
}while(u!=v);
}
}
int main()
{
int i,j,u,v;
n=read(),m=read();
fo(i,1,m)
{
x[i]=read(),y[i]=read();
add(x[i],y[i]),add(y[i],x[i]);
}
fo(i,1,n) if(!dfn[i]) dfs(i,0);
fo(i,1,m)
if(bel[x[i]]!=bel[y[i]])
du[bel[x[i]]]++,du[bel[y[i]]]++;
fo(i,1,num) if(du[i]==1) ans++;
printf("%d\n",ans+1>>1);
return 0;
}

【3.求桥】(条件 : dfn[u] < low[v]) (洛谷 炸铁路)

//tarjan 求桥 
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define mp make_pair
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=100005;
int n,m,cnt,tim,tot;
int dfn[mxn],low[mxn],head[mxn];
struct edge {int to,next;} f[mxn];
pair <int,int> ans[mxn];
inline void add(int u,int v)
{
f[++cnt].to=v,f[cnt].next=head[u],head[u]=cnt;
}
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
inline void dfs(int u,int fa)
{
dfn[u]=low[u]=++tim;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(!dfn[v]) dfs(v,u),low[u]=min(low[u],low[v]);
else if(v!=fa) low[u]=min(low[u],dfn[v]);
if(dfn[u]<low[v]) //判断桥的条件
{
tot++;
if(u<v) ans[tot].first=u,ans[tot].second=v;
else ans[tot].first=v,ans[tot].second=u;
}
}
}
int main()
{
int i,j,u,v;
n=read(),m=read();
fo(i,1,m)
{
u=read(),v=read();
add(u,v),add(v,u);
}
fo(i,1,n)
if(!dfn[i])
dfs(i,0);
sort(ans+1,ans+tot+1);
fo(i,1,tot)
printf("%d %d\n",ans[i].first,ans[i].second);
return 0;
}

【4.求割点】(bzoj 2730: [HNOI2012]矿场搭建 ) [注意这道题不是板子题]

//tarjan 求割点
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll unsigned long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=100005;
ll ans;
int n,m,cnt,tim,tot,sum,point;
bool vis[mxn],yes[mxn],czy[mxn],ok[mxn]; //yes:是否是割点
int dfn[mxn],low[mxn],head[mxn];
struct edge {int to,next;} f[mxn];
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
inline void add(int u,int v)
{
f[++cnt].to=v,f[cnt].next=head[u],head[u]=cnt;
}
inline void dfs(int u,int fa)
{
int child=0;
dfn[u]=low[u]=++tim;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(!dfn[v])
{
dfs(v,u),child++;
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v]) yes[u]=1;
}
else if(v!=fa) low[u]=min(low[u],dfn[v]);
}
if(!fa && child==1) yes[u]=0;
}
inline void bfs(int u,int fa)
{
vis[u]=1,sum++; // -_- #_# $_$ *_*
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(v==fa) continue;
if(!vis[v] && !yes[v]) bfs(v,u);
if(yes[v] && !ok[v]) ok[v]=1,tot++;
}
}
int main()
{
int i,j,u,v,T=0;
while(scanf("%d",&m) && m)
{
T++;
M(vis);M(yes);M(czy);
M(dfn);M(low);M(head);
n=cnt=tim=point=0;ans=1;
fo(i,1,m)
{
u=read(),v=read();
add(u,v),add(v,u);
n=max(n,max(u,v));
czy[u]=czy[v]=1;
}
fo(i,1,n) if(czy[i] && !dfn[i]) dfs(i,0);
//求连通块个数
fo(i,1,n)
if(czy[i] && !yes[i] && !vis[i])
{
tot=sum=0;
M(ok); //编号为i的割点是否被访问过
bfs(i,0);
if(tot==1) ans*=(ll)sum,point++;
}
if(!point) ans=(ll)n*(ll)(n-1)/2,printf("Case %d: 2 %llu\n",T,ans);
else printf("Case %d: %d %llu\n",T,point,ans);
}
return 0;
}

以上。