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. ZOJ 3717 二分+2-sat判定。

    好久没有2-sat了,此题当复习之用,二分求最大值+2-sat判断可行,此题主要跪于题意:The results should be rounded to three decimal places. ...

  2. LeetCode:926. 将字符串翻转到单调递增

    暴力法超时:思想:动态规划 public int minFlipsMonoIncrb(String S) { int result = S.length(); for (int i = 0; i &l ...

  3. grafana结合influxdb、open-falcon出图配置

    1.https://www.jianshu.com/p/fadcf4d92b0e 2.https://www.jianshu.com/p/21ce6ee143f3 3.http://www.super ...

  4. aSmack连接server异常smack.SmackException$ ConnectionException thrown by XMPPConnection.connect();

    以下是我在研究asmack4.0出现的异常 06-17 12:02:56.924: W/System.err(10622): org.jivesoftware.smack.SmackException ...

  5. centos 7 -- Disk Requirements: At least 134MB more space needed on the / filesystem.

    用了幾年的centos7,今天執行yum update時,彈出一行有錯誤的提示:Disk Requirements:   At least 134MB more space needed on the ...

  6. 怎样改动X-code中的字体大小、颜色

  7. ZOJ ACM 1314(JAVA)

    昨天做了几个题目.过于简单,就不在博客里面写了. 1314这道题也比較简单,写出来是由于我认为在这里有一个小技巧,对于时间复杂度和空间复杂度都比較节省. 这个题目类似哈希表的求解.可是更简单.刚拿到题 ...

  8. Java 递归解决 &quot;仅仅能两数相乘的计算器计算x^y&quot; 问题

    /** * 求一个数的乘方 * 求x^y,y是一个正整数. 设计算器仅仅能计算两数相乘,不能一次计算n个数相乘. * 知:2^5=(2^2)^2*2; 2^6=(2^2)^3=((4)^2)*4; 2 ...

  9. Missing &#39;name&#39; key attribute on element activity at AndroidMan

    <uses-permission android:content="android.permission.CHANGE_WIFI_STATE" /> 这是android ...

  10. UWP 新手教程2——怎样实现自适应用户界面

    系列文章 UWP新手教程1--UWP的前世今生 如上文所说的,布局面板依据可用的屏幕空间.指定界面元素的大小和位置. 比如StackPanel 会水平或垂直排列界面元素.Grid 布局与CSS 中的表 ...