Title Link:http://poj.org/problem?id=1523
The main topic: there are multiple sets of data, you need to find out the cut point of each group of data, and output the number of blocks where the cut
Algorithm implementation:
What is a cut point: when a non-connecting graph is stripped of a point and connected to the point, the undirected graph is divided into multiple disjoint sub-blocks, which are called cut points
What is a timestamp: The earliest time to visit when searching
Algorithm: Tarjan algorithm
Maintenance Dfn[u] represents the timestamp of U
Low[u] Represents the time stamp of the earliest ancestor that the U point can return to
Cut[u] Indicates the number of blocks to which the U point belongs
Determine the condition of the cut point Dfn[u]>=low[v]//v Point and its block does not have an edge point above the U point
Cut array Start assignment 1//each point belongs to at least one block
Each judgment cut point condition establishes cut[u]++;
Finally, if cut[u]>=2, you are the cutting point.
Note: The point at which you start the search is that the time stamp is the least, so the condition of the cut must be met, so the initial value is assigned 0.
Storage of graphs: chain-forward stars
1#include <cstdio>2#include <cmath>3#include <cstring>4#include <cstdlib>5#include <queue>6#include <stack>7#include <vector>8#include <iostream>9#include"algorithm"Ten using namespacestd; OnetypedefLong LongLL; A Const intmax1=1005; - Const intMax2=2505; - intn,m; the intTot,c,tx; - intHEAD[MAX1],ADJ[MAX2],NEXT[MAX2]; - intDfn[max1],low[max1],sta[max1]; - BOOLT[max1]; + intCut[max1]; - voidAddedge (intUintv) { +tot++; Aadj[tot]=v; atnext[tot]=Head[u]; -head[u]=tot; - } - voidDfsintXintFA) { -t[x]=true; -sta[++sta[0]]=x; indfn[x]=low[x]=++C; - inti,j; to BOOLFlag; +flag=true; - for(i=head[x];i;i=Next[i]) the{if(Adj[i]==fa &&flag) *{flag=true; $ Continue;Panax Notoginseng } - if(T[adj[i]]) thelow[x]=min (Low[x],dfn[adj[i]]); + Else A {DFS (adj[i],x); thelow[x]=min (Low[x],low[adj[i]]); + if(low[adj[i]]>=Dfn[x]) -{ while(sta[sta[0]]!=x) ${sta[0]--; $ } -cut[sta[sta[0]]]++; - } the } - }Wuyi } the intMain () { -Freopen ("spf.in","R", stdin); WuFreopen ("Spf.out","W", stdout); - intI,j;intcas;cas=0; About intu,v; $ while(1){ -scanf"%d",&u); - if(u==0) - Break; Aprintf"Network #%d\n",++CAs); +Memset (Next,0,sizeof(next)); theMemset (Head,0,sizeof(head)); -memset (adj,0,sizeof(adj)); $memset (DFN,0,sizeof(DFN)); thememset (Low,0,sizeof(Low)); thememset (Cut,0,sizeof(cut)); thememset (T,false,sizeof(t)); thetx=sta[0]=c=n=m=0; - while(1) in{scanf ("%d",&v); them++; then=Max (N,max (u,v)); About Addedge (u,v); the Addedge (v,u); thescanf"%d",&u); the if(u==0) + Break; - } the for(i=1; i<=n;i++)Bayi if(!T[i]) the{cut[i]=-1; the DFS (i,i); - } - BOOLflag=true; the for(i=1; i<=n;i++) the if(cut[i]>0) the{flag=false; theprintf"SPF node%d leaves%d subnets\n", i,cut[i]+1); - } the if(flag) puts ("No SPF Nodes"); thePutchar ('\ n'); the }94 return 0; the } the
POJ-1523 SPF (Tarjan for cutting points)