POJ 3352 Road Construction (边双连通,缩点)
2011-10-22 10:29
323 查看
题意:加上最少的边,使得改造后的图中去掉任意一条边后图依然连通。
题解:先找出边双连通分量,然后缩点,的到一棵树。需要加的最少边=(leaves+1)/2
PS:
![](http://hi.csdn.net/attachment/201110/22/0_1319251206U2kX.gif)
我们可以发现low[4]=3,low[7]=4 但是我们知道<4,7>这条边并不是割边.所以 low[u]!=low[v]是割边的必要不充分条件.
题解:先找出边双连通分量,然后缩点,的到一棵树。需要加的最少边=(leaves+1)/2
PS:
![](http://hi.csdn.net/attachment/201110/22/0_1319251206U2kX.gif)
我们可以发现low[4]=3,low[7]=4 但是我们知道<4,7>这条边并不是割边.所以 low[u]!=low[v]是割边的必要不充分条件.
#include<cstdio> #include<string> #include<algorithm> using namespace std; #define N 2009 int n, m, leaves; int size, scc, id, top; int stack , instack ; int head , low , dfn , degree , block ; struct Edge { int v, next; } edge[N*3]; void Tarjan ( int u, int father ) { int v, t; stack[++top] = u; instack[u] = 1; dfn[u] = low[u] = ++id; for ( int i = head[u]; i; i = edge[i].next ) { v = edge[i].v; if ( father == v ) continue; if ( ! dfn[v] ) { Tarjan ( v, u ); low[u] = min ( low[u], low[v] ); if ( dfn[u] < low[v] ) { scc++; do { t = stack[top--]; instack[t] = 0; block[t] = scc; } while ( v != t ); } } else if ( instack[v] ) low[u] = min ( low[u], dfn[v] ); } } void shrink() { int u, v, i; for ( u = 1; u <= n; u++ ) { for ( i = head[u]; i; i = edge[i].next ) { v = edge[i].v; if ( block[u] != block[v] ) { degree[block[u]]++; degree[block[v]]++; } } } for ( i = 1; i <= scc; i++ ) //由于每个点重复计算了一次,所以叶子节点的度为2 if ( degree[i] == 2 ) leaves++; } void Initial() { size = id = scc = top = leaves = 0; for ( int i = 1; i <= n; i++ ) { head[i] = degree[i] = instack[i] = 0; low[i] = dfn[i] = block[i] = 0; } } void add ( int u, int v ) { size++; edge[size].v = v; edge[size].next = head[u]; head[u] = size; } int main() { int u, v; while ( scanf("%d%d",&n,&m) != EOF ) { Initial(); while ( m-- ) { scanf("%d%d",&u,&v); add ( u, v ); add ( v, u ); } for ( int i = 1; i <= n; i++ ) if ( ! dfn[i] ) Tarjan ( i, n+1 ); shrink(); printf("%d\n",(leaves+1)/2 ); } return 0; }
相关文章推荐
- poj 3352 Road Construction 边双连通缩点
- POJ 3352--Road Construction【无向图增加最少的边成为边双连通图 && tarjan求ebc && 缩点构造缩点树】
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
- POJ-3352 Road Construction,tarjan缩点求边双连通!
- POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)
- poj3352——Road Construction——————【加边为边-双连通图】
- POJ 3352 Road Construction 边的双连通分量 + 缩点
- POJ 3352 Road Construction (边双连通分量 Tarjan缩点)
- poj 3352 Road Construction(边双连通)
- POJ 3352 Road Construction POJ 3177Redundant Paths(Tarjan缩点)
- POJ 3352 Road Construction (缩点)
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)
- poj3352--E - Road Construction(无向图加边成双连通)
- poj 3352 Road Construction (双连通)
- POJ 3177 / POJ 3352 : Redundant Paths / Road Construction - 边双连通分量,缩点
- poj 3352 Road Construction 缩点
- POJ 题目 Road Construction(双连通)
- 【POJ 3352】 Road Construction(边联通分量入门)
- poj3352——Road Construction//最小割边
- poj 3352 Road Construction 边双连通分量