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. iOS开发中16进制颜色(html颜色值)字符串转为UIColor

    //16进制颜色(html颜色值)字符串转为UIColor +(UIColor *) hexStringToColor: (NSString *) stringToConvert { NSString ...

  2. GridView动态删除Item

    activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  3. ****如何优雅的用Axure装逼?高保真原型心得分享

    本文核心内容点:- 啥是高保真原型?(附简单说明原型)- Axure可以画出什么水准的高保真?(给示例,开启装逼模式)- 高保真原型图技巧:- 啥时候上高保真?适用场景 and 不适用场景 啥是高保真 ...

  4. raspi集成库及安装

    原文:http://blog.csdn.net/xukai871105/article/details/12684617   树莓派来自国外,国外嵌入式开源领域具有良好的分享精神,树莓派各种集成库也层 ...

  5. 12/10 C语言程序设计竞赛 后五题

    Title(题目) 小朋友顺逆报数 Problem ID(题目编号) (题目添加成功后由系统自动生成) Time Limit(运行时间限制) S(秒) Memory Limit(内存限制) MByte ...

  6. 5.JAVA语言基础部分—多线程

    一个应用有一个进程,一个进程里可以用多个线程 1)定义 定义线程有两种方式,一是继承java.lang.Thread类,二是实现java.lang.Runnable接口.其实Thread类就是实现了R ...

  7. Maven错误 diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)问题解决

    如果在Maven构建时出现: diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable d ...

  8. git-flow 工作流 备忘清单

    关于 git-flow 是一个 git 扩展集,按 Vincent Driessen 的分支模型提供高层次的库操作. 查看详情 ★ ★ ★ 这个备忘清单展示了 git-flow 的基本操作和效果. ★ ...

  9. [scrapy]Item Loders

    Items Items就是结构化数据的模块,相当于字典,比如定义一个{"title":"","author":""},i ...

  10. cocos2d-x改底层之获取UIListView的实际内容大小

    实际项目中UI界面中常常会用到UIListView.大多会在CocoStudio中直接加入这个控件. 可是在使用中发现了一些坑和功能缺乏,然后就看了一下底层的逻辑,发现略微改一下底层就能够满足需求,所 ...