| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
第一道强连通,呵呵#include <stdio.h>
#define NL 0
#define MN 10002
int stack [MN],top= 0 ;
int pre[MN],low[MN],cnt0 = 1;
int sc[MN],cnt1 = 1;
int n,m ;
typedef struct Edge * Link ;
struct Edge{
int node ;
Link next ;
Edge( int n = 0, Link p = NULL ):node(n),next(p){}
} head[MN] ;
int count[MN],dcount[MN],ccount[MN] ;
void InsertEdge( Edge h[],int x,int y){
h[y].next = new Edge(x,h[y].next) ;
}
void SCR( int x ){ //求强连通分量
low[x] = pre[x] = cnt0 ++ ; stack[top++] = x ;
int minn = low[x] ; int nx,v ;
for( Link p = head[x].next ; p ; p = p->next ){
nx = p->node ;
if( pre[nx] == NL )SCR(nx) ;
if( low[nx] == n+1 && ccount[nx] != -1 ){
ccount[x]+=dcount[sc[nx]] ;
ccount[nx] = -1 ;
}
if( low[nx] < minn ) minn = low[nx];
}
if( minn < low[x] ){
low[x] = minn ; return ;
}
while( stack[top] != x ){
sc[ v = stack[--top] ] = cnt1 ; low[v] = n+1 ; //置为最大
++count[cnt1] ; if( v!= x)ccount[x] += ccount[v] ;
}
dcount[cnt1++] = ccount[x] ;
}
void Initial(){
int i ,j;
for( i = 1; i <= n ; i++ )
ccount[i] = 1 ;
for( i = 1; i <= n ;i ++ )
if( sc[i] == 0 )SCR(i) ;
}
int main(){
freopen("in.txt","r",stdin) ;
scanf("%d%d",&n,&m ) ; int x,y ;
while (m--){
scanf("%d%d",&x,&y) ;
if(x!=y) InsertEdge(head,x,y) ;
}
Initial() ;
if( dcount[--cnt1] == n )printf("%d",count[cnt1]);
else printf("0") ;
return 0 ;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator