1. For determining if there is a ring to the graph
Topology Ordering:
Topology Sorting principle:
1. From the Dag (directed acyclic graph), select a vertex without a precursor (that is, 0) and output.
2. Delete the vertex and all the forward edges that start with it.
3. Repeat 1 and 2 until the current DAG is empty or no precursor vertices exist in the current diagram, the latter case indicates that there must be a ring in the directed graph.
Time complexity: O (n+e), number of vertices and edges.
1 //topological sort to determine whether a graph has a ring2#include <queue>3#include <cstdio>4#include <cstring>5#include <algorithm>6 using namespacestd;7 8 Const intn=1e5+Ten;9 int inch[N];TenQueue <int>Q; One intt,n,m,cnt; AVector <int>E[n]; - - voidToposort () { the for(intI=1; i<=n;i++){ - if(inch[i]==0) Q.push (i); - } - while(!Q.empty ()) { +cnt++; - intu=Q.front (); + Q.pop (); A for(intI=0; I<e[u].size (); i++){ at intv=E[u][i]; - inch[v]--; - if(inch[v]==0) Q.push (v); - } - } - } in - intMain () { toscanf"%d",&t); + while(t--){ -Cnt=0; theMemsetinch,0,sizeof(inch)); * for(intI=1; i<n;i++) e[i].clear (); $scanf"%d%d",&n,&m);Panax Notoginseng for(intI=1; i<=m;i++){ - intx, y; thescanf"%d%d",&x,&y); + e[x].push_back (y); A inch[y]++; the } + Toposort (); - if(cnt==n) printf ("no\n"); $ Elseprintf"yes\n"); $ } - return 0; -}
View Code
Dfs:
Point staining during DFS:
- The points that have not been accessed by DFS are white, and the initial is a little white.
- If the point u has been accessed by DFS, but the child node of U is not fully accessible, then you are dyed grey
- If both the point U and the child nodes of u have been visited, you will be dyed black when you go back to the parent node of U.
If in the process of Dfs we reach a gray node along a forward edge, there is a ring in the diagram, and if the gray node has never been reached, there is no ring.
Time complexity: O (n+e), number of vertices and edges.
1 //DFS Determines if the graph has a ring2#include <vector>3#include <cstdio>4#include <cstring>5#include <iostream>6#include <algorithm>7 using namespacestd;8 9 Const intn=1e5+Ten;TenVector <int>E[n]; One intF,vis[n]; A - voidDfsintu) { - if(f)return ; thevis[u]=-1; - for(intI=0; I<e[u].size (); i++){ - intv=E[u][i]; - if(vis[v]==0) Dfs (v); + Else if(vis[v]==-1) {f=1;return ;} - } +vis[u]=1; A } at - intMain () { - intt,n,m; -scanf"%d",&t); - while(t--){ -f=0; inmemset (Vis,0,sizeof(Vis)); - for(intI=1; i<n;i++){ to e[i].clear (); + } -scanf"%d%d",&n,&m); the intb; * for(intI=1; i<=m;i++){ $scanf"%d%d",&a,&b);Panax Notoginseng E[a].push_back (b); - } the for(intI=1; i<=n;i++){ + if(f) Break; A if(vis[i]==0) Dfs (i); the } + if(f==1) printf ("yes\n"); - Elseprintf"no\n"); $ } $ return 0; -}
View Code
Note: Note the emptying of the vector's storage edge.
2. Determine if the graph has a ring
Suppose there are n connected sub-graphs of undirected graphs. If undirected graphs have no loops, each connected sub-graph is a tree, that is, the number of vertices of the connected sub-graph (vi) = number of sides (EI) +1.
Last obtained: V=e+n
Hiho No. 215 Week Circle Detect (Topological sort | DFS)