POJ 1236 Network of schools strong connectivity graph

Source: Internet
Author: User
Tags in degrees

Description

A number of schools is connected to a computer network. Agreements has been developed among those Schools:each School maintains a list of schools to which it distributes Softwa Re (the "Receiving schools"). Note that if B was in the distribution list of school A, then a does not necessarily appear in the list of school B
You is to write a program this computes the minimal number of schools that must receive a copy of the new software in Ord Er for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure is sending the copy of new software to an arbitrary school, this software would r Each of the schools in the network. To achieve this goal we are having to extend the lists of receivers by new members. Compute the minimal number of extensions that has to is made so this whatever school we send the new software to, it'll Reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.

Input

The first line contains a integer n:the number of schools in the network (2 <= N <= 100). The schools is identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school I. Each list is ends with a 0. An empty list contains a 0 alone in the line.

Output

Your program should write, lines to the standard output. The first line should contain one positive integer:the solution of subtask A. The second line should contain the solution of subtask B.

Sample Input

52 4 3 04 5 0001 0

Sample Output

12

Graph theory topic, need to solve the problem:
1 using Tarjan algorithm to find sub-strong connected graphs

2 Identify which sub-strong connected graph the vertex belongs to

3 Calculating the 0 in degrees and 0 degrees of each sub-strong connected graph

Graphic theory of advanced content, is a bit difficult, not careful a bit will certainly be wrong.


This time Benbow the Lord carefully annotation good almost every sentence, hoped that everybody can follow my program.

#include <cstdio> #include <stack> #include <cstring> #include <algorithm> #include <vector >using namespace Std;const int max_n = 101;//Maximum number of vertices vector<int> graadj[max_n];//vector represents adjacency table method int visno[max_n];/ /Record the access order of each vertex in the deep search the lowest identification number of the int lowlink[max_n];//connected graph, and if so, whether it is recursive to the vertex that has already been visited, and if so, it is labeled as the lowest vertex access order, so that the label of the sub-connected graph can be unified. By judging whether the identification number and access order number of the current lowest connected graph are consistent, we can determine if a sub-strong connected graph is found. int dfsno;//record deep Search Total access number int connectno;//The number of the current sub-strong connected graph, and finally the quantity of all sub-strong connected graphs int markno[ MAX_N];//MARKNO[V] Represents the vertex v is a sub-strong connected graph Markno[v], the value of which is the sub-strong connected figure int In[max_n], out[max_n];//recorded a sub-strong connected graph in degrees and degrees stack<int> stk;//Deep search vertex into the stack, find the sub-strong connected graph when the stack, until the current vertex number, all points belong to the same sub-strong connectivity diagram//Deep Search to find sub-strong connectivity graph, and record good vertex belongs to which sub strong connectivity diagram void getstrongconnected (int u) {visno[u ] = Lowlink[u] = ++dfsno;//The initial value of the first entry to the current vertex int n = (int) graadj[u].size (); Stk.push (U); for (int i = 0; i < n; i++)//traverse current vertex All connection Points {int v = graadj[u][i];if (!visno[v])//not accessed {getstrongconnected (v);//recursive Lowlink[u] = min (Lowlink[u], lowlink[v ]);//record lowest sequence number}//has been visited, but still in the stack, that is, there is no record that vertex belongs to which strong connected graph else if (!markno[v]) lowlink[u] = min (lowlink[U], lowlink[v]);} if (visno[u] = = Lowlink[u])//The current access order number is equal to the lowest label, {//Then a sub-strong connected graph is found ++connectno;//each time to increase the global connectivity designator int v;do{v = Stk.top (); Stk.pop (); Markno[v] = connectno;//vertex pair with strongly connected graph number} while (U! = v);}} void Tarjan (int n) {//Pre-Qing 0 Works dfsno = 0, Connectno = 0;fill (Visno, visno+n+1, 0); Fill (lowlink, lowlink+n+1, 0); Fill (Markno, M arkno+n+1, 0), while (!stk.empty ()) Stk.pop (), for (int u = 1; u <= n; u++) {//Some vertices may be detached, that is, the vertices of the graph are unconnected, so traverse all vertices if (!visno[ U]) getstrongconnected (U);}} int main () {int n, u, v;scanf ("%d", &n), for (U = 1; u <= N; u++) {scanf ("%d", &v) and while (v)//zero = end {Graadj[u].pus H_back (v);//use vector to create an adjacency table scanf ("%d", &v);}} Tarjan (N);//Calculate the number of strong connected graphs and show which sub-strong connected graph for each vertex belongs to (U = 1; u <= N; u++) {//traverse all vertices and traverse the adjacent edges of vertices, equivalent to traverse all edges for (int i = 0; i < int ) Graadj[u].size (); i++) {int v = graadj[u][i];if (markno[u]! = Markno[v])//does not belong to the same sub-strong connected graph {//increases the penetration and exit of the strongly connected graph respectively out[markno[u]]++;in[markno[v]]+ +;}}} int zeroin = 0, zeroout = 0;for (int i = 1; I <= connectno; i++) {if (in[i] = = 0) zeroin++;if (out[i] = = 0) zeroout++;} In the degree of zero you need to place a copy of the software printf ("%d\n", zeroin);//into a strong connected graph, in 2 cases: 1 is a strong connected graph, 20 in or out maximum printf ("%d\n", Connectno = = 1? 0:max ( Zeroin, Zeroout)); return 0;}



POJ 1236 Network of schools strong connectivity graph

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.