Test instructions: There are some networks connected by some lines, the critical connection, that is, the bridge, if you delete this link will produce two sub-tree analysis: Note One is not connected graph can be *************************************************** ****************
#include <stdio.h>
#include <string.h>
#include <stack>
#include <algorithm>
usingnamespaceStd
ConstintMAXN =1005;
///Building adjacency Tables
structedge{intV, Next;} E[MAXN*MAXN];
intHEAD[MAXN], CNT;
voidAddedge (intUintV
{
E[CNT].V = v;
E[cnt].next = Head[u];
Head[u] = cnt++;
}
///Record Bridge
structbridge{intU, V;} BRI[MAXN];
intBnt
BOOLCMP (bridge N1, bridge N2)
{
if(n1.u! = n2.u)
returnN1.u < n2.u;
returnN1.V < N2.V;
}
intF[MAXN];
intDFN[MAXN], LOW[MAXN], Index;
voidInIt (intN
{
CNT = BNT = Index =0;
for(intI=0; i<=n; i++)
{
Head[i] =-1;
Dfn[i] =false;
}
}
voidTarjan (intUintFather
{
F[u] = father;
Low[u] = dfn[u] = ++index;
for(intJ=head[u]; j!=-1; J=e[j].next)
{
intv = e[j].v;
if( ! DFN[V])
{
Tarjan (V, u);
Low[u] = min (Low[u], low[v]);
}
Elseif(V! = father)
Low[u] = min (Low[u], dfn[v]);
}
}
intMain ()
{
intN
while(SCANF ("%d", &n)! = EOF)
{
intI, U, V, M;
InIt (N);
for(i=0; i<n; i++)
{
scanf"%d (%d)", &u, &m);
while(m--)
{
scanf"%d", &v);
Addedge (U, v);
}
}
for(i=0; i<n; i++)
{
if( ! Dfn[i])
Tarjan (i, I);
}
for(i=0; i<n; i++)
{
intU = f[i];
if(Low[i] > Dfn[u])
{
bri[bnt].u = min (u, i);
BRI[BNT++].V = max (U, i);
}
}
Sort (BRI, BRI+BNT, CMP);
printf"%d critical links\n", BNT);
for(i=0; i<bnt; i++)
printf"%d-%d\n", bri[i].u, BRI[I].V);
printf"\ n");
}
return0;
}
C-critical Links-uva 796 (for bridge)