Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5354   Accepted: 2106

Description

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

Source

 

题意:

有N个学校...每个学校可能向几个学校进行数据传输,问至少需要把一个文件给几个学校可以使给的N个学校都收到文件,再问在加几个通信线路可以使各个学校之间都能直接或间接的传递文件;

题解:首先跑一发tarjan缩点,

第一问就是问重建图后入度为0的点,如果没有至少是1;

第二问就是max(入度为0,出度为0);

///
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
typedef long long ll;
using namespace std;
#define inf 10000000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//***************************************************************
struct ss
{
int to,next;
} e[**];
int belong[],hav[];
int scc,n,top;
int in[],out[],ansl,ansr;
int head[],dfn[],low[],cnt,t,vis[],q[],inq[];
void init()
{
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(head,,sizeof(head));
top=;
scc=;
cnt=;
t=;
memset(inq,,sizeof(inq));
memset(hav,,sizeof(hav));
memset(vis,,sizeof(vis));
}
void add(int u,int v)
{
e[t].to=v;
e[t].next=head[u];
head[u]=t++;
}
void dfs(int a)
{
int now=;
dfn[a]=low[a]=++cnt;
vis[a]=inq[a]=;
q[++top]=a;
for(int i=head[a]; i; i=e[i].next)
{
if(!vis[e[i].to])
{
dfs(e[i].to);
low[a]=min(low[a],low[e[i].to]);
}
else
{
if(inq[e[i].to])low[a]=min(low[a],dfn[e[i].to]);///在队列中
}
}
if(low[a]==dfn[a])
{
scc++;
while(now!=a)
{
now=q[top--];
inq[now]=;
belong[now]=scc;
++hav[scc];
}
}
}
void rebuild()
{
for(int i=; i<=n; i++)
{
for(int j=head[i]; j; j=e[j].next)
{
if(belong[i]!=belong[e[j].to])
{
out[belong[i]]++;
in[belong[e[j].to]]++;
}
}
}
int A=,B=;
for(int i=; i<=scc; i++)
{
if(in[i]==)A++;
if(out[i]==)B++;
}
ansr=A;
ansl=max(A,B);
}
void tarjan()
{
for(int i=; i<=n; i++)if(!vis[i])dfs(i);
rebuild();
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
int x;
for(int i=; i<=n; i++)
{
while(scanf("%d",&x)!=EOF)
{
if(x==)break;
add(i,x);
}
}
tarjan();
if(scc==)ansl=,ansr=;
printf("%d\n%d\n",ansr,ansl);
} return ;
}

代码

POJ1236 - Network of Schools tarjan的更多相关文章

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

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

  2. POJ-1236 Network of Schools,人生第一道Tarjan....

    Network of Schools 题意:若干个学校组成一个计算机网络系统,一个学校作为出发端连接着若干个学校,信息可以传送到这些学校.被链接的学校不需要再次与出发端相连,现在问你:A:最少选几个学 ...

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

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

  4. POJ 1236 Network of Schools (Tarjan + 缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 48 ...

  5. POJ1236 Network of Schools (强连通)(缩点)

                                                                Network of Schools Time Limit: 1000MS   ...

  6. POJ 1236 Network of Schools Tarjan缩点

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22729   Accepted: 89 ...

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

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

  8. P2746 [USACO5.3]校园网Network of Schools(Tarjan)

    P2746 [USACO5.3]校园网Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 ...

  9. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

随机推荐

  1. SQL Server 2005导入Excel表问题

    EXCEL导入到SQL Server经常出现“文本被截断,或者一个或多个字符在目标代码页中没有匹配项” 原因: SQL Server的导入导出为了确定数据表的字段类型,取excel文件的前8行来判别. ...

  2. 如何在CentOS 7服务器上安装NodeJS

    你可以通过运行以下命令. 1 sudo yum install epel-release 现在可以使用yum命令安装Node.js了. 1 sudo yum install nodejs 因为在开发过 ...

  3. 转15个必须知道的chrome开发者技巧GIF

    在Web开发者中,Google Chrome是使用最广泛的浏览器.六周一次的发布周期和一套强大的不断扩大开发功能,使其成为了web开发者必备的工具.你可能已经熟悉了它的部分功能,如使用console和 ...

  4. [Effective JavaScript 笔记]第16条:避免使用eval创建局部变量

    js中的eval函数是一个强大.灵活的工具.强大的工具容易被滥用,所以了解是值得的.(本人只用过它来处理json数据).错误使用eval函数的方式一:允许它干扰作用域.调用eval函数会将其参数作为j ...

  5. Android 中 设置TextView垂直滚动

    布局文件 android:scrollbars="vertical" android:singleLine="false" 代码文件 ctl_tv_conten ...

  6. dubbo作为消费者注册过程分析

    请支持原创: http://www.cnblogs.com/donlianli/p/3847676.html   作者当前分析的版本为2.5.x.作者在分析的时候,都是带着疑问去查看代码,debug进 ...

  7. Jump Game | & ||

    Jump Game | Given an array of non-negative integers, you are initially positioned at the first index ...

  8. windows下不打开浏览器访问网页的方法

    我们打开电脑,大多时候都是打开浏览器在上网.这都是通过浏览器来实现的,然而windows下有没有办法不通过浏览器也可以像linux那样达到访问网页的目的呢?这当然少不了批处理或者VBScript.然而 ...

  9. iOS7总显示状态栏的解决方法

    转载http://blog.csdn.net/langresser_king/article/details/18351021 2014年2月份开始,苹果需求开发者必须使用xcode5开发游戏和应用, ...

  10. (转)SQL SERVER的锁机制(四)——概述(各种事务隔离级别发生的影响)

    六.各种事务隔离级别发生的影响 修改数据的用户会影响同时读取或修改相同数据的其他用户.即这些用户可以并发访问数据.如果数据存储系统没有并发控制,则用户可能会看到以下负面影响: · 未提交的依赖关系(脏 ...