poj2375 强连通

时间:2021-08-18 19:12:38

题意:有一个 l * w 大小的滑雪场,每个格子都有一个高度,每个格子可以直接通到上下左右四个格子中高度小于等于自己的格子,现在要建立通道,能够连通任意两个格子,问最少建多少通道能够使所有格子能够互相到达。

其实就是问加多少条边能够使整个图强连通,也就是求强连通分量中入度为 0 和出度为 0 的分量个数的最大值,如果仅一个强连通分量则为 0 。

RE 10 发,我以为是因为我链式前向星数组开太大,于是换邻接矩阵,又 RE,一看DISCUSS,G++RE,C++AC,一交C++A了,把第一次RE的交了一发A了,所以RE的小伙伴们……交C++吧……

链式前向星:

 #include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
using namespace std; const int maxn=;
const int maxm=1e6+; int head[maxn],point[maxm],nxt[maxm],size;
int n,t,scccnt;
int stx[maxn],low[maxn],scc[maxn];
int id[maxn],od[maxn];
int ma[][];
int xx[]={,-,,};
int yy[]={,,,-};
stack<int>S; int max(int a,int b){return a>b?a:b;} void init(){
memset(head,-,sizeof(head));
size=;
} void add(int a,int b){
point[size]=b;
nxt[size]=head[a];
head[a]=size++;
} void dfs(int s){
stx[s]=low[s]=++t;
S.push(s);
for(int i=head[s];~i;i=nxt[i]){
int j=point[i];
if(!stx[j]){
dfs(j);
low[s]=min(low[s],low[j]);
}
else if(!scc[j]){
low[s]=min(low[s],stx[j]);
}
}
if(low[s]==stx[s]){
scccnt++;
while(){
int u=S.top();S.pop();
scc[u]=scccnt;
if(s==u)break;
}
}
} void setscc(){
memset(stx,,sizeof(stx));
memset(scc,,sizeof(scc));
t=scccnt=;
for(int i=;i<=n;++i)if(!stx[i])dfs(i);
for(int i=;i<=n;++i){
for(int j=head[i];~j;j=nxt[j]){
int k=point[j];
if(scc[i]!=scc[k]){
od[scc[i]]++;
id[scc[k]]++;
}
}
}
} int main(){
int w,l;
scanf("%d%d",&w,&l);
n=w*l;
init();
for(int i=;i<=l;++i){
for(int j=;j<=w;++j){
scanf("%d",&ma[i][j]);
}
}
for(int i=;i<=l;++i){
for(int j=;j<=w;++j){
for(int k=;k<;++k){
int x=i+xx[k],y=j+yy[k];
if(x>=&&x<=l&&y>=&&y<=w&&ma[x][y]<=ma[i][j]){
add((i-)*w+j,(x-)*w+y);
}
}
}
}
setscc();
if(scccnt==)printf("0\n");
else{
int in=,out=;
for(int i=;i<=scccnt;++i){
if(!id[i])in++;
if(!od[i])out++;
}
printf("%d\n",max(in,out));
}
}

邻接矩阵:

 #include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
using namespace std; const int maxn=*; int n,t,scccnt;
int w,l;
int stx[maxn],low[maxn],scc[maxn];
int id[maxn],od[maxn];
int ma[][];
int xx[]={,-,,};
int yy[]={,,,-};
stack<int>S; int max(int a,int b){return a>b?a:b;} inline int getid(int a,int b){
return (a-)*w+b;
} inline void getpoint(int num,int &a,int &b){
a=num/w;
b=num%w;
if(b)a++;
else b=w;
} void dfs(int s){
stx[s]=low[s]=++t;
S.push(s);
int x,y;
getpoint(s,x,y);
for(int p=;p<;++p){
int dx=x+xx[p],dy=y+yy[p];
if(dx>=&&dx<=l&&dy>=&&dy<=w&&ma[dx][dy]<=ma[x][y]){
int j=getid(dx,dy);
if(!stx[j]){
dfs(j);
low[s]=min(low[s],low[j]);
}
else if(!scc[j]){
low[s]=min(low[s],stx[j]);
}
}
}
if(low[s]==stx[s]){
scccnt++;
while(){
int u=S.top();S.pop();
scc[u]=scccnt;
if(s==u)break;
}
}
} void setscc(){
memset(stx,,sizeof(stx));
memset(scc,,sizeof(scc));
t=scccnt=;
for(int i=;i<=n;++i)if(!stx[i])dfs(i);
for(int i=;i<=n;++i){
int x,y;
getpoint(i,x,y);
for(int p=;p<;++p){
int dx=x+xx[p],dy=y+yy[p];
if(dx>=&&dx<=l&&dy>=&&dy<=w&&ma[dx][dy]<=ma[x][y]){
int k=getid(dx,dy);
if(scc[i]!=scc[k]){
od[scc[i]]++;
id[scc[k]]++;
}
}
}
}
} int main(){
scanf("%d%d",&w,&l);
n=w*l;
memset(id,,sizeof(id));
memset(od,,sizeof(od));
for(int i=;i<=l;++i){
for(int j=;j<=w;++j){
scanf("%d",&ma[i][j]);
}
}
setscc();
if(scccnt==)printf("0\n");
else{
int in=,out=;
for(int i=;i<=scccnt;++i){
if(!id[i])in++;
if(!od[i])out++;
}
printf("%d\n",max(in,out));
}
}