Spf
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 7406 |
|
Accepted: 3363 |
Description
Consider the networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of A single node, 3, and the network on the left would prevent some of the still available nodes from communicating with each Other. Nodes 1 and 2 could still communicate with all other as could Nodes 4 and 5, but communication between all other pairs of Nodes would no longer be possible.
Node 3 is therefore a, Failure (SPF) for this network. Strictly, an SPF would be defined as any node that, if unavailable, would prevent at least one pair of available nodes from Being able to communicate in what is previously a fully connected network. Note the network on the right have no such node; There is no SPF in the network. At least machines must fail before there is any pairs of available nodes which cannot communicate.
Input
The input would contain the description of several networks. A Network description would consist of pairs of integers, one pair per line, that identify connected nodes. Ordering of the pairs is irrelevant; 1 2 and 2 1 specify the same connection. All node numbers would range from 1 to 1000. A line containing a single zero ends the list of connected nodes. An empty network description flags the end of the input. Blank lines in the input file should is ignored.
Output
For each network in the input, you'll output its number in the file, and followed by a list of all SPF nodes that exist.
The first network in the file should is 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 the number of ful Ly connected subnets that remain where that node fails. If the network has no SPF nodes, simply output the text "No SPF nodes" instead of a list of SPF nodes.
Sample Input
1 25 43 13 23 43 501 22 33 44 55 101 22 33 44 66 32 55 100
Sample Output
Network #1 SPF node 3 leaves 2 subnetsnetwork #2 No SPF nodesnetwork #3 SPF node 2 leaves 2 subnets SPF No De 3 leaves 2 subnets
Test instructions: Ask how many connected components can be generated by deleting a point.
Idea: Investigate the principle of Tarjan algorithm understanding, explanation see Code.
#include"Cstdio"#include"CString"using namespacestd;Const intmaxn=1005;structedge{intTo,next;} ES[MAXN*2];intv,e;intHead[maxn];inlineintMaxintUintv) { returnU > v?U:v;} InlineintMinintAintb) { returna > B?b:a;}voidAdd_edge (intUintv) {es[e].to=v; Es[e].next=Head[u]; Head[u]=e++; V=Max (Max (u,v), v);}BOOLFlag;intRoot;intSUBNETS[MAXN];intindex;intDFN[MAXN],LOW[MAXN];voidTarjan (intUintFA) { intson=0; Dfn[u]=low[u]=++index; for(inti=head[u];i!=-1; i=Es[i].next) { intv=es[i].to; if(!Dfn[v]) {Tarjan (v,u); Son++; Low[u]=min (low[u],low[v]); if((u==root&&son>1)|| (u!=root&&dfn[u]<=Low[v])) {Flag=true; Subnets[u]++; //U->v this side causes U to become cut point//when Dfn[u]==low[v] U->v is the atavistic side, U, v in the same double connected component//when Dfn[u]<low[v] u->v for cutting edges//The number of connections that are generated by deleting a cut U is: The number of connected components you are in + the number of cut edges connected to u +1 (side: Fa->u) } } Else if(V!=FA) low[u]=min (low[u],dfn[v]); }}intMain () {intcas=0; intu,v; while(true) {v=-1; Index=0; memset (Subnets,0,sizeof(subnets)); memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); memset (Head,-1,sizeof(head)); V=-1, e=0; Flag=false; while(SCANF ("%d", &u) &&u) {scanf ("%d",&v); Add_edge (U,V); Add_edge (V,u); } if(v==-1) Break; Root=V; Tarjan (Root,-1); printf ("Network #%d\n",++CAs); if(flag) { for(intI=1; i<=v;i++) { if(subnets[i]>0) {printf ("SPF node%d leaves%d subnets\n", i,subnets[i]+1);//plus Fa->u The connected component of the edge } } } Elseprintf"No SPF nodes\n"); printf ("\ n"); } return 0;}
POJ1523 (to find the number of components, Tarjan algorithm principle understanding)