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

1,一个有向图,问最少选择几个点做源头,使得可以通过它们到达所以点(一个点可以到达任意多个后序点)------求入度为0的点数。

2,一个有向图,问最少加几条边后强连通------求max(入度为0的点数,出度为0的点数),之前证明过。但是需要注意已经强连通的情况下会出错(需要特判)。

3,一个有向图,问最少选择几个点做源头,使得可以通过它们到达所以点(一个点可以到达一个后序点)------缩点后用最小路径覆盖(二分图匹配)

此题只求1,2两个问,一定要注意特判。

//有向图缩点 ,注意scc_cnt=1时。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn*],To[maxn*],cnt,n;
int dfn[maxn],low[maxn],times,scc_cnt,scc[maxn];
int instc[maxn],stc[maxn],top,ans1,ans2;
int ind[maxn],oud[maxn];
void update()
{
cnt=times=scc_cnt=top=ans1=ans2=;
memset(Laxt,,sizeof(Laxt));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(scc,,sizeof(scc));
memset(instc,,sizeof(instc));
memset(stc,,sizeof(stc));
memset(ind,,sizeof(ind));
memset(oud,,sizeof(oud));
}
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void dfs(int u)
{
dfn[u]=low[u]=++times;
stc[++top]=u; instc[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(!dfn[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(instc[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
scc_cnt++;
while(true){
int x=stc[top--];
scc[x]=scc_cnt;
instc[x]=;
if(x==u) break;
}
}
}
void tarjan()
{
for(int i=;i<=n;i++)
if(!dfn[i]) dfs(i);
for(int i=;i<=n;i++)
for(int j=Laxt[i];j;j=Next[j]){
if(scc[i]!=scc[To[j]]) {
ind[scc[To[j]]]++;
oud[scc[i]]++;
}
} for(int i=;i<=scc_cnt;i++){
if(ind[i]==) ans1++;
if(oud[i]==) ans2++;
}
}
int main()
{
while(~scanf("%d",&n)){
update();
for(int i=;i<=n;i++) {
int x; while(scanf("%d",&x)){
if(x==) break;
add(i,x);
}
}
tarjan();
if(scc_cnt==) printf("1\n0\n");
else printf("%d\n%d\n",ans1,ans2);
} return ;
}

POJ1236 Network of Schools (强连通分量,注意边界)的更多相关文章

  1. poj-1236.network of schools(强连通分量 + 图的入度出度)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27121   Accepted: 10 ...

  2. POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度

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

  3. Network of Schools(强连通分量缩点(邻接表&矩阵))

    Description A number of schools are connected to a computer network. Agreements have been developed ...

  4. [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)

    nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...

  5. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  6. Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13801   Accepted: 55 ...

  7. POJ 1236 Network of Schools (强连通分量缩点求度数)

    题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...

  8. P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools

    P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学 ...

  9. [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)

    题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...

随机推荐

  1. GOPATH设置

    go help gopath查看gopath的原文帮助信息 go env查看gopath的配置 GOPATH与工作空间 前面我们在安装Go的时候看到需要设置GOPATH变量,Go从1.1版本到1.7必 ...

  2. Android自己定义控件

    今天我们来讲一下 Android中自己定义控件的介绍,在Android中, 我们一般写xml都是用的是单个的控件来完毕的 ,但是.往往在一些项目中.单个控件有时是满足不了的.故此我们能够自己定义控件 ...

  3. 解题报告 之 HDU5288 OO&#39; s Sequence

    解题报告 之 HDU5288 OO' s Sequence Description OO has got a array A of size n ,defined a function f(l,r) ...

  4. mysql 查看当前连接数

    http://www.cnblogs.com/pcdelphi/archive/2009/10/31/2017990.html   实战经验: >登录到mysql数据库的终端 >show ...

  5. PHP工作模型与运行机制

    PHP的工作模型非常特殊.从某种程度上说,PHP和ASP.ASP.NET.JSP/Servlet等流行的Web技术,有着本质上的区别.   以Java为例,Java在Web应用领域,有两种技术:Jav ...

  6. Ejb in action(六)——拦截器

    Ejb拦截器可以监听程序中的一个或全部方法.与Struts2中拦截器同名,并且他们都可以实现切面式服务.同一时候也与Spring中的AOP技术类似. 不同的是struts2的拦截器的实现原理是一层一层 ...

  7. 华为P7电信4G版刷机包 EMUI2.3 官方B125 第3版 精简 ROOT

    ROM介绍 基于底包至 B125 SP03解包制作 增加自己订制的超美丽EMUI 2.3专用的全局主题 自调刷机脚本,全部权限完美百分百与官方原版相贴合. 加入Root权限并使用SuperSU 2.0 ...

  8. 2016年第七届蓝桥杯c/c++省赛B组

    2016年第七届蓝桥杯c/c++省赛B组 声明:以下答案是我自己做的.不能保证正确,须要參考正确答案的请到其它地方找. 第一题 :煤球数目 题目叙述: 有一堆煤球,堆成三角棱锥形.详细: 第一层放1个 ...

  9. Windows 7 &amp; Ubuntu 14.04完美双系统安装及系统引导配置----校园网Mentohust配置

    本文写于完美安装双系统之后,所以图片会不全然.主要目的是总结下注意事项.备用. 一.Win7-64-旗舰版U盘安装 win7-64-旗舰版纯净版下载,下载安装后仅仅有1个驱动人生! 附刻盘工具激活工具 ...

  10. iperf3 测试路由器吞吐率

    mini newifi 电脑端: iperf3 -s 路由器: root@OpenWrt:/# iperf3 -c 10.10.10.3 -t 20 Connecting to host 10.10. ...