POJ 1144 Network 裸割点

题意:

给一个图,求割点个数


#include 
#include 
#include 
using namespace std;
const int MAXN = 110;
int low[MAXN], dfn[MAXN], head[MAXN], tail, cnt, timer, n;
struct Line{ int to, nxt; }line[ MAXN * MAXN ];
bool iscut[MAXN];
void add_line( int from, int to ) {
    line[++tail].nxt = head[from];
    head[from] = tail;
    line[tail].to = to;
}
void init(){
    memset( head, 0, sizeof( head ) );
    memset( iscut, false, sizeof( iscut ) );
    memset( dfn, 0, sizeof( dfn ) );
    tail = 0; cnt = 0; timer = 0;
}
void Tarjan( int u ) {
    low[u] = ++timer; 
    dfn[u] = timer;
    int child = 0;
    for( register int i = head[u]; i; i = line[i].nxt ) {
        int v = line[i].to;
        if( !dfn[v] ) {
            child++;
            Tarjan( v );
            low[u] = min( low[u], low[v] );
            if( low[v] <= u ) iscut[u] = true;
        } else low[u] = min( low[u], dfn[v] );
    }
    if( u == 1 && child == 1 ) iscut[u] = false;
}
int main( ) {
    while( scanf( "%d", &n ) && n ) {
        init(); int x, y; 
        while( scanf( "%d", &x ) && x ) {
            while( getchar() != '\n' ) {
                scanf( "%d", &y );
                add_line( x, y ); add_line( y, x );
            }
        }
        Tarjan( 1 );
        for( register int i = 1; i <= n; i++ ) 
            if( iscut[i] ) cnt++;
        printf( "%d\n", cnt );
    }
    return 0;
}

POJ 1144 Network 裸割点_第1张图片

你可能感兴趣的:(割点和桥,tarjan)