SPF
Time Limit: 1000 MS
Memory Limit: 10000 K
Total Submissions: 3790
Accepted: 1727
Description
Consider the two networks shown below. assuming that data moves around these networks only between directly connectednodes on a peer-to-peer basis, a failure of a single node, 3, in the network onthe left wowould prevent some of the still available nodes from communicatingwith each other. nodes 1 and 2 cocould still communicate with each other as couldnodes 4 and 5, but communication between any other pairs of nodes wowould nolonger be possible.
Node 3 is therefore a Single Point of Failure (SPF) for this network. strictly, an SPF will be defined as any node that, if unavailable, wocould prevent at leastone pair of available nodes from being able to communicate on what waspreviusly a fully connected network. note that the network on the right has nosuch node; there is no SPF in the network. at least two machines must fail beforethere are any pairs of available nodes which cannot communicate.
Input
The input will contain in the description ofseveral networks. A network description will consist of pairs of integers, onepair per line, that identify connected nodes. ordering of the pairs isirrelevant; 1 2 and 2 1 specify the same connection. all node numbers willrange from 1 to 1000. A line containing a single zero ends the list ofconnected nodes. an empty network description flags the end of the input. blanklines in the input file shoshould be ignored.
Output
For each network in the input, you willoutput its number in the file, followed by a list of any SPF nodes thatexist.
The first network in the file shocould be identified as "Network #1", the second as "Network #2", etc. for each SPF node, output a line, formatted as shown in the examples below, that identifies the node and thenumber of fully connected subnets that remain when that node fails. if thenetwork has no SPF nodes, simply output the text "No SPF nodes" instead of a list of SPF nodes.
Sample Input
1 2
5 4
3 1
3 2
3 4
3 5
0
1 2
2 3
3 4
4 5
5 1
0
1 2
2 3
3 4
4 6
6 3
2 5
5 1
0
0
Sample Output
Network #1
SPF node 3 leaves2 subnets
Network #2
No SPF nodes
Network #3
SPF node 2 leaves2 subnets
SPF node 3 leaves2 subnets
Source
Greater New York 2000
Question:
Here is a network diagram for you to find and remove the cut points in the graph, which can split the graph into several subgraphs.
Solution:
Create a map based on the input, and find the cut point based on the tarJan algorithm. For each cut point, traverse the points connected to it and perform a deep search. After calculation, all the points can be accessed several times.
Code:
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Define maxn1003
Using namespace std;
Int map [maxn] [maxn];
Int low [maxn];
Int dfn [maxn];
Int visited [maxn];
Int flag [maxn];
Int n; // calculates the number of points.
Int times;
Int root; // root Node
Int max (int a, int B)
{
Return a> B? A: B;
}
Int min (int a, int B)
{
Return a <B? A: B;
}
// Deep search for connected subgraphs
Void dfs (int x, int fa)
{
Visited [x] = 1;
For (int I = 1; I <= n; I ++)
{
If (I! = Fa)
{// Remove the deleted Node
If (! Visited [I] & map [x] [I])
{
Dfs (I, fa );
}
}
}
}
Void tarJan (int u, int fa) // The parent node of Point u and Point u
{
Int son = 0; // son cannot be defined as a global variable.
Low [u] = dfn [u] = ++ times;
For (int I = 1; I <= n; I ++)
{
If (! Map [u] [I])
Continue;
If (! Dfn [I])
{// I point if it has not been accessed
Son ++;
TarJan (I, u );
Low [u] = min (low [I], low [u]);
If (u = root & son> = 2) | (u! = Root & dfn [u] <= low [I])
{
Flag [u] = 1;
}
}
Else if (I! = Fa)
{
Low [u] = min (low [u], dfn [I]);
}
}
}
Void init ()
{
Memset (map, 0, sizeof (map ));
Memset (low, 0, sizeof (low ));
Memset (dfn, 0, sizeof (dfn ));
Memset (flag, 0, sizeof (flag ));
Times = 0;
N =-1;
Root = 1;
}
Void input ()
{
Int s, t;
Int f = 0;
Int ts = 1;
While (true)
{
Init ();
While (true)
{
Scanf ("% d", & s );
If (s = 0)
{
F ++;
If (f> = 2)
Return;
Break;
}
F = 0;
Scanf ("% d", & t );
Map [s] [t] = map [t] [s] = 1;
N = max (n, max (s, t ));
}
TarJan (root,-1 );
Int count;
Int num = 0;
Printf ("Network # % d \ n", ts );
For (int I = 1; I <= n; I ++)
{
If (flag [I] = 1)
{
// Remove I
Num ++;
Count = 0;
Memset (visited, 0, sizeof (visited ));
For (int j = 1; j <= n; j ++)
{
If (! Visited [j] & map [I] [j] & j! = I)
{
Dfs (j, I );
Count ++;
}
}
Printf ("SPF node % d leaves % d subnets \ n", I, count );
}
}
If (num = 0)
{
Printf ("No SPF nodes \ n ");
}
Ts ++;
Printf ("\ n ");
}
}
Int main ()
{
Input ();
Return 0;
}