poj1236 Network of schools, strong connected component (Tarjan algorithm), pinch point

Source: Internet
Author: User
Tags in degrees

Topic Link: Click to open the link


Test instructions

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 <= 100


After the strong connected component is obtained, the indentation point is calculated, and the degree of each point 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;} /*by Kuangbin Strong connected component, the number of the indentation of 0 and the number of the component with a degree of 0 is the problem: N (2<n<100) Each school has a one-way network, each school gets a set of software, can through a one-way network to the surrounding schools transmission, Question 1: Initially, at least how many schools need to be distributed software, so that all schools in the network will eventually 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) At least select a few vertices, to do from these vertices, you can reach all vertices 2) at least how many edges to be added to make from any one vertex, can reach all vertex vertex number <= 100 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 an entry level of 0, the answer to question 1 is how many sides are added to the DAG to make the Dag strong-connected, and the answer to question 2 is how many edges are added: To add an edge to each point with a degree of 0, and to add an edge for each point with an out of 0 to assume that there are points with n degrees in 0. M a point with a degree of 0, 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 (i+1)%N that out of 0 points, which need to add n edge if M <= N, then add this n edge, has no degree 0 points, then the problem solved, altogether added 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, it is necessary 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 one point after the indentation, 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 18 017 024 013 21 026 002 23 30 9 014 0028 0output:36*/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.