P2746 [USACO5.3]校园网Network of Schools

来源:互联网 发布:解忧杂货店淘宝 编辑:程序博客网 时间:2024/06/05 02:42

题目描述

一些学校连入一个电脑网络。那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”)。注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中。你要写一个程序计算,根据协议,为了让网络中所有的学校都用上新软件,必须接受新软件副本的最少学校数目(子任务 A)。更进一步,我们想要确定通过给任意一个学校发送新软件,这个软件就会分发到网络中的所有学校。为了完成这个任务,我们可能必须扩展接收学校列表,使其加入新成员。计算最少需要增加几个扩展,使得不论我们给哪个学校发送新软件,它都会到达其余所有的学校(子任务 B)。一个扩展就是在一个学校的接收学校列表中引入一个新成员。

输入格式:

输入文件的第一行包括一个整数 N:网络中的学校数目。学校用前 N 个正整数标识。接下来 N 行中每行都表示一个接收学校列表。第 i+1 行包括学校 i 的接收学校的标识符。每个列表用 0 结束。空列表只用一个 0 表示。

输出格式:

你的程序应该在输出文件中输出两行。第一行应该包括一个正整数:子任务 A 的解。第二行应该包括子任务 B 的解。

输入样例#1:

52 4 3 04 5 0001 0

输出样例#1:

12

思路

首先缩点,再分别统计缩点后的图中入度和出度为0的点数。对于任务A,我们只需要输出入度为0的点数。对于任务B,输出max(入度为0的点数,出度为0的点数)。需要注意的是当整个图缩为一个点的情况。此时不用额外连边。
type  arr=record        next,x,y:longint;      end;var  n,m,tot,e,b,w:longint;  ls,stack,low,dfn,d:array[1..100] of longint;  g:array[1..100] of arr;  instack:array[1..100] of boolean;  f:array[1..100,1..100] of boolean;procedure init;var  i,x,y,t:longint;begin  readln(n);w:=0;  for i:=1 to n do    begin      read(t);      while t<>0 do        begin          inc(w);          f[i,t]:=true;          g[w].x:=i;          g[w].y:=t;          g[w].next:=ls[g[w].x];          ls[g[w].x]:=w;          read(t);        end;    end;end;procedure tarjan(i:longint);var  j,t:longint;begin  inc(e);  dfn[i]:=e;  low[i]:=e;  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);               if low[i]>low[j] then low[i]:=low[j];             end        else if instack[j] and (dfn[j]<low[i])                then low[i]:=dfn[j];       t:=g[t].next;    end;  if dfn[i]=low[i] then     begin       inc(b);       repeat         j:=Stack[tot];         instack[j]:=false;         dec(tot);         d[j]:=b;       until i=j;    end;end;procedure solve;var  i:longint;begin  tot:=0;  b:=0;  e:=0;  fillchar(dfn,sizeof(dfn),0);  for i:=1 to n do    if dfn[i]=0 then  tarjan(i);end;var  i,a,aa:longint;  r,c:array[1..100] of longint;begin  init;  solve;  for i:=1 to b do    begin      r[i]:=1;      c[i]:=1;    end;  for i:=1 to w do    if d[g[i].y]<>d[g[i].x] then      begin        r[d[g[i].y]]:=0;        c[d[g[i].x]]:=0;      end;  for i:=1 to b do    begin      a:=a+r[i];      aa:=aa+c[i];    end;  writeln(a);  if b=1 then writeln(0)    else if a>aa then writeln(a) else writeln(aa);end.
原创粉丝点击