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 实例名/服务名 请问SID和Service_Name有什么区别

    可以简单的这样理解:一个公司比喻成一台服务器,数据库是这个公司中的一个部门. 1.SID:一个数据库可以有多个实例(如RAC),SID是用来标识这个数据库内部每个实例的名字, 就好像一个部门里,每个人 ...

  2. 这一篇里面有很多关于scala的list的操作的好的知识

    https://www.cnblogs.com/weilunhui/p/5658860.html 1.++[B]   在A元素后面追加B元素 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  3. Apache和IIS共享80端口的四个设置方法

    方法一:IIS5,多IP下共存,IIS为192.168.0.1,apache为192.168.0.2c:\Inetpub\Adminscriptscscript adsutil.vbs set w3s ...

  4. Dubbo zookeeper 初探

    先把zookeeper在本地给安装好, 安装方法参考:http://blog.csdn.net/wxwzy738/article/details/16330253 这里的话讲述了两个工程一个工程是提供 ...

  5. 【matlab】:matlab中不断的出现计算过程怎么办

    这个问题是会常常性出的.就是matlab中不断的出现计算. 关于这个问题,我们须要考虑的是自己是不是写错了,通常会出现以下两种可能的错误 1,关于计算的函数没有写分号 :这样的是致命问题,假设函数不写 ...

  6. C#网络编程系列文章(一)之Socket实现异步TCPserver

    原创性声明 本文作者:小竹zz 本文地址http://blog.csdn.net/zhujunxxxxx/article/details/44258719 转载请注明出处 文章系列文件夹 C#网络编程 ...

  7. javascript 怎么操纵OGNL标签

    吧ONGL标签放到html标签中,来操作html的标签就能够了 样例代码: html <div id="categoryid" style="display:non ...

  8. Html5学习笔记1 元素 标签 属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. [转]si设置

    好吧,我有代码格式的强迫症,代码不整齐,我看的都头疼,之前一直喜欢用SourceStyler C++的,但是这个在win7下貌似不能使用,只能转向astyle了. http://www.cnblogs ...

  10. [Android5 系列—] 1. 构建一个简单的用户界面

    前言 安卓应用的用户界面是构建在View 和ViewGroup 这两个物件的层级之上的. View 就是一般的UI组件.像button,输入框等. viewGroup 是一些不可见的view的容器,用 ...