poj 1236

Description

A numberof schools are connected to a computer network. Agreements have been developedamong those schools: each school maintains a list of schools to which itdistributes software (the “receiving schools”). Note that if B is in thedistribution list of school A, then A does not necessarily appear in the listof school B  You are to write a program that computes the minimal number of schools thatmust receive a copy of the new software in order for the software to reach allschools in the network according to the agreement (Subtask A). As a furthertask, we want to ensure that by sending the copy of new software to anarbitrary school, this software will reach all schools in the network. Toachieve 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 whateverschool we send the new software to, it will reach all other schools (SubtaskB). One extension means introducing one new member into the list of receiversof one school. 

Input

The firstline contains an integer N: the number of schools in the network (2 <= N<= 100). The schools are identified by the first N positive integers. Eachof the next N lines describes a list of receivers. The line i+1 contains theidentifiers of the receivers of school i. Each list ends with a 0. An emptylist contains a 0 alone in the line.

Output

Yourprogram should write two lines to the standard output. The first line shouldcontain one positive integer: the solution of subtask A. The second line shouldcontain the solution of subtask B.

Sample Input

5

2 4 3 0

4 5 0

0

0

1 0

Sample Output

1

2

 

题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。

 

分析:我们可以用tarjan算法求出连通分量,我们可以把每个连通分量看为一个点,就成了一个新图,第一问求新图有多少个入度为0的个数(如果没有也要输出1)。第二问求的是max(入度个数,出度个数)。

 

const

 MaxV=10000;

 MaxE=10000;

type

  arr=record

       next,x,y:longint;

     end;

var

  n,m,tot,dindex,bcnt:longint;

  ls,stack,low,dfn,belong,c,r:array[1..maxV]of longint;

  g:array[1..MaxE]of arr;

 instack:array[1..maxv] of boolean;

procedure init;

var

  i,s,m:longint;

begin

  readln(n);

  m:=0;

  fori:=1 to n do

   begin

    while 1=1 do

     begin

     read(s);

     if s=0 then break;

     inc(m);

      with g[m] do

       begin

         x:=i; y:=s;

         next:=ls[x];

          ls[x]:=m;

       end;

     end;

   end;

end;

 

function min(x,y:longint):longint;

 begin

  ifx>y then exit(y)

        else exit(x);

 end;

 

function max(x,y:longint):longint;

 begin

  ifx>y then exit(x)

        else exit(y);

 end;

 

procedure tarjan(i:longint);

var j,t:longint;

begin

 inc(dindex);

 low[i]:=dindex;

 dfn[i]:=dindex;

 inc(tot);

 stack[tot]:=i;

 t:=ls[i];

 instack[i]:=true;

 while t>0 do

  begin

   j:=g[t].y;

   if dfn[j]=0 then

    begin

     tarjan(j);

     low[i]:=min(low[i],low[j]);

    end

    else

     if instack[j] then

    low[i]:=min(low[i],dfn[j]);

    t:=g[t].next;

  end;

  if low[i]=dfn[i] then

   begin

    inc(bcnt);

    repeat

    j:=stack[tot];

    instack[j]:=false;

     dec(tot);

    belong[j]:=bcnt;

    until i=j;

   end;

end;

 

procedure solve;

var i:longint;

begin

 dindex:=0;

 bcnt:=0;

 dindex:=0;

 fillchar(dfn,sizeof(dfn),0);

 for i:=1 to n do

  if dfn[i]=0 then

   begin

    tarjan(i);

   end;

end;

 

procedure main;

 varj,i,k:longint;

begin

 init;

 solve;

 for i:=1 to n do

  begin

   j:=ls[i];

   while j<>0 do

    begin

     with g[j] do

     begin

      if belong[x]<>belong[y] then

       begin

        inc(c[belong[x]]);

        inc(r[belong[y]]);

       end;

      j:=next;

     end;

    end;

  end;

 for i:=1 to bcnt do

  if r[i]=0 then j:=j+1;

  writeln(j);

 k:=0;

 for i:=1 to bcnt do

  if c[i]=0 then inc(k);

 if bcnt=1 then

  begin

   write(0);

  halt;

  end;

  writeln(max(k,j));

end;

 

begin

while not seekeof do

 begin

 main;

 end;

end.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值