An undirected graph is used to calculate a cut point. Is the use of tarjan.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Const int maxn = 1e3 + 9, N = 1e3;
Bool e [maxn] [maxn];
Int dfn [maxn], low [maxn], count, in [maxn];
Void tarjan (int t, int from)
{
Dfn [t] = low [t] = ++ count;
Int ret = 0;
For (int I = 1; I <= N; I ++)
If (e [t] [I])
{
If (I = from) continue;
If (dfn [I] =-1)
{
Ret ++;
Tarjan (I, t );
Low [t] = min (low [t], low [I]);
If (low [I]> = dfn [t])
In [t] ++;
}
Else
{
Low [t] = min (low [t], dfn [I]);
}
}
If (t = from)
In [t] = ret-1;
}
Int main ()
{
// Freopen ("in.txt", "r", stdin );
Int from, to, tcase = 0;
While (scanf ("% d", & from), from)
{
Scanf ("% d", & );
E [from] [to] = 1;
E [to] [from] = 1;
While (scanf ("% d", & from), from)
{
Scanf ("% d", & );
E [from] [to] = 1;
E [to] [from] = 1;
}
// Printf ("hi ");
Count = 0;
Memset (dfn,-1, sizeof (dfn ));
Memset (in, 0, sizeof (in ));
For (int I = 1; I <= N; I ++)
If (dfn [I] =-1)
Tarjan (I, I );
Printf ("Network # % d \ n", ++ tcase );
Int cnt = 0;
For (int I = 1; I <= N; I ++)
{
If (in [I]> 0)
{
Printf ("SPF node % d leaves % d subnets \ n", I, in [I] + 1 );
Cnt ++;
}
}
If (! Cnt)
Printf ("No SPF nodes \ n ");
Memset (e, 0, sizeof (e ));
Printf ("\ n ");
}
Return 0;
}
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Const int maxn = 1e3 + 9, N = 1e3;
Bool e [maxn] [maxn];
Int dfn [maxn], low [maxn], count, in [maxn];
Void tarjan (int t, int from)
{
Dfn [t] = low [t] = ++ count;
Int ret = 0;
For (int I = 1; I <= N; I ++)
If (e [t] [I])
{
If (I = from) continue;
If (dfn [I] =-1)
{
Ret ++;
Tarjan (I, t );
Low [t] = min (low [t], low [I]);
If (low [I]> = dfn [t])
In [t] ++;
}
Else
{
Low [t] = min (low [t], dfn [I]);
}
}
If (t = from)
In [t] = ret-1;
}
Int main ()
{
// Freopen ("in.txt", "r", stdin );
Int from, to, tcase = 0;
While (scanf ("% d", & from), from)
{
Scanf ("% d", & );
E [from] [to] = 1;
E [to] [from] = 1;
While (scanf ("% d", & from), from)
{
Scanf ("% d", & );
E [from] [to] = 1;
E [to] [from] = 1;
}
// Printf ("hi ");
Count = 0;
Memset (dfn,-1, sizeof (dfn ));
Memset (in, 0, sizeof (in ));
For (int I = 1; I <= N; I ++)
If (dfn [I] =-1)
Tarjan (I, I );
Printf ("Network # % d \ n", ++ tcase );
Int cnt = 0;
For (int I = 1; I <= N; I ++)
{
If (in [I]> 0)
{
Printf ("SPF node % d leaves % d subnets \ n", I, in [I] + 1 );
Cnt ++;
}
}
If (! Cnt)
Printf ("No SPF nodes \ n ");
Memset (e, 0, sizeof (e ));
Printf ("\ n ");
}
Return 0;
}