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. oracle 查看各表空间剩余量

    1.查看所有表空间大小.剩余量: select dbf.tablespace_name,dbf.totalspace "总量(M)",dbf.totalblocks as 总块数, ...

  2. 【Todo】Tomcat与Jetty的比较 以及Tomcat架构的学习

    主要参考这篇文章: http://blog.csdn.net/qing_2012/article/details/8276789 然后Tomcat原理的介绍,可以看这篇文章: http://www.i ...

  3. BumpMapping [转]

    http://fabiensanglard.net/bumpMapping/index.php Fabien Sanglard's Website Home About FAQ Email Rss T ...

  4. CMake使用hellocmake&&make的使用

    2016-12-11   20:38:32 已经知道cmake这个东西很长的时间了,一直没有试验过,知道它是一个编译工具,在opencv和Linux下都有makefile的内容.感觉现在对源码的编译有 ...

  5. apk 签名

    给apk签名步骤:(比方apk名称是EasyMsg.apk) (1)将EasyMsg.apk包后缀改为zip, EasyMsg.zip (2)删除EasyMsg.zip文件包中的META-INF目录, ...

  6. MRP routing设置释疑

    Jeffer9@gmail.com         工艺是指在不同工作中心执行的作业序列         作业的详细信息 Number of cycles 在该工作中心操作几个循环 Number of ...

  7. git+jenkins

    开发写代码的演变 一个开发单打独斗,撸代码,开发网站,自由自在 多个开发同时开发一个网站,同时改一份代码.但是同时改一个文件会导致冲突 分支结构,每天上班第一件事克隆代码,下班前最后一件事合并代码 好 ...

  8. PS 魔法棒

    魔术棒工具是通过选取图像中颜色相近或大面积单色区域的像素来制作选区,魔术棒用于纯色背景中较多. 容差数值越大,选择出的选区就越大,容差越小,对颜色差别的要求也就越严格,选择出的选区也就越小 按住shi ...

  9. C# 通过Hook的方法 屏蔽快捷键

     #region 屏蔽Windows功能键(快捷键)         public delegate int HookProc(int nCode, int wParam, IntPtr lParam ...

  10. 【BZOJ3782】上学路线 组合数+容斥+CRT

    [BZOJ3782]上学路线 Description 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不 ...