POJ 1144 Network (undirected graph component cut point), poj1144
Address: POJ 1144
Cut Point. There are two conditions for determining whether a point is a cut point:
If u is a cut point, and only if the following one is satisfied
1. If u is the root of a tree, u must have more than one subtree.
2. if u is not the root of a tree, (u, v) is the edge of a tree branch. When Low [v]> = DFN [u.
Then we can find the cut point based on the two sentences.
The Code is as follows:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;int head[200], cnt, index1, ans;int vis[200], low[200], dfn[200], ge[200];struct node{ int u, v, next;}edge[2000];void add(int u, int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}void tarjan(int u, int fa){ int son=0, i; low[u]=dfn[u]=++index1; vis[u]=1; for(i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; son++; if(!vis[v]) { tarjan(v,u); low[u]=min(low[v],low[u]); if(u==1&&son>1||dfn[u]<=low[v]&&u!=1) { ge[u]++; } } else if(v!=fa) low[u]=min(low[u],dfn[v]); }}void init(){ memset(head,-1,sizeof(head)); cnt=0; index1=ans=0; memset(vis,0,sizeof(vis)); memset(dfn,0,sizeof(dfn)); memset(ge,0,sizeof(ge));}int main(){ int n, i, j, u, v; while(scanf("%d",&n)!=EOF&&n) { init(); while(scanf("%d",&u)!=EOF&&u) { while(getchar()!='\n') { scanf("%d",&v); add(u,v); add(v,u); } } tarjan(1,-1); for(i=1;i<=n;i++) { if(ge[i]) ans++; } printf("%d\n",ans); } return 0;}
The connected component of the undirected connected graph to be interpreted
Select B to create one connected component.
Because the graph itself is a connected graph, it is a connected component ~
If the graph is not connected, it has at least two connected components.
Ask how to (directed/undirected) The strong connected components of a graph and how to have several connected graphs.
Algorithms for determining strongly connected components include tarjan and kosaraju.
Compared with tarjan, Kosaraju is relatively simple to write.
But Kosaraju is relatively simple.
For other algorithms that require strongly connected components, an estimation is an algorithm that requires a more advanced data structure.
I suggest you study tarjan because he can help you do a lot of things, such as asking for a bridge to cut the point and shrink the ring and writing it easily.
The method of creating a connected graph can be used to directly record the DFS to a point at a time as it has arrived, and then continue to search for a connected graph at a time.
Code of tarjan
Var
Next, head, point: array [1 .. 1000] of longint;
Time, tot, I, j, n, m, x, y, t: longint;
V: array [1 .. 10000] of byte;
F, z, q: array [1 .. 1000] of longint;
Low, rea: array [1 .. 10000] of longint;
Function min (x, y: longint): longint;
Begin
If x <y then exit (x) else exit (y );
End;
Procedure add (x, y: longint );
Begin
Inc (tot );
Next [tot]: = head [x];
Head [x]: = tot;
Point [tot]: = y;
End;
Procedure dfs (x: Longint );
Var
I, j: longint;
Begin
Inc (time );
Low [x]: = time;
Rea [x]: = time;
V [x]: = 1;
Inc (t );
Z [t]: = x;
J: = head [x];
While j <> 0 do
Begin
If v [point [j] = 0 then dfs (point [j]);
If v [point [j] <2 then low [x]: = min (low [point [j], low [x]);
J: = next [j];
End;
If low [x] = rea [x] then
Begin
Inc (tot );
While z [t + 1] <> x do
Begin
Inc (q [tot]);
F [z [t]: = tot;
V [z [t]: = 2;
Dec (t );
End;
End;
End;
Begin
Readln (n, m );
For I: = 1 to m do
Begin
Readln (x, y );
Add (x, y );
End;
Tot: = 0; time: = 0;
For I: = 1 to n do
If v [I] = 0 then dfs (I );
// Writeln (tot );
For I: = 1 to n do
If q [f [I] <> 1 then writeln ('T') else writeln ('F ');
End... remaining full text>