Description

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

Input

第一行包括一个整数 N:网络中的学校数目(2 <= N <= 100)。学校用前 N 个正整数标识。
接下来 N 行中每行都表示一个接收学校列表(分发列表)。第 i+1 行包括学校 i 的接收学校的标识符。每个列表用 0 结束。空列表只用一个 0 表示。

Output

输出两行。第一行应该包括一个正整数:子任务 A 的解 。
第二行应该包括子任务 B 的解。

Sample Input

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

Hint

Source

USACO
强连通分量, 连通性

 

A 即为tarjan 缩点后的 DAG 上所有入度为 0 的点的数目。因为若点X入度为 0 ,则他不能从其他点处获得软件,所以必须给他一份软件。

B 即为 添加多少条边后,DAG 可变为一个环。感性的理解一下就是 max(入度为0的点,出度为0的点)

 #include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define file(a) freopen(a".in","r",stdin); freopen(a".out","w",stdout); inline int gi()
{
bool b=; int r=; char c=getchar();
while(c<'' || c>'') { if(c=='-') b=!b; c=getchar(); }
while(c>='' && c<='') { r=r*+c-''; c=getchar(); }
if(b) return -r; return r;
} const int inf = 1e9+, N = , M = ;
int n,num,f[N],dfn[N],low[N],bl[N],cnt,siz[N],Deep,cd[N],rd[N];
bool b[N];
struct data
{
int nx,fr,to;
}da[M];
stack <int> s; inline void add (int fr,int to)
{
da[++num].fr=fr, da[num].to=to, da[num].nx=f[fr], f[fr]=num;
} inline void tarjan (int o)
{
dfn[o]=low[o]=++Deep;
s.push (o); b[o]=;
int i,to;
for (i=f[o]; i; i=da[i].nx)
{
to=da[i].to;
if (!dfn[to]) tarjan (to), low[o]=min (low[o],low[to]);
else if(b[to]) low[o]=min (low[o],dfn[to]);
}
if (low[o] == dfn[o])
{
cnt++;
do { to=s.top(), s.pop(), b[to]=, bl[to]=cnt; } while (to != o);
}
} int main()
{
// file("schlnet");
n=gi();
int i,x,y;
for (i=; i<=n; i++)
{
x=gi();
while (x) add (i,x), x=gi();
}
for (i=; i<=n; i++) if (!dfn[i]) tarjan (i);
if (cnt == ) { puts ("1\n0"); return ; }
for (i=; i<=num; i++)
{
x=bl[da[i].fr], y=bl[da[i].to];
if (x != y) cd[x]++, rd[y]++;
}
x=, y=;
for (i=; i<=cnt; i++)
{
if (!rd[i]) x++;
if (!cd[i]) y++;
}
printf("%d\n%d\n",x,max (x,y));
return ;
}

POJ 1236 Network of Schools (校园网)的更多相关文章

  1. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  2. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  3. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  4. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  5. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  6. [tarjan] poj 1236 Network of Schools

    主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K To ...

  7. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  8. POJ 1236——Network of Schools——————【加边形成强连通图】

    Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

  9. [强连通分量] POJ 1236 Network of Schools

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16803   Accepted: 66 ...

  10. POJ 1236 Network of Schools (Tarjan + 缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 48 ...

随机推荐

  1. android实现通知栏消息

    一.原理 消息推送有两种,一种是客户端定时直接到服务器搜索消息,如果发现有新的消息,就获取消息下来:另一种是服务器向客户端发送消息,也就是当有信息消息时,服务器端就会向客户端发送消息. 二.步骤(代码 ...

  2. go1.11新特性,mark一下

    包管理新特性: export GO111MODULE=on #开启modules go mod init # 创建go.mod (我是在项目根目录下输入的命令) ls // 可以看下创建成功 cat ...

  3. 关于整合spring+mybatis 第二种方式

    和第一种方式一样的步骤,不过bean.xml中有些许差异 <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory& ...

  4. Java中Arrays类与Math类

    Arrays(数组工具类) Java中已经封装好的类,提供大量静态方法供用户对数组的使用. 导包:import java.util.Arrays 1.Arrays.toString(数组) //返回值 ...

  5. ORACLE 内部原理

    http://www.ohsdba.cn/index.php?m=Article&a=index&id=46 内部原理 2016-05-04• 如何使用BBED 2016-04-16• ...

  6. 1054. 求平均值 (20)-PAT乙级真题

    今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...

  7. [AngularJS + Unit Testing] Testing a component with requiring ngModel

    The component test: describe('The component test', () => { let component, $componentController, $ ...

  8. 推断dxf文件的版本号

    打开DXF參考手冊,在DXF參考手冊中,点击"索引"-->输入"HEADER",在ACADVER字段有acd的版本号信息: 以下是用C语言,写的推断dxf ...

  9. Drozer

    Drozer原名mercury,是一款不错的Android APP安全评估工具.现在有社区版/专业版两个版本 本教程针对于Windows平台下drozer的安装与使用.使用该工具需要JDK的支持,所以 ...

  10. Android--向SD卡读写数据

    // 向SD卡写入数据 private void writeSDcard(String str) { try { // 推断是否存在SD卡 if (Environment.getExternalSto ...