Title: POJ 1236 Network of schools
Similar topics hdoj 2767 3836
/******* the following Kuang explanation of the Great God, written very well does not explain the *************************/
The number of 0 and the number of degrees of 0 for the indentation of strongly connected components
The main topic:N (2<n<100) Each school has a one-way network, each school gets a set of software, can be transmitted through a one-way network to the surrounding schools, question 1: At least how many schools need to initially distribute software, So that all the schools in the network can finally get the software. 2, at least a few transmission lines ( edges )need to be added, so that after any software release to a school, after several transfers, all the schools in the network will eventually get the software.
That is
- given a direction graph, ask:
1) Select at least a few vertices to be able to proceed from these vertices to reach all vertices
2) at least how many edges must be added to allow all vertices to be reached from any vertex
- number of vertices <=
Problem Solving Ideas:
- 1. Find all the strongly connected components
- 2. Each strongly connected component is shrunk to a point, forming a directed acyclic graph DAG.
- 3. how many vertices in the DAG have a 0 in degrees, and the answer to question 1 is
To add a few edges on a dag to make the dag Strong, the answer to question 2 is how much
Ways to add Edges:
To add an in edge for each point with a degree of 0 , add an edge for each point with a degree of 0
Suppose there are N points with an entry degree of 0 , and a point with anm -out of 0 , how do I add edges?
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 of (i+1)%N of the 0 points ,
This needs to add n edges.
If m <= n, then
Add this n edge, no more than 0 points, then solve the problem, add a total of n Edge
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 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, that is, only one point after the indentation, although the degree of 0 of the degree is one, but actually do not need to increase the list of items, so the answer is 1,0;
AC Code Hdoj3676:
#include <cstdio> #include <vector> #include <iostream> #include <stack> #include <cstring >using namespace Std;const int N = 25000;vector<int> G[n];int pre[n],lowlink[n],sccno[n],dfs_clock,scc_cnt; Sccno "I" I where the SCC diagram number//scc_cnt the number of unicom blocks stack<int> s;void dfs (int u) {pre[u] = lowlink[u] = ++dfs_clock; S.push (U); for (int i=0; i<g[u].size (); i++) {int v=g[u][i]; if (!pre[v]) {DFS (v); Lowlink[u]=min (Lowlink[u],lowlink[v]); } else if (!sccno[v]) {lowlink[u]=min (lowlink[u],pre[v]); }} if (Lowlink[u]==pre[u]) {scc_cnt++; for (;;) {int x=s.top (); S.pop (); sccno[x]=scc_cnt; if (x==u) break; }}}void FIND_SCC (int n) {dfs_clock=scc_cnt=0; memset (sccno,0,sizeof (SCCNO)); memset (pre,0,sizeof (pre)); for (int i=0; i<n; i++) if (!pre[i]) DFS (i);} int in[N],out[n];int Workout (int N) {if (scc_cnt = = 1) return 0; memset (in, 0, sizeof (int) * (n+1)); memset (out, 0, sizeof (int) * (n+1)); for (int u = 0, u < n; u++) {for (int i = 0; i < g[u].size (); i++) {int v = g[u][i]; if (sccno[u]! = Sccno[v]) {In[sccno[v]] + = 1; Out[sccno[u]] + = 1; }}} int c1 = 0, c2 = 0; for (int i = 1; I <= scc_cnt; i++) {if (in[i] = = 0) C1 + = 1; if (out[i] = = 0) C2 + = 1; } return Max (c1, c2);} int main () {int n,m,t; while (~SCANF ("%d%d", &n,&m)) {for (int i=0;i<m;i++) {int x, y; scanf ("%d%d", &x,&y); G[x-1].push_back (y-1); } FIND_SCC (n); printf ("%d\n", Workout (n)); for (int i=0;i<n;i++) g[i].clear (); } return 0;}
POJ 1236 Network of schools "strong connectivity graph"