[Poj1236]Network of Schools

来源:互联网 发布:python多进程共享对象 编辑:程序博客网 时间:2024/05/17 21:54

传送门

http://poj.org/problem?id=1236

题目大意

学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,求:

  • 1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。

  • 2:至少需要添加几条传输线路,使任意向一个学校发放软件后,经过传送,所有学校都能得到软件。

题解

tarjan之后重建图
第一问是求解有多少入度为0的点
第二问是求解将图添边变成一个强连通分量,即没有入度为0且没有出度为0的点的图,将所有出度为0的点连一条边到入度为0的点即可,就是max(出度为0的个数,入度为0的个数)
注意特判整个图一开始就是1个强连通分量

var ru,chu,low,dfn,id,p,t:array[0..200]of longint; w:array[0..20005,1..2]of longint; i,j,k:longint; n,len,scc:longint; a,b,tt,ans1,ans2:longint;function min(a,b:longint):longint;begin if a>b then exit(b) else exit(a);end;procedure init(a,b:longint);begin w[len,1]:=b; if w[a,2]=0 then w[a,2]:=len else w[w[a,1],2]:=len; w[a,1]:=len; inc(len);end;procedure tarjan(a:longint);var tt:longint;begin inc(len); dfn[a]:=len; low[a]:=len; p[a]:=1; inc(t[0]); t[t[0]]:=a; tt:=w[a,2]; while tt<>0 do  begin   if dfn[w[tt,1]]=0   then begin tarjan(w[tt,1]); low[a]:=min(low[a],low[w[tt,1]]); end   else if p[w[tt,1]]=1 then low[a]:=min(low[a],dfn[w[tt,1]]);   tt:=w[tt,2];  end; if dfn[a]=low[a] then begin  inc(scc);  repeat   p[t[t[0]]]:=0;   id[t[t[0]]]:=scc;   dec(t[0]);  until a=t[t[0]+1]; end;end;begin readln(n); len:=n+1; for i:=1 to n do  begin   read(a);   while not eoln do    begin     init(i,a); read(a); if a=0 then break;    end;  end; len:=0; scc:=0; t[0]:=0; for i:=1 to n do  if dfn[i]=0 then tarjan(i); for i:=1 to n do  begin   tt:=w[i,2];   while tt<>0 do    begin     if id[i]<>id[w[tt,1]] then begin inc(ru[id[w[tt,1]]]); inc(chu[id[i]]); end;     tt:=w[tt,2];    end;  end; ans1:=0; ans2:=0; for i:=1 to scc do  begin   if ru[i]=0 then inc(ans1);   if chu[i]=0 then inc(ans2);  end; if scc=1 then begin writeln(1); writeln(0); end else begin writeln(ans1); if ans1>ans2 then ans2:=ans1; writeln(ans2); end;end.
0 0
原创粉丝点击