当前位置: 首页 > news >正文

Network of Schools_POJ1236_Tarjan

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.

Sample Input

5

2 4 3 0

4 5 0

0

0

1 0

Sample Output

1

2

Source

IOI 1996

题目大意:

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

思路:

假设有1个学校得到了软件,那么与它直接相连或是间接相连的学校都可以通过它获得软件,即1个强连通分量需要1套软件

于是我们就找到了强力的强连通分量算法Tarjan(看名字是不是很强力)

得出每一个学校在哪一个强连通分量里。然后算出入度为0的强连通分量的个数,就是第一个答案

统计入度为0和出度为0的强连通分量个数,较大那个就是答案二

注意如果强连通分量的个数是一个,那么第二个的答案是0

源代码/pas:

 

type
  point=record
  x,y,next:longint;
end;
var
  m,t, maxE:longint;
  e:array[0..15000]of point;
  ls,low,dfn,s,comp:array[0..15000]of longint;
  v:array[0..15000]of boolean;
function min(a,b:longint):longint;
begin
  min:=a;
  if b<a then
  min:=b;
end;
procedure tarjan(x:longint);
var
  i,y:longint;
begin
  inc(t);
  dfn[x]:=t;
  low[x]:=t;
  inc(s[0]);
  s[s[0]]:=x;
  v[x]:=true;
  i:=ls[x];
  while i>0 do
  begin
    y:=e[i].y;
    if not v[y] then
    begin
      tarjan(y);
      low[x]:=min(low[y],low[x]);
    end
    else
    low[x]:=min(low[x],dfn[y]);
    i:=e[i].next;
  end;
  if dfn[x]=low[x] then
  begin
    inc(comp[0]);
    repeat
      y:=s[s[0]];
      comp[y]:=comp[0];
      dec(s[0]);
    until x=y;
  end;
end;
procedure main;
var
  i:longint;
begin
  for i:=1 to m do
  begin
    t:=0;
    if not v[i] then
    tarjan(i);
  end;
end;
procedure print;
var
  i,x,y:longint;
  indeg,oudeg:array[0..15000]of longint;
begin
  x:=0;
  y:=0;
  fillchar(indeg,sizeof(indeg),0);
  fillchar(oudeg,sizeof(oudeg),0);
  if comp[0]=1 then
  begin
    writeln(1);
    writeln(0);
    exit;
  end;
  for i:=1 to maxE do
  if (comp[e[i].x]<>comp[e[i].y]) then
  begin
    inc(oudeg[comp[e[i].x]]);
    inc(indeg[comp[e[i].y]]);
  end;
  for i:=1 to comp[0] do
  if indeg[i]=0 then inc(x);
  for i:=1 to comp[0] do
  if oudeg[i]=0 then inc(y);
  writeln(x);
  writeln(x+y-min(x,y));
end;
procedure init;
var
  i,j:longint;
begin
  readln(m);
  for i:=1 to m do
  begin
    read(j);
    while j<>0 do
    begin
      inc(maxE);
      with e[maxE] do
      begin
        x:=i;
        y:=j;
        next:=ls[x];
        ls[x]:=maxE;
      end;
      read(j);
    end;
  end;
end;
begin
  init;
  main;
  print;
end.


转载于:https://www.cnblogs.com/olahiuj/p/5781312.html

相关文章:

  • 18121 排排坐看电影
  • 编辑中
  • html5 历史管理
  • fopen()函数以a+方式打开一个不存在的文件后读写出现问题
  • 第五章
  • Android Studio自定义注释模板
  • 觉得很有用存一份
  • Grails用CONSOLE测试,比写集成测试还快
  • 186.Reverse Words in a String II
  • 用C#代码来安装、卸载、启动、关闭服务
  • 《java入门第一季》之TreeSet存储自定义对象并保证排序和唯一
  • 创建模仿存储库 Making a Mock Repository 精通ASP-NET-MVC-5-弗瑞曼 Listing 7-5
  • 《训练指南》——6.7
  • BadgeValueView
  • 64位win7下安装SQL Server 2008(图文解说版)
  • 时间复杂度分析经典问题——最大子序列和
  • 0x05 Python数据分析,Anaconda八斩刀
  • Cookie 在前端中的实践
  • CSS魔法堂:Absolute Positioning就这个样
  • E-HPC支持多队列管理和自动伸缩
  • ES6核心特性
  • LeetCode算法系列_0891_子序列宽度之和
  • Map集合、散列表、红黑树介绍
  • Material Design
  • React-生命周期杂记
  • Sequelize 中文文档 v4 - Getting started - 入门
  • spring-boot List转Page
  • SQLServer之索引简介
  • 闭包,sync使用细节
  • 订阅Forge Viewer所有的事件
  • 聊聊redis的数据结构的应用
  • 模型微调
  • 山寨一个 Promise
  • 算法之不定期更新(一)(2018-04-12)
  • 跳前端坑前,先看看这个!!
  • 译有关态射的一切
  • 用jquery写贪吃蛇
  • MPAndroidChart 教程:Y轴 YAxis
  • # 睡眠3秒_床上这样睡觉的人,睡眠质量多半不好
  • (6)设计一个TimeMap
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (二十三)Flask之高频面试点
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (六)软件测试分工
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • (四) 虚拟摄像头vivi体验
  • (五)MySQL的备份及恢复
  • (总结)Linux下的暴力密码在线破解工具Hydra详解
  • ./和../以及/和~之间的区别
  • .NET实现之(自动更新)
  • .NET中两种OCR方式对比
  • /bin、/sbin、/usr/bin、/usr/sbin