POJ 1236--network of schools "SCC indent composition && Calculate the number of SCC in 0 && ask for at least a few edges to make the graph strong unicom"

Source: Internet
Author: User
Tags in degrees

Network of schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13325 Accepted: 5328

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

The following resolution turns from Bin God's Blog

The number of 0 and the number of degrees of 0 for the indentation of strongly connected components
The main problem: N (2<n<100) schools have 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 the initial number of schools need to distribute 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) 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

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 an M-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;

#include <cstdio> #include <algorithm> #include <cstring> #include <vector> #define MAXN 100 + 100 #define MAXM * 100using namespace std;int N, m;struct node{int u, V, next;}; Node Edge[maxm];int HEAD[MAXN], Cnt;int LOW[MAXN], Dfn[maxn];int dfs_clock;int STACK[MAXN], Top;bool Instack[maxn];int    Belong[maxn];int Scc_clock;int IN[MAXN], out[maxn];int num[maxn];void init () {cnt = 0; Memset (Head,-1, sizeof (head));}    void Addedge (int u, int v) {edge[cnt] = {u, V, Head[u]}; Head[u] = cnt++;}        void Getmap () {for (int i = 1; I <= n; ++i) {int V;        while (scanf ("%d", &v), V) {Addedge (i, v);    }}}void Tarjan (int u, int per) {int V;    Low[u] = dfn[u] = ++dfs_clock;    stack[top++] = u;    Instack[u] = true;        for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V;            if (!dfn[v]) {Tarjan (V, u);        Low[u] = min (Low[u], low[v]); } else if (Instack[v]) Low[u] = Min (Low[u], dfn[v]);        } if (dfn[u] = = Low[u]) {scc_clock++;            do{v = stack[--top];            INSTACK[V] = false;        BELONG[V] = Scc_clock;    } while (V! = u);        }}void Suodian () {for (int i = 1; I <= scc_clock; ++i) {out[i] = 0;    In[i] = 0;        } for (int i = 0; i < cnt; ++i) {int u = belong[edge[i].u];        int v = belong[edge[i].v];            if (U! = v) {out[u]++;        in[v]++;    }}}void Find () {memset (low, 0, sizeof (low));    memset (DFN, 0, sizeof (DFN));    memset (Belong, 0, sizeof (Belong));    memset (stack, 0, sizeof (stack));    Memset (Instack, False, sizeof (false));    Dfs_clock = Scc_clock = top = 0;    for (int i = 1; I <= n; ++i) {if (!dfn[i]) Tarjan (i, I);        }}void Solve () {if (Scc_clock = = 1) {printf ("1\n0\n");    return;    } int numin, numout;    numin = numout = 0; for (int i = 1; I <= scc_clock; ++i) {if (in[i] = = 0) numin++;    if (out[i] = = 0) numout++; } printf ("%d\n%d\n", Numin, Max (Numout, numin));}        int main () {while (scanf ("%d", &n)! = EOF) {init ();        Getmap ();        Find ();        Suodian ();    Solve (); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1236--network of schools "SCC indent composition && Calculate the number of SCC in 0 && ask for at least a few edges to make the graph strong unicom"

Related Article

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.