POJ 1523 SPF Problem Solving report

Source: Internet
Author: User

Idea: Use the Tarjan algorithm to find the cut point, in the enumeration to remove each cut point can form the number of Unicom block.

Note: Later I looked at the other code, found that my enumeration of the way to cut points is a rather stupid way, we can completely in the Tarjan process to find out the answer, introduce the discussion:

If this cut point is the root node, in the Tarjan algorithm to find a few children node (Low[v] >= dfn[u]), he can cut a few unicom block, if the cut is a child node, then he formed the number of Unicom block +1, because he also has a father node indirectly or directly connected to the side.

The code is as follows:

#include <map>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespacestd;#defineMAXN 1010structedge{intTO,NXT;} EDGE[MAXN*Ten];intHEAD[MAXN],LOW[MAXN],DFN[MAXN],MARK[MAXN],VIS[MAXN];intTot,all,son,start,tail;voidAdd_edge (intUintv) {edge[tot].to=v; EDGE[TOT].NXT=Head[u]; Head[u]= tot++;}voidinit () {memset (Low,0,sizeof(low)); memset (DFN,0,sizeof(DFN)); memset (Mark,0,sizeof(Mark)); All=0; Son=0;}voidTarjan (intUintFA) {Dfn[u]= Low[u] = + +All ;  for(inti = Head[u]; I! =-1; i =edge[i].nxt) {        intv =edge[i].to; if(!Dfn[v])            {Tarjan (v,u); Low[u]=min (low[v],low[u]); if(Low[v] >=Dfn[u]) {                if(U = = start) son++; ElseMark[u] =1; }        }        Else if(v! = FA) Low[u] =min (low[u],dfn[v]); }    return ;}voidBFsintXintcant) {Queue<int>que;  while(!que.empty ()) Que.pop ();    Que.push (x); VIS[X]=1;  while(!Que.empty ()) {        intnum =Que.front ();        Que.pop ();  for(inti = Head[num]; I! =-1; i =edge[i].nxt) {            intv =edge[i].to; if(v! = Cant &&!Vis[v])                {Que.push (v); VIS[V]=1; }        }    }    return ;}intMain () {intb; intCA =0;  while(~SCANF ("%d",&a)) {if(!a) Break; Start=0, tail =0; scanf ("%d",&b); Start=min (A, b); Tail=Max (A, b); Tot=0; memset (Head,-1,sizeof(head));        Add_edge (A, b);        Add_edge (B,a);  while(~SCANF ("%d",&a)) {if(!a) Break; scanf ("%d",&b);            Add_edge (A, b);            Add_edge (B,a); Start=min (A, B), start); Tail=Max (A, b), tail;        } init (); Tarjan (Start,-1); if(Son >=2) Mark[start] =1; intsubnets =0, SPF =0; printf ("Network #%d\n",++CA); BOOLFlag =true;  for(inti = start; I <= tail; i++)        {            if(Mark[i]) {flag=false; printf ("SPF node%d leaves", i); memset (Vis,0,sizeof(VIS)); Subnets=0;  for(intj = Start;j <= tail;j++)                {                    if(j = = i)Continue; if(!Vis[j]) {Subnets++;                    BFS (J,i); }} printf ("%d subnets\n", subnets); }        }        if(flag) puts ("No SPF Nodes"); Puts (""); }    return 0;}

POJ 1523 SPF Problem Solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.