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. Java Socket发送与接收HTTP消息简单实现

    在上次Java Socket现实简单的HTTP服务我 们实现了简单的HTTP服务,它可以用来模拟HTTP服务,用它可以截获HTTP请求的原始码流,让我们很清楚的了解到我们向服务发的HTTP消息的结 构 ...

  2. dedecms首页调用的简介一直修改不了是自动文章摘要在作怪

    一位美女问:dedecms首页调用的简介一直修改不了,ytkah让她到具体的文章修改,然后再重新生成一下首页.她说还是不行.那就奇了怪了,点击到具体的文章页面是显示已经修改好了,为什么首页还是原来的呢 ...

  3. Java 参数的一些心得

    java 对象入参是传入的是引用(一块内存), 基础类型是值(复制内容),测试代码如下 public class TestA { private String name; public String ...

  4. C# 浅谈接口的优势

    总结了一下接口的小优势,可以便于新手理解为什么要用接口,用接口有什么好处. 1.接口的定义: 关键字:interface,接口名一般大写I开头,接口中定义方法,但是不实现方法 interface IB ...

  5. 搭建Nginx+JAVA环境

    搭建Nginx+JAVA环境 Apache对Java的支持很灵活,他们的结合度也很高,例如Apache+Tomcat和Apache+resin等都可以实现对Java应用的支持.Apache一般采用一个 ...

  6. poj1125最短路

    Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30408   Accepted: ...

  7. Linux system V

    Sysvinit 的小结 Sysvinit 的优点是概念简单.Service 开发人员只需要编写启动和停止脚本,概念非常清楚:将 service 添加/删除到某个 runlevel 时,只需要执行一些 ...

  8. Mysql数据库中设置root密码的命令及方法

    我们都知道通常PHP连接 Mysql都是通过root用户名和密码连接,默认情况下在Mysql安装时root初始密码为空,在安装使用PHP开源系统时,都需要填写连接Mysql数据库的用户名和密码,此时当 ...

  9. iOS 利用constraint实现2个控件上下的空白是相等的

    说的有点乱,先看个图把 其实这个constrant的目的就是控制两个方形的控件上方和下方的空白大小. 对于每一个方块来说,他们上方和下方的空白是相同的.这种“居中”的设计到处可见.一个控件想实现这种居 ...

  10. ubuntu dpkg 命令详解

    linux的包管理有多种,除了rpm,apt等还有优秀的dpkg,下面是dpkg命令的详细使用教程,希望对你有用.deb包的管理是比较优秀的包管理工具,用的linux系统有 debian ubuntu ...