[Poj1236]Network of Schools
2015-11-14 15:29
218 查看
传送门
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.
相关文章推荐
- 2015noip pj 总结&反思
- CString 转 String和String转CString
- Objective-C 实例方法可见度,方法
- DOM
- 元素移动
- 一个android的各种控件库
- c++ 实验探究之数组
- 上下班同行有利于婚姻美满?
- Java小程序之GUI开发简单前台登录界面
- JavaScript中__proto__与prototype的关系
- 2.抽取代码(BaseActivity)
- 2342432432
- 修改Sybase数据库的默认字符集为CP936
- apache配置网络驱动器
- Android实现TextView字符串波浪式跳动
- 负反馈简要说明
- HDU OJ1061 Rightmost Digit
- Objective-C中类属性的 copy, retain, assign , readonly , readwrite, nonatomic区别
- Deep Learning 9_深度学习UFLDL教程:linear decoder_exercise(斯坦福大学深度学习教程)
- iOS开发学习之触摸事件和手势识别