Topic Link: Click to open the link
Test instructions
Given a direction graph, ask:
1) Select at least a few vertices. Ability to proceed from these vertices to reach all vertices
2) At least how many edges to add. Talent makes it possible to reach all vertices from whatever vertex
Number of vertices <= 100
After the strong connected component is obtained, the shrinkage point is calculated, and the degree of each point's penetration is computed.
The answer to the first question is the number of points with zero degrees,
The second question is that Max (N,M)//is zero in the number of N, the number of degrees to zero is M. Kuangbin giant analysis is great!
#include <cstdio> #include <cstring> #include <vector> #include <stack> #include <algorithm >using namespace Std;const int maxn = + 10;vector<int> g[maxn];int DFN[MAXN], LOW[MAXN], BELONG[MAXN], DFS_CL Ock, scc_cnt;stack<int> s;void dfs (int u) {dfn[u] = low[u] = ++dfs_clock; S.push (U); for (int i=0; i<g[u].size (); ++i) {int v = g[u][i]; if (!dfn[v]) {DFS (v); Low[u] = min (Low[u], low[v]); }else if (!belong[v]) {Low[u] = min (Low[u], dfn[v]); }} if (low[u] = = Dfn[u]) {scc_cnt++; for (;;) {int x = S.top (); S.pop (); BELONG[X] = scc_cnt; if (x = = u) break; }}}void FIND_SCC (int n) {dfs_clock = scc_cnt = 0; memset (belong, 0, sizeof belong); memset (DFN, 0, sizeof DFN); for (int i=0; i<n; ++i) if (!dfn[i]) DFS (i);} int main () {int n, I, J, X; scanf ("%d", &n); For (i=0, i<n; ++i) {while(scanf ("%d", &x), X) {x--; G[i].push_back (x); }} FIND_SCC (n); if (scc_cnt==1) {printf ("1\n0\n"); return 0; }//indent, statistic the out and in degrees of each point int in[maxn], OUT[MAXN]; memset (in, 0, sizeof in); memset (out, 0, sizeof out); For (i=0, i<n; ++i) for (j=0; J<g[i].size (); ++j) {int v = g[i][j]; if (belong[i]! = Belong[v]) {out[belong[i]]++; in[Belong[v]]++; }} int in_tot = 0, Out_tot = 0; for (I=1; i<=scc_cnt; ++i) {if (!in[i]) in_tot++; if (!out[i]) out_tot++; } printf ("%d\n%d\n", In_tot,max (In_tot,out_tot)); return 0;} The number of/*by Kuangbin strong connected components and the number of components with a degree of 0 are 0. The problem is: N (2<n<100) Each school has one-way network. After each school gets a set of software. Able to transfer to neighboring schools via a one-way network, question 1: At least how many schools are initially required to distribute software, so that all schools in the network can finally get the software. 2, at least several transmission lines (sides) need to be added. Make the software freely distributed to a school. Passed several times. All the schools in the network can finally get the software. That is: Given a graph, ask: 1 at least choose a few vertices, talent to do from these vertices. Able to reach all vertices 2) at least how many edges to add, the ability to start from whatever vertex. Can reach all vertex vertices number <= 100 problem-solving ideas: 1. Find all the strongly connected components 2. Each of the strongly connected components is shrunk to a point, forming a directed acyclic graph Dag.3. The DAG has a number of vertices with an entry level of 0. The answer to question 1 is how many sides to add on the DAG. Talent makes the DAG become strong connectivity, the answer to question 2 is how many add edge method: To each of the points of 0 to join the edge, for each point of the 0 points added out of the edge assumed to have n degrees 0 points, m out of 0 points, how to add edge? Number of points with all degrees 0 0,1,2,3,4 .... N-1 each time for a number I in the degree of 0 points can reach the out of 0 points, add an out edge, connected to the number is (i+1)%N that out of 0 points, which need to add n side if M <= N, then add this n edge, there is no degree 0 points. Then the problem is solved, adding a total of n edges if M > N. Then there are m-n 0 points, then take a point from outside these points, and these points are connected to the top, you can, this also need to add m-n edge. So, Max (M,n) is the solution to the second problem. In addition: when there is only one strong connected branch, it is only a point after the indentation point, although there is a degree of 0, there is one, but actually do not need to add the list of items, so the answer is 1. 0;*//*input:3018 07 21 01 4 15 28 09 010 15 16 022 26 01 5 10 12 03 17 29 02 5 17 019 23 020 01 7 15 19 0023 0005 18 007 1 8 017 024 013 026 002 9 each in Geneva 014 0028 0output:36*/
poj1236 Network of schools, a forward graph for strong connected components (Tarjan algorithm), pinch point