Network of Schools (强连通分量+缩点)

A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B 
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will 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 an integer N: the number of schools in the network (2 <= N <= 100). The schools are 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 ends with a 0. An empty list contains a 0 alone in the line.
Output
Your program should write two 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
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2

题目大概:

每个学校都可以把软件复制好,交给它名单上的学校。

问题A:把软件复制成几份,然后交给不同的学校,所有学校才能够都有软件。

问题B:添加几条边,能使得这个图变成强连通图。

思路:

找出所有的强连通分量,然后缩点,变成一个新有向无环图,求每个强连通分量的入度和出度。

A:入度是0的点就是复制的最小数量,因为只要给强连通分量一个软件,他就会传到每个点,所以找出没有入口的强连通分量的数量就是要复制的数量(其他强连通分量都可以由这些分量传递过去)。

B:in0为入度为0点的数量,out0出度为0的点的数量,添加的边的数量是=max(in0,out0);这个不好证明,但画一下图,自己理解一下,也是很容易理解的。

代码:

#include 
#include 
#include 
#include 
#include 
#define MA 10000+5
using namespace std;

vectorg[MA];
int dfn[MA],low[MA];
int sccno[MA],scc_cnt;
int n,m;
int dfs_clock;
stacks;
int in[105],out[105];
void getmap()
{
    int ans=0;int q;
    for(int i=0;i


你可能感兴趣的:(有向图的强连通分量)