// 题目来源:POJ 3352 ( CCC 2007 )
// 题目大意:给定无向图G,为了满足删去任意一条边后原图任然连通,则原图至少需增加多少条边
// 解决方法:求出原图的桥数量,记作sum,则(sum+1)/2即为答案
#include <cstdio>
void link( int, int );
void tarjan( int, int );
void dfs( int );
int next[2002], h[2002], g[2002], code[2002], id[2002], l[2002], r[2002], dfn[2002], low[2002];
int n, m, t = 1, cnt, bcnt, sum, index;
bool p[2002];
int main( )
{
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );
int aa, bb;
scanf( "%d%d", &n, &m );
while( m-- )
{
scanf( "%d%d", &aa, &bb );
link( aa, bb );
}
tarjan( 1, 0 );
for( int i = 1; i <= n; i++ )
if( code[i] == 0 )
{
cnt++;
dfs( i );
}
for( int i = 1; i <= bcnt; i++ )
{
id[code[l[i]]]++;
id[code[r[i]]]++;
}
for( int i = 1; i <= cnt; i++ )
if( id[i] == 1 ) sum++;
printf( "%d", (sum+1)/2 );
return 0;
}
void link( int aa, int bb )
{
next[++t] = h[aa];
h[aa] = t;
g[t] = bb;
next[++t] = h[bb];
h[bb] = t;
g[t] = aa;
}
void tarjan( int i, int num )
{
int j;
dfn[i] = low[i] = ++index;
for( int k = h[i]; k; k = next[k] )
{
j = g[k];
if( ( k^num ) == 1 ) continue;
if( !dfn[j] )
{
tarjan( j, k );
if( low[j] < low[i] ) low[i] = low[j];
if( dfn[i] < low[j] )
{
p[k] = p[k^1] = 1;
l[++bcnt] = i;
r[bcnt] = j;
}
}
else
if( dfn[j] < low[i] ) low[i] = dfn[j];
}
}
void dfs( int i )
{
int j;
code[i] = cnt;
for( int k = h[i]; k; k = next[k] )
{
j = g[k];
if( p[k] || code[j] != 0 ) continue;
dfs( j );
}
}
【代码】POJ 3352
最新推荐文章于 2020-06-16 23:42:29 发布