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. Centos更换yum库镜像

    首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...

  2. windows下使用批处理文件调用python程序

    这个随笔涉及到几个批处理脚本得知识点. windows的start命令, 启动另一个窗口运行指定的程序或命令. windows的call命令, 从批处理程序调用另一个程序, 直到被调用程序退出, 再继 ...

  3. ThinkPHP访问不存在的模块跳到404页面

    在ACTION中新建一个文件EmptyAction.class.php,文件中的代码如下: <?php class EmptyAction extends Action{     functio ...

  4. 织梦channelid是什么?dede channel typeid有什么区别

    昨儿帮小伙伴整dedecms首页调用栏目文章,当时没注意用到的是channelid参数,修改了好多次赋值,新建了一个新的栏目获取id是156,添加栏目文章,把channelid改为156重新生成首页, ...

  5. dedecms发布文章时多个Tag间分割逗号自动变成英文逗号

    dedecms发布文章时经常会添加多个Tag,我们输入汉字时总是喜欢使用全角的逗号,那么有没有办法使用JS脚本把输入的Tag间中文逗号变成英文逗号呢? dedecms发布文章时多个Tag间分割逗号自动 ...

  6. Swift Tour 随笔总结 (1)

    let Constant var Variable let implicitInteger = 70 let implicitDouble = 70.0 let explicitDouble: Dou ...

  7. Sed替换行和字符shell

    1.在某一行后面追加一行 RD=2000sed -i '/ssi_types/ a\limit_req zone=lreq burst='$RD';' /opt/bee.location 2.替换字符 ...

  8. 第16章 使用Squid部署代理缓存服务

    章节概述: 本章节从代理缓存服务的工作原理开始讲起,让读者能够清晰理解正向代理(普通模式.透明模式)与反向代理的作用. 正确的使用Squid服务程序部署代理缓存服务可以有效提升访问静态资源的效率,降低 ...

  9. 关于ruby重构的过程中去除不必要的format

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) #这段话可以由下面的话替代56     respond_to do |format|57       ...

  10. poj2240最短路 floyd

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17360   Accepted: 7308 Descri ...