poj1236 Network of Schools

Description

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.
题解:

这题大意是给一个有向图,求至少给多少个结点发消息能使消息传遍整个网络,并进一步求出至少添加多少条边能使对图中任意一个结点发消息都能使消息传遍整个网络。这道题就是求学校之间连成网络的强联通分量。

(1).可以先用,得到原图的基图,然后统计入度为0的连通分量个数和出度为0的连通分量个数,入度为0的必须给它发消息,入度不为0的不必给发消息。

(2).第一问,所求即为图中入度为0的个数。

(3).第二问,只需将入度为0的结点与出度为0的结点连接即可满足要求,最少需加边数目为两者之中的较大者,需注意的是,单只有一个连通分量时,输出结果为0 。


代码(节选):

procedure tarjan(i:longint);

var

  j,t:longint;

begin

  inc(s);

  a[i]:=s;

  b[i]:=s;

  inc(temp);

  sum[tot]:=i;

  t:=ls[i];

  while t>0 do

    begin

      j:=g[t].y;

      if a[j]=0 then

        begin

          tarjan(j);

          if b[i]>b[j] then

            b[i]:=b[j];

        end

      else if flag[j] and (a[j]<b[i])

        b[i]:=a[j];

    end;

  if a[i]=b[i] then

    begin

      inc(x);

      repeat

        j:=sum[temp];

        dec(temp);

        flag[j]:=false;

        main[j]:=x;

      until i=j;

    end;

end;



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值